Unable to create *new* labels through New Relic API

New Relic Labels API
The above page, and associated REST API, does not work if you are creating a new label.

Eg. using the following JSON will return an HTTP 500, assuming the label pair “NewCategory:test” does not already exist in your account:

{
  "label": {
    "category": "NewCategory",
    "name": "test",
    "links": {
      "applications": [],
      "servers": [<my_server_id>]
    }
  }
}

If I then go into the New Relic UI and create the label NewCategroy:test, then I can successfully add additional servers / applications using the above JSON, but I need to create each LabelCategory:LabelName pair in the UI before this will work, which is far from ideal when we want to automate the (re)creation of an environment in a reproduce-able manner.

Note, I did try a curl issuing a POST rather than a PUT on the off chance this would allow creation of a new label, but it acted just like a GET (returned HTTP 200 along with the list of labels).

  • Is there a different API to create new labels? There is nothing documented in the Api Explorer.
  • If this a bug in the Create Label API?

Hi @andrew.sibley - You need an Admin key to create or delete items using the API’s.

Thanks @stefan_garnham for the very quick reply!

I tried using my Admin API Key in place of the standard API key, but got the same results.

The way I’m using the Admin API Key seems to be correct, as I can successfully issue GETs, and add servers to existing labelCategory:labelName pairs using PUT, however I still can’t create new labelCategory:labelName pairs (I get HTTP 500 in this case). I tried again using a POST with my Admin API Key, and again just got HTTP 200 and no modifications were made to the labels.

The test curl from the API Explorer is pasted below (this still gives HTTP 500):

curl -X PUT 'https://api.newrelic.com/v2/labels.json' \
     -H 'X-Api-Key:<my_admin_api_key>' -i \
     -H 'Content-Type: application/json' \
     -d \
'{
  "label": {
    "category": "NewCategory",
    "name": "test",
    "links": {
      "applications": [],
      "servers": [<my_server_id>]
    }
  }
}'

Try removing the applications section from the json as I don’t think that is required.

@stefan_garnham - Same result I’m afraid even without the applications section.

FYI, leaving the empty application section in there works fine, as long as the labelCategory:labelName pair already exists.

I’ve found out that this only occurs in some New Relic Accounts, not all. I’m not sure why. I have raised a ticket with New Relic.

@andrew.sibley I see that a Technical Support Engineer is still working on this with you via ticket. Once you have come to a conclusion would you mind sharing your findings? I’m sure others will find it useful.

Support investigated and found that this is an issue when an account has servers, but no APMs (I assume it could also be an issue the other way around).

Even though the list of applications passed in the REST call is empty, the New Relic API is still looking for any applications it should apply the change to, and since there are no registered apps, is failing.

The workaround they suggested (which I haven’t had time to try yet) is to add an application temporarily to the account and then remove it. They have also added this to their bugs list, so it may be fixed in a future release.

1 Like