Synthetics REST API version 1 (deprecated)

Deprecated

Currently New Relic supports two versions of the Synthetics API: v1 and v3. Version 3 was released October 2016. Version 1 is deprecated and will eventually no longer be available. No termination date has been announced. However, no further development or modifications will be made to v1.

Recommendation: Create new monitors using the v3 Synthetics API and migrate v1 scripts to their v3 equivalent.

Owner or Admins

To use the REST API for New Relic Synthetics, you must activate API access and generate your Admin User's API key from your account settings. Then you can make standard API calls via the command line.

Access to this feature depends on your subscription level.

Managing simple and scripted monitors

You must use your Admin User's API key to make Synthetics REST API calls. The account's REST API key will not work.

The Synthetics REST API limits an account's rate of requests to three requests per second. Requests made in excess of this threshold will return a 429 response code.

These examples show cURL commands:

To view a list of all monitors in New Relic Synthetics for your account, send a GET request to https://synthetics.newrelic.com/synthetics/api/v1/monitors. For example:

curl -v \
     -H 'X-Api-Key:{Admin_User_Key}' https://synthetics.newrelic.com/synthetics/api/v1/monitors

A successful request will return a 200 OK response. The data returned will be a JSON object in the following format:

{
  "count": integer,
  "monitors": [
    {
      "id": UUID,
      "name": string,
      "type": string,
      "frequency": integer,
      "uri": string,
      "locations": array of strings,
      "status": string,
      "slaThreshold": double,
      "userId": integer,
      "apiVersion": string
    }
  ]
}

To view a single existing monitor in New Relic Synthetics, send a GET request to https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}. Replace the {id} in the following example with the specific monitor ID.

curl -v \
     -H 'X-Api-Key:{Admin_User_Key}' https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}

A successful request will return a 200 OK response. The data returned will be a JSON object in the following format:

{
  "id": UUID,
  "name": string,
  "type": string,
  "frequency": integer,
  "uri": string,
  "locations": array of strings,
  "status": string,
  "slaThreshold": double,
  "userId": integer,
  "apiVersion": string
}

An invalid monitor ID will return the error 404 Not Found: The specified monitor doesn't exist.

To add a new monitor to your account in New Relic Synthetics, send a POST request to https://synthetics.newrelic.com/synthetics/api/v1/monitors with a JSON payload that describes the monitor:

{
  "name": string [required],
  "type": string (SIMPLE, BROWSER, SCRIPT_API, SCRIPT_BROWSER) [required],
  "frequency": integer (minutes) [required, must be one of 1, 5, 10, 15, 30, 60, 360, 720, or 1440],
  "uri": string [required for SIMPLE and BROWSER type],
  "locations": array of strings (send a GET request to https://synthetics.newrelic.com/synthetics/api/v1/locations to get a list of valid locations) [at least one required],
  "status": string (ENABLED, MUTED, DISABLED) [required],  
  "slaThreshold": double, 
}

In addition, to add a scripted monitor via the REST API, call an additional API endpoint to send the script for the monitor just created.

Replace the Synthetics REST API attributes in the following example with your specific values.

curl -v \
     -X POST -H 'X-Api-Key:{Admin_User_Key}' \
     -H 'Content-Type: application/json' https://synthetics.newrelic.com/synthetics/api/v1/monitors \
     -d '{  "name" : "monitor1",  "frequency" : 15, "uri" : "http://my-uri.com",   "locations" : [ "AWS_US_WEST_1" ],   "type" : "browser"}'

A successful request will return a 201 Created response, with the URI of the newly-created monitor specified in the location header. Possible error codes include:

  • 400 Bad Request: One or more of the monitor values is invalid, or the format of the request is invalid. For example, the frequency is out of bounds or one or more of the specified locations is invalid (See the error message in the body of the response.)
  • 402 Payment Required: Creating the monitor will increase your scheduled checks past your account's purchased check limit.

To update an existing monitor in New Relic Synthetics, send a PUT request to https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}. In addition, for scripted monitors, follow the procedures to update the BASE64 encoded script.

Replace the {id} in the following example with the specific monitor ID, and replace the Synthetics REST API attributes with your specific values.

curl -v \
     -X PUT -H 'X-Api-Key:{Admin_User_Key}' \
     -H 'Content-Type: application/json' https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id} \
     -d '{  "name" : "updated monitor name", "type" : "simple", "frequency" : 15, "uri" : "http://my-uri.com/",   "locations" : [ "AWS_US_WEST_1" ],   "status" : "enabled", "slaThreshold": "7.0" }'

PUT requests are intended to replace target entities, so all attributes required in the JSON payload when creating a new monitor are also required when updating an existing monitor.

A successful request will return a 204 No Content response, with an empty body. Possible error codes include:

  • 400 Bad Request: One or more of the monitor values is invalid, or the format of the request is invalid. For example, the frequency is out of bounds or one or more of the specified locations is invalid (See the error message in the body of the response.)
  • 404 Not Found: The specified monitor does not exist.

To delete an existing monitor in New Relic Synthetics, send a DELETE request to https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}. Replace the {id} in the following example with the specific monitor ID.

curl -v \
     -H 'X-Api-Key:{Admin_User_Key}' \
     -X DELETE https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}

A successful request will return a 204 No Content response, with an empty body. An unsuccessful request will return the response, 404 Not Found: The specified monitor does not exist.

To retrieve the list of valid locations in New Relic Synthetics, use the following command.

curl -v \
     -X GET -H 'X-Api-Key:{Admin_User_Key}' https://synthetics.newrelic.com/synthetics/api/v1/locations

Managing scripted monitors

In addition to the general API, there are several API methods for the monitor types Scripted Browser (SCRIPT_BROWSER) and API Test (SCRIPT_API).

These examples show cURL commands.

Get monitor script

To view the script associated with a specific SCRIPT_BROWSER or SCRIPT_API monitor in New Relic Synthetics for your account, send a GET request to https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}/script. Replace the {id} with the specific monitor ID. For example:

curl -v
     -H 'X-Api-Key: {Admin_User_Key}' 
     https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}/script

A successful request will return a 200 OK response. The data returned will be a JSON object in the following format:

{
  "scriptText": BASE64 encoded string
}

Possible error codes include:

  • 403 Forbidden: The specified monitor is not of type SCRIPT_BROWSER or SCRIPT_API.
  • 404 Not Found: The specified monitor doesn't exist or the script associated with the monitor doesn't exist.
Add scripted monitor

To add a new scripted monitor to New Relic Synthetics with the REST API:

  1. Follow standard API procedures to add a new monitor, and identify the type as a SCRIPT_BROWSER or SCRIPT_API.
  2. Update the new monitor with a BASE64 encoded version of the script to the ${MONITOR_UUID}/script endpoint.

For more information, refer to the example.

Update monitor script

To update the script associated with a specific SCRIPT_BROWSER or SCRIPT_API monitor in New Relic Synthetics for your account, send a PUT request to https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}/script with a JSON payload that contains the scriptText (required). The scriptLocations data is required only for private locations with Verified Script Execution turned on.

The password used to generate the HMAC string must match the password set for the private location. When generating the HMAC string, use the SHA256 algorithm.

{
  "scriptText": BASE64 encoded String,
  "scriptLocations": [
    {
      "name": Location name,
      "hmac" BASE64 encoded String of SHA256 HMAC for location
    }
  ]
}

Replace the {id} with the specific monitor ID. Here is an example for the script:

var assert = require('assert');
assert.equal('1', '1');

This example uses password as the password for the scriptLocation.

curl -v
	-H 'X-Api-Key: '{Admin_User_Key}'
	-H 'content-type: application/json'
	https://synthetics.newrelic.com/synthetics/api/v1/monitors/{id}/script
	-d '{ "scriptText": "dmFyIGFzc2VydCA9IHJlcXVpcmUoJ2Fzc2VydCcpOw0KYXNzZXJ0LmVxdWFsKCcxJywgJzEnKTs=","scriptLocations": [ { "name": "my_vse_enabled_location", "hmac": "MjhiNGE4MjVlMDE1N2M4NDQ4MjNjNDFkZDEyYTRjMmUzZDE3NGJlNjU0MWFmOTJlMzNiODExOGU2ZjhkZTY4"} ]}'
     

A successful request will return a 204 No Content response with an empty body. Possible error codes include:

  • 400 Bad Request: Invalid BASE64 encoded string for scriptText or hmac.
  • 403 Forbidden: The specified monitor is not of the type SCRIPT_BROWSER or SCRIPT_API.
  • 404 Not Found: The specified monitor does not exist.

Scripted browser example

Here is an example of using New Relic's REST API and the bash script to create a scripted browser monitor.

Scripted browser API example

The following example shows cURL commands to create a scripted browser monitor.

  • At the top of the script, replace the variables with your specific values.
  • For the scriptfile variable, identify the filename for the script to be created. Here is a sample script that can be saved as sample_synth_script.js to use in the example:
