REST API endpoint test using New Relic Synthetic API tests

Dumindu Yapa, MSc
3 min readMar 29, 2021

--

If you want to detect and resolve the poor performance of your application before your customers notice, New Relic’s synthetic monitoring feature would be an ideal option to initiate.

There are four types of synthetic monitors

From this story, I’m going to talk about an API test monitor which can be used 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.

  1. First, you need to log in to your New Relic account

2. You can see the synthetic tab on the main page. Under the synthetic tab, you need to click create monitor option.

3. Then you can select which monitor you are going to select as of your journey. As this topic relates to the API Test monitor we can select it as it’s.

You should provide a monitor name & choose specific locations to hit the API route.

  • Monitor Type: API Test
  • Monitor Name: Test Monitor
  • Monitoring Location: (By choosing a location, we can be able to know what would be the application performance for the customers, who are based in those selected areas)
  • Set the schedule: You can set a predefined schedule time to hit the API route that we need to check from the above-selected locations.
  • The last step of the configuration is writing the script

To start your script:

  • You can define your own variables.
  • Define request options such as the URL endpoint and custom headers.

Example:

  1. To make a GET request, use the $http.get method to submit your request.
  2. To make a POST request, use the $http.post method to submit your request.
  3. Apart from GET & POST, you can make other request types such as PATCH ($http.patch) as well.

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.

  • Sample GET script
var assert = require('assert');
var
sampleVariable = "Sample Text";
$http.get('https://sample_route/'+sampleVariable,
//Post data
{
headers: {
'Content-Type': 'application/json'
},
},
//Callback
function (err, response, body) {
var data = JSON.parse(body); assert.equal(response.statusCode, 200, 'Expected a 200 OK response');
assert.equal(data.sampleKey, sampleVariable, "Invalid Response");
});
  • Sample POST script
var assert = require('assert');
var
sampleVariable = "Sample Text";
$http.post('https://sample_route/',
//Post data
{
headers: {
'Content-Type': 'application/json'
},
body: { "key1": "value1",
"key2": "value2"
},
json: true
},
//Callback
function (err, response, body) {
var data = JSON.parse(body); assert.equal(response.statusCode, 201, 'Expected a 201 Created response');
assert.equal(data.sampleKey, sampleVariable, "Invalid Response");
});

Refer: https://docs.newrelic.com/docs/synthetics/table-of-contents/

Enjoy :)

--

--