API tests are powered by the got module, which is available through the $http object. The $http object provides a custom request-like experience with got, giving your monitors backwards compatibility for basic use cases. The request-like experience provided by the $http object will also be returned for any customers attempting to use request directly in Node.js 16 and newer scripted API runtimes.
Result timing details will be provided as long as you use the $http object. For scripted API use cases not covered by the $http object, you can use the $har object to report custom timing details.
Important
After a maximum run time of three minutes, New Relic manually stops the script.
To make a GET request, use the $http.get method to submit your request. Define your request options, make your request using $http.get, then validate the response to ensure your endpoint is returning the correct results.
Send a POST request
To make a POST request, use the $http.post method to submit your request. Define your request options, make your request using $http.post, then validate the response to ensure your endpoint is returning the correct results.
nrql: 'SELECT average(duration) FROM Transaction',
},
}),
};
// Define expected results using callback function
function callback(err, response, body) {
// Log JSON results from endpoint to Synthetics console
console.log(body);
console.log('Script execution completed');
}
// Make POST request, passing in options and callback
$http.post(options, callback);
This example POSTs a custom event containing static integers to the Event API:
//Define your authentication credentials.
var myAccountID = '{YOUR_ACCOUNT_ID}';
var myAPIKey = '{YOUR_API_KEY}';
//Import the 'assert' module to validate results.
var assert = require('assert');
var options = {
//Define endpoint URL.
url: "https://insights-collector.newrelic.com/v1/accounts/"+myAccountID+"/events",
//Define body of POST request.
body: '[{"eventType":"SyntheticsEvent","integer1":1000,"integer2":2000}]',
//Define New Relic API key and expected data type.
headers: {
'Api-Key': myAPIKey,
'Content-Type': 'application/json'
}
};
//Define expected results using callback function.
function callback(error, response, body) {
//Log status code to Synthetics console.
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, 'Expected 200 OK response');
//Verify that `body` contains element named `success` with a value of `true`.
assert.ok(body.success == true, 'Expected True results in Response Body, result was ' + body.success);
//Log end of script.
console.log("End reached");
}
//Make POST request, passing in options and callback.
$http.post(options, callback);
Validate results
To validate your results, import the assert module to define your test case. Call an assert method to validate your endpoint's response. If any assert functions fail, the entire monitor will be considered a failed check. This may trigger alert notifications and affect your metrics.
Important
Synthetic monitoring does not allow thrown exceptions. Thrown exceptions result in script failure. Use the assert module to validate your results, and use console.log() to log results to the synthetic's console.
This example POSTs to the Event API, then validates that the response is {"success":true}: