{"id":49422,"date":"2025-05-21T11:51:30","date_gmt":"2025-05-21T11:51:30","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=49422"},"modified":"2025-05-22T12:27:44","modified_gmt":"2025-05-22T12:27:44","slug":"new-relic-api-guide-step-by-step-tutorial-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/new-relic-api-guide-step-by-step-tutorial-with-example\/","title":{"rendered":"New Relic API Guide \u2013 Step-by-Step Tutorial with Example"},"content":{"rendered":"\n<p>Here is a <strong>step-by-step tutorial on how to work with the New Relic API<\/strong>, including how to <strong>create an alert condition<\/strong> using the API.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\ude80 New Relic API Guide \u2013 Step-by-Step Tutorial with Example (Create Alert)<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddf0 Prerequisites<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Requirement<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>\u2705 New Relic Account<\/td><td>Ensure you have access to New Relic<\/td><\/tr><tr><td>\u2705 API Key<\/td><td>You need a <a href=\"https:\/\/one.newrelic.com\/launcher\/api-keys-ui.api-keys-launcher\" target=\"_blank\" rel=\"noopener\">Personal API Key<\/a> (not ingest license)<\/td><\/tr><tr><td>\u2705 Account ID<\/td><td>You can find your Account ID in the top-right profile dropdown in NR One<\/td><\/tr><tr><td>\u2705 <code>curl<\/code> or Postman<\/td><td>Use <code>curl<\/code>, Postman, or any HTTP client for making API requests<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 Step 1: Get Your API Key &amp; Account ID<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to: <strong><a href=\"https:\/\/one.newrelic.com\/launcher\/api-keys-ui.api-keys-launcher\/\" target=\"_blank\" rel=\"noopener\">https:\/\/one.newrelic.com\/launcher\/api-keys-ui.api-keys-launcher\/<\/a><\/strong><\/li>\n\n\n\n<li>Click <strong>+ Create a key<\/strong><\/li>\n\n\n\n<li>Choose <strong>Personal<\/strong> or <strong>User<\/strong> API key<\/li>\n\n\n\n<li>Save the <strong>Key<\/strong> and <strong>Account ID<\/strong> (needed for all requests)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 Step 2: Understand the API Base URL<\/h2>\n\n\n\n<p>Use the <strong>GraphQL endpoint<\/strong> for most operations:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">https:<span class=\"hljs-comment\">\/\/api.newrelic.com\/graphql<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Some older <strong>REST endpoints<\/strong> (like for alerts) are still at:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">https:<span class=\"hljs-comment\">\/\/api.newrelic.com\/v2\/...<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 Step 3: Create an Alert Policy (REST API)<\/h2>\n\n\n\n<p><strong>API Endpoint:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">POST https:<span class=\"hljs-comment\">\/\/api.newrelic.com\/v2\/alerts_policies.json<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Headers:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTTP\" data-shcb-language-slug=\"http\"><span><code class=\"hljs language-http\"><span class=\"hljs-attribute\">Api-Key<\/span>: &lt;YOUR_API_KEY&gt;\n<span class=\"hljs-attribute\">Content-Type<\/span>: application\/json\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTTP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">http<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Request Body:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{\n  <span class=\"hljs-attr\">\"policy\"<\/span>: {\n    <span class=\"hljs-attr\">\"name\"<\/span>: <span class=\"hljs-string\">\"High CPU Usage Policy\"<\/span>,\n    <span class=\"hljs-attr\">\"incident_preference\"<\/span>: <span class=\"hljs-string\">\"PER_POLICY\"<\/span>\n  }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example cURL:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">curl -X POST <span class=\"hljs-string\">'https:\/\/api.newrelic.com\/v2\/alerts_policies.json'<\/span> \\\n  -H <span class=\"hljs-string\">'Api-Key: YOUR_API_KEY'<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> \\\n  -d <span class=\"hljs-string\">'{\n        \"policy\": {\n          \"name\": \"High CPU Usage Policy\",\n          \"incident_preference\": \"PER_POLICY\"\n        }\n      }'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>\u2705 <strong>Response will include <code>id<\/code><\/strong> \u2014 save this <code>policy_id<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 Step 4: Create an Alert Condition (e.g., High CPU)<\/h2>\n\n\n\n<p><strong>API Endpoint:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">POST https:\/\/api.newrelic.com\/v2\/alerts_conditions\/policies\/<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">policy_id<\/span>&gt;<\/span>.json\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">curl -X POST <span class=\"hljs-string\">'https:\/\/api.newrelic.com\/v2\/alerts_conditions\/policies\/6269799.json'<\/span> \\\n  -H <span class=\"hljs-string\">'Api-Key: NRAK-3T8WTXSP4KJSLMG74WHR5I3SSY9'<\/span> \\\n  -H <span class=\"hljs-string\">'Content-Type: application\/json'<\/span> \\\n  -d <span class=\"hljs-string\">'{\n        \"condition\": {\n          \"type\": \"apm_app_metric\",\n          \"name\": \"Error Rate Alert for devopsschool\",\n          \"enabled\": true,\n          \"condition_scope\": \"application\",\n          \"entities\": &#91;1508171253],\n          \"metric\": \"error_percentage\",\n          \"terms\": &#91;\n            {\n              \"duration\": \"5\",\n              \"operator\": \"above\",\n              \"priority\": \"critical\",\n              \"threshold\": \"5\",\n              \"time_function\": \"all\"\n            }\n          ]\n        }\n      }'<\/span>\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"type\": \"apm_app_metric\"<\/code> \u2014 type of monitoring (can also be <code>\"infra_metric\"<\/code>, <code>\"nrql\"<\/code> etc.)<\/li>\n\n\n\n<li><code>\"entities\"<\/code> \u2014 Application or host ID (can be fetched from other APIs)<\/li>\n\n\n\n<li><code>\"metric\": \"cpu_percent\"<\/code> \u2014 Metric name<\/li>\n\n\n\n<li><code>\"terms\"<\/code> \u2014 Defines the condition (thresholds, duration, etc.)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcd8 Step 5: Verify the Alert in New Relic UI<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to <strong>Alerts &amp; AI<\/strong> \u2192 <strong>Alert Policies<\/strong><\/li>\n\n\n\n<li>Check if &#8220;High CPU Usage Policy&#8221; and &#8220;CPU Alert&#8221; are visible and enabled.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Bonus: List Alert Policies (Get API)<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">curl -X GET <span class=\"hljs-string\">'https:\/\/api.newrelic.com\/v2\/alerts_policies.json'<\/span> \\\n  -H <span class=\"hljs-string\">'Api-Key: YOUR_API_KEY'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f Advanced: Create Alerts using NRQL Condition (for Logs or Custom Queries)<\/h2>\n\n\n\n<p><strong>Endpoint:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">POST https:\/\/api.newrelic.com\/v2\/alerts_nrql_conditions\/policies\/<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">policy_id<\/span>&gt;<\/span>.json\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">curl -X POST <span class=\"hljs-string\">\"https:\/\/api.newrelic.com\/v2\/alerts_nrql_conditions\/policies\/123456.json\"<\/span> \\\n  -H <span class=\"hljs-string\">\"Api-Key: YOUR_API_KEY\"<\/span> \\\n  -H <span class=\"hljs-string\">\"Content-Type: application\/json\"<\/span> \\\n  -d <span class=\"hljs-string\">'{\n    \"nrql_condition\": {\n      \"name\": \"Too many 404s\",\n      \"enabled\": true,\n      \"type\": \"static\",\n      \"nrql\": {\n        \"query\": \"SELECT count(*) FROM Log WHERE message LIKE '<\/span>%<span class=\"hljs-number\">404<\/span>%<span class=\"hljs-string\">'\"\n      },\n      \"terms\": &#91;\n        {\n          \"duration\": \"5\",\n          \"operator\": \"above\",\n          \"priority\": \"critical\",\n          \"threshold\": \"10\",\n          \"time_function\": \"all\"\n        }\n      ]\n    }\n  }'<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddfe Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>Action<\/th><th>API Used<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Create Alert Policy<\/td><td><code>\/v2\/alerts_policies.json<\/code><\/td><\/tr><tr><td>2<\/td><td>Create Metric or NRQL Condition<\/td><td><code>\/v2\/alerts_conditions\/...<\/code><\/td><\/tr><tr><td>3<\/td><td>(Optional) Associate Notification<\/td><td>Notification Channels API<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. \ud83d\ude80 New Relic API Guide \u2013&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-49422","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/49422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=49422"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/49422\/revisions"}],"predecessor-version":[{"id":49439,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/49422\/revisions\/49439"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=49422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=49422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=49422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}