Here is a step-by-step tutorial on how to work with the New Relic API, including how to create an alert condition using the API.
š New Relic API Guide ā Step-by-Step Tutorial with Example (Create Alert)
š§° Prerequisites
Requirement | Description |
---|---|
ā New Relic Account | Ensure you have access to New Relic |
ā API Key | You need a Personal API Key (not ingest license) |
ā Account ID | You can find your Account ID in the top-right profile dropdown in NR One |
ā
curl or Postman | Use curl , Postman, or any HTTP client for making API requests |
š Step 1: Get Your API Key & Account ID
- Go to: https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher/
- Click + Create a key
- Choose Personal or User API key
- Save the Key and Account ID (needed for all requests)
š Step 2: Understand the API Base URL
Use the GraphQL endpoint for most operations:
https://api.newrelic.com/graphql
Code language: JavaScript (javascript)
Some older REST endpoints (like for alerts) are still at:
https://api.newrelic.com/v2/...
Code language: JavaScript (javascript)
š Step 3: Create an Alert Policy (REST API)
API Endpoint:
POST https://api.newrelic.com/v2/alerts_policies.json
Code language: JavaScript (javascript)
Headers:
Api-Key: <YOUR_API_KEY>
Content-Type: application/json
Code language: HTTP (http)
Request Body:
{
"policy": {
"name": "High CPU Usage Policy",
"incident_preference": "PER_POLICY"
}
}
Code language: JSON / JSON with Comments (json)
Example cURL:
curl -X POST 'https://api.newrelic.com/v2/alerts_policies.json' \
-H 'Api-Key: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"policy": {
"name": "High CPU Usage Policy",
"incident_preference": "PER_POLICY"
}
}'
Code language: PHP (php)
ā
Response will include id
ā save this policy_id
.
š Step 4: Create an Alert Condition (e.g., High CPU)
API Endpoint:
POST https://api.newrelic.com/v2/alerts_conditions/policies/<policy_id>.json
Code language: HTML, XML (xml)
Example:
curl -X POST 'https://api.newrelic.com/v2/alerts_conditions/policies/6269799.json' \
-H 'Api-Key: NRAK-3T8WTXSP4KJSLMG74WHR5I3SSY9' \
-H 'Content-Type: application/json' \
-d '{
"condition": {
"type": "apm_app_metric",
"name": "Error Rate Alert for devopsschool",
"enabled": true,
"condition_scope": "application",
"entities": [1508171253],
"metric": "error_percentage",
"terms": [
{
"duration": "5",
"operator": "above",
"priority": "critical",
"threshold": "5",
"time_function": "all"
}
]
}
}'
Code language: PHP (php)
š Explanation:
"type": "apm_app_metric"
ā type of monitoring (can also be"infra_metric"
,"nrql"
etc.)"entities"
ā Application or host ID (can be fetched from other APIs)"metric": "cpu_percent"
ā Metric name"terms"
ā Defines the condition (thresholds, duration, etc.)
š Step 5: Verify the Alert in New Relic UI
- Go to Alerts & AI ā Alert Policies
- Check if “High CPU Usage Policy” and “CPU Alert” are visible and enabled.
š§ Bonus: List Alert Policies (Get API)
curl -X GET 'https://api.newrelic.com/v2/alerts_policies.json' \
-H 'Api-Key: YOUR_API_KEY'
Code language: JavaScript (javascript)
š ļø Advanced: Create Alerts using NRQL Condition (for Logs or Custom Queries)
Endpoint:
POST https://api.newrelic.com/v2/alerts_nrql_conditions/policies/<policy_id>.json
Code language: HTML, XML (xml)
Example:
curl -X POST "https://api.newrelic.com/v2/alerts_nrql_conditions/policies/123456.json" \
-H "Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"nrql_condition": {
"name": "Too many 404s",
"enabled": true,
"type": "static",
"nrql": {
"query": "SELECT count(*) FROM Log WHERE message LIKE '%404%'"
},
"terms": [
{
"duration": "5",
"operator": "above",
"priority": "critical",
"threshold": "10",
"time_function": "all"
}
]
}
}'
Code language: PHP (php)
š§¾ Summary
Step | Action | API Used |
---|---|---|
1 | Create Alert Policy | /v2/alerts_policies.json |
2 | Create Metric or NRQL Condition | /v2/alerts_conditions/... |
3 | (Optional) Associate Notification | Notification Channels API |
Iām a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND