---
title: NerdGraph tutorial: Alert policies
source: https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-api-alerts-policies
---

For New Relic alerting, you can manage your [alert policies](https://docs.newrelic.com/docs/alerts-applied-intelligence/new-relic-alerts/alert-policies/create-edit-or-find-alert-policy) using our NerdGraph API.

> #### 💡 TIP
>
> For how to get started with NerdGraph, see [Introduction to NerdGraph](https://docs.newrelic.com/docs/apis/nerdgraph/get-started/introduction-new-relic-nerdgraph).

## List and filter policies [#list-and-filter]

The `policiesSearch` query allows you to paginate through all of your [alert policies](https://docs.newrelic.com/docs/alerts-applied-intelligence/new-relic-alerts/alert-policies/create-edit-or-find-alert-policy) per account. It also allows some filtering functionality on the account policies.

**Listing all policies for an account**

Here's an example:

````graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policiesSearch {
          policies {
            id
            name
            incidentPreference
          }
        }
      }
    }
  }
}
```

````

**Paginating through policies with cursor pagination**

In order to paginate through your policies, you must request the `nextCursor` field on your initial query.

With cursor pagination, you continue to make a request through the result set until the `nextCursor` that is returned from the response comes back empty. This signifies that you reached the end of your results.

Here's an example:

````graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policiesSearch {
          nextCursor
          policies {
            id
            name
            incidentPreference
          }
          totalCount
        }
      }
    }
  }
}
```

The code above returns a set of results like this:

```json
{
  "data": {
    "actor": {
      "account": {
        "alerts": {
          "policiesSearch": {
            "nextCursor": "/8o0y2qiR54m6thkdgHgwg==:jZTXDFKbTkhKwvMx+CtsPVM=",
            "policies": [
              {
                "id": "3455",
                "incidentPreference": "PER_POLICY",
                "name": "First Policy Name"
              },
              {
                "id": "2123",
                "incidentPreference": "PER_POLICY",
                "name": "Another Policy"
              },
              // ... more policies here in reality
            ],
            "totalCount": 745
          }
        }
      }
    }
  }
}
```

So, in your subsequent request, provide the cursor like so, until the cursor is empty:

```graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policiesSearch(cursor: "/8o0y2qiR54m6thkdgHgwg==:jZTXDFKbTkhKwvMx+CtsPVM=") {
          nextCursor
          policies {
            id
            name
            incidentPreference
          }
          totalCount
        }
      }
    }
  }
}
```

````

**Find all policies by selected ids**

The API allows policy queries by a sub-select of ids. This will only return the information for these policies that you provide.

````graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policiesSearch(searchCriteria: {
          ids: [ A_POLICY_ID, ANOTHER_POLICY_ID ]
        }) {
          policies {
            id
            name
            incidentPreference
          }
        }
      }
    }
  }
}
```

````

**Find all policies by name**

The API allows policy queries by name. Use `name` for matching by exact names or `nameLike` for a partial match. Both search criteria are case insensitive. This will only return the information for the policies that match the name supplied.

In this example, we want to find policies with `DevOps` in the name:

````graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policiesSearch(searchCriteria: {
          nameLike: "DevOps"
        }) {
          policies {
            id
            name
          }
        }
      }
    }
  }
}
```

````

**Find policy by id**

The API lets you query by policy id:

````graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      alerts {
        policy(id: YOUR_POLICY_ID) {
          id
          name
          incidentPreference
        }
      }
    }
  }
}
```

````

## Create a policy [#create-policy]

In order to create a policy, supply a name and an `incidentPreference`.

The incident preference will configure how incidents get created for each condition created in the policy. For more information, refer to the documentation about [choosing your alert event preference](https://docs.newrelic.com/docs/alerts/organize-alerts/specify-when-alerts-create-events).

```graphql
mutation {
  alertsPolicyCreate(accountId: YOUR_ACCOUNT_ID, policy: {
    name: "Your Policy Name"
    incidentPreference: PER_CONDITION
  }) {
    id
    name
    incidentPreference
  }
}
```

## Update a policy [#update-policy]

When you update a policy, note that you don't need to supply all of the attributes on the policy. For example, you only need to supply the name if you only intend to update the name:

```graphql
mutation {
  alertsPolicyUpdate(accountId: YOUR_ACCOUNT_ID, id: YOUR_POLICY_ID, policy: {
    name: "Updated Policy Name"
  }) {
    id
    name
    incidentPreference
  }
}
```

## Delete a policy [#delete-policy]

You can delete policies via the NerdGraph API. Note that only the id may be requested back from a deleted resource:

```graphql
mutation {
  alertsPolicyDelete(accountId: YOUR_ACCOUNT_ID, id: YOUR_POLICY_ID) {
    id
  }
}
```