var assert = require("assert");
$browser.get("http://example.com").then(function(){ 
  // Check the H1 title matches "Example Domain" 
  return $browser.findElement($driver.By.css("h1")).then(function(element){ 
    return element.getText().then(function(text){ 
      assert.equal("Example Domain", text, "Page H1 title did not match"); 
    }); 
  }); 
}).then(function(){ 
  // Check that the external link matches "http://www.iana.org/domains/example" 
  return $browser.findElement($driver.By.css("div > p > a")).then(function(element){ 
    return element.getAttribute("href").then(function(link){ 
      assert.equal("http://www.iana.org/domains/example", link, "More information link did not match"); 
    }); 
  }); 
});  
Bash script example

This example show the bash script that will create the SCRIPTED_BROWSER monitor.

#!/bin/bash

# Admin API key from your account settings
adminAPIKey=''
# Other attributes found at https://docs.newrelic.com/docs/apis/synthetics-rest-api/monitor-examples/attributes-synthetics-rest-api#api-attributes
monitorName='Test API Script'
monitorType='SCRIPT_BROWSER'
frequency=1440
locations='"AWS_US_WEST_1", "AWS_US_EAST_1"'
slaThreshold=7.0
# Location of the file with your script
scriptfile=sample_synth_script.js

# Test that the script file exists (does not validate content)
if [ -e "$scriptfile" ]
then
  script=$(cat "$scriptfile")

  payload="{  \"name\" : \"$monitorName\", \"frequency\" : $frequency,    \"locations\" : [ $locations ],   \"status\" : \"ENABLED\",  \"type\" : \"$monitorType\", \"slaThreshold\" : $slaThreshold, \"uri\":\"\"}"
  echo "Creating monitor"

  # Make cURL call to API and parse response headers to get monitor UUID
  shopt -s extglob # Required to trim whitespace; see below
  while IFS=':' read key value; do
    # trim whitespace in "value"
    value=${value##+([[:space:]])}; value=${value%%+([[:space:]])}
    case "$key" in
        location) LOCATION="$value"
                ;;
        HTTP*) read PROTO STATUS MSG <<< "$key{$value:+:$value}"
                ;;
    esac
  done < <(curl -sS -i  -X POST -H "X-Api-Key:$adminAPIKey" -H 'Content-Type: application/json' https://synthetics.newrelic.com/synthetics/api/v1/monitors -d "$payload")

  # Validate monitor creation & add script unless it failed
  if [ $STATUS = 201 ]; then
    echo "Monitor created, $LOCATION "
    echo "Uploading script"
      # base64 encode script
      encoded=`echo "$script" | base64`
      scriptPayload='{"scriptText":"'$encoded'"}'
        curl -s -X PUT -H "X-Api-Key:$adminAPIKey" -H 'Content-Type: application/json' "$LOCATION/script" -d $scriptPayload
        echo "Script uploaded"
  else
    echo "Monitor creation failed"
  fi

else
  echo "script file not found, not creating monitor"
fi

Synthetics attributes

Here are the attributes used with Synthetics REST API calls, listed in alphabetical order.

Synthetics API attribute Definition
apiVersion String: The version number.
emails Array of strings: Email addresses for alert notifications with New Relic Synthetics.
frequency Integer: Number of minutes between checks. Valid values include 1, 5, 15, 30, 60, 360, 720, and 1440.
id The UUID for the specific Synthetics monitor.
locations Array of strings: Array of locations by full label.
name String: The monitor's name.
scriptLocations String: The name and hmac values for private locations using Verified Script Execution.
scriptText String: The BASE64 encoded text for scripted monitors.
slaThreshold Double: Value for the Synthetics SLA report, in seconds.
status String: Valid values include ENABLED, MUTED, and DISABLED.
type

String: Type of monitor. Valid values include:

  • SIMPLE (Ping)
  • BROWSER
  • SCRIPT_BROWSER
  • SCRIPT_API
uri String: The URI for SIMPLE and BROWSER monitor types; for example, http://my-site.com. Optional for SCRIPT_BROWSER and SCRIPT_API.
userID Integer: The specific user ID.

Specific monitor endpoint

When making REST API calls for a specific monitor, include the monitor_uuid as part of the endpoint. The monitor_uuid is the GUID which is part of the URL. For example, a selected Synthetics monitor has this URL:

https://synthetics.newrelic.com/accounts/nnnn/monitors/ab123-c456d-e78-90123-f45g

The monitor_uuid is the value that follows /monitors/.

For more help

Additional documentation resources include:

Recommendations for learning more: