This document is targeted to the Node 16.10 scripted API runtime and newer. Details for the
http-request based Node 10 and older runtimes
Use synthetic monitoring's API tests to monitor your API endpoint to ensure it is functioning correctly. New Relic uses the got 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. The
$http object provides a custom
request-like experience while being powered by
got to provide backward compatibility for basic use cases while avoiding the use of the deprecated
request module. Not all advanced use cases of
request are or will be supported by this backward compatibility. For detailed documentation on the options available for this object, see the got module readme. The
got module documentation includes a migration guide highlighting changes to expect from the
request module used in previous scripted API runtimes.
request-like experience provided by the
$http object will also be returned for any customers attempting to use
request directly in Node 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.
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 got options object.
- Define request options such as the URL endpoint and custom headers.
For a full list of supported options, see got options in the
got documentation on GitHub.
Here's an example of optional metadata in the options object:
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.