Relic Solution: Auto-Labelling Apps With APIs


You might be familiar with one or more of the New Relic APIs. But are you using these APIs in a way that gets more done with less effort?
Let’s look at how we can nest some fairly simple API calls together to create a powerful solution.
We will look at a fairly common workflow people encounter while using New Relic and how we can streamline it with our APIs:
Please note we will not be covering the basics of API keys and the API explorer here, if you would like to learn about those then click here.

Labelling an Application Based on its Name

There are many reasons why labels are useful:

  • searchability
  • categorisation
  • alerting

Though it is probably easiest to apply a label when configuring your New Relic agent for the first time you may have either not added the labels or your label needs may have changed.

Let’s look at one specific scenario:

You’ve started with a naming convention of adding the environment to the application name for example “Prod - My Application” or “Staging - My Application” and now you have realised that using Environent:Production and Environment:Staging labels would be better in terms of searchability and targeting your app for alerts.

What do you do?

You could manually apply the labels, or update the application configurations but if you have a large number of apps or services this could be a very laborious process. Enter the API!

First of all, let’s think about what we want to achieve here and break it down into steps.

  1. Get the details or all our apps/services.
  2. Identify if the names contain a specific string, say “prod”.
  3. Create a label for example “Environment:Production” and apply it to all apps/services where the name contains “prod”
  4. Smile

Or for those of you who prefer a flow chart:

*please note that “New Relic Data” is a rather radical over-simplification of how things actually work - this is just for illustration.

Disclaimer: this is my first time writing a Node.JS script so I am sure there are more elegant ways to do this - however it is functional.

You will need to replace "YOUR_ADMIN_KEY_HERE" with your Admin API Key.

If you like this, please watch out for my next post on how to automate setting your Apdex T value for all applications.

We’d love to hear your thoughts on this post and some of the things you have done with New Relic APIs.

Remember, if you are interested in learning more about APIs please check out

Happy automating!

var request = require("request");

var adminKey = "YOUR_ADMIN_KEY_HERE";

var headers = {
    "Content-Type": "json/application",
    "X-Api-Key": adminKey

var options = {
    url: "",
    method: 'GET',
    headers: headers

  function(error, response, body) {
    if (error) return onErr(error);
    if (!error && response.statusCode == 200) {
      var result = JSON.parse(body);
      console.log('Number of applications found:'+result["applications"].length);
      for (var i = 0; i < result["applications"].length; ++i) { 
        var application = result["applications"][i]
        var appName  = application["name"];
        var lowerCaseName = appName.toLowerCase();

        if (lowerCaseName.indexOf('prod')==-1) {
          console.log ('application name: '+application["name"]+' does not contain prod');
          console.log('application name: '+application["name"]+' contains prod');

function addLabel(application){

console.log ('application name: '+application["name"]+ ' application ID '+application["id"]);
  var request = require("request");

  var options = { method: 'PUT',
    url: '',
      'X-Api-Key': adminKey,
      'Content-Type': 'application/json' },
    { label: 
        { category: 'Environment',
          name: 'Production',
          links: { applications: [ application["id"] ] } } },
    json: true };

  request(options, function (error, response, body) {
    if (error) throw new Error(error);


function onErr(err) {
    return 1;