Use synthetic monitoring's API tests to monitor your API endpoint to ensure it is functioning correctly. New Relic uses the http-request module internally to make HTTP calls to your endpoint and validate the results.
Here we present some example functions showing how to use the
$http object to submit your request. For detailed documentation on the options available for this object, see the http-request readme. (Note that Request is deprecated, but these options still apply.)
To view and share other API test examples, visit the synthetics scripts section in Explorers Hub.
API tests are powered by the http-request module, which is available through the
$http object. Once each frequency interval, New Relic queries your endpoint from each of your selected locations. For instructions on creating a monitor, see Adding monitors.
After a maximum run time of three minutes, New Relic manually stops the script.
To start your script:
- Declare a variable (such as
options) to store your request options object.
- Define request options such as the URL endpoint, and custom headers.
- If you're setting SSL or agent options, see SSL and agentOptions requirements. We recommend using SSL to avoid exposing plain text credentials in your headers.
For a full list of supported request options, see request(options, callback) in the
http-request documentation on GitHub.
Here's an example of optional metadata in the options object:
For SSL and agentOptions: If you are setting SSL options or providing an
agentOptions object, the
agent property in the request
options object will need to be set to
$globalAgents.http to ensure your HTTP requests use the instrumented global agent.
Here's an example of using a SSL option or
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.
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.
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.
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.
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's and and documentation.