Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

API monitor- Script not working

synthetics-script
api-test
api-script
scripting

#1

Hi,

I want to set a API script alert to test if the API is working fine. i wrote the following code to but when i bring the API down it is not sending any alert. Could you please tell me what am i doing wrong. its more than 2 API url that i need to monitor.

const request = require(‘request’);

request(‘https:API_URL_1’, { json: true }, (err, res, body) => {
if (err) throw new Error(err);
console.log(res.body);
});

request(‘https:API_URL_2’, { json: true }, (err, res, body) => {
if (err) throw new Error(err);
console.log(res.body);
});


#2

Hi, @Madhu_Sharma1: I don’t think throwing an error will cause a script to fail. You must use an assert statement, as shown in the sample script provided when you create an API test:

var assert = require('assert');

$http.post('http://httpbin.org/post',
  // Post data
  {
    json: {
      widgetType: 'gear',
      widgetCount: 10
    }
  },
  // Callback
  function (err, response, body) {
    assert.equal(response.statusCode, 200, 'Expected a 200 OK response');

    console.log('Response:', body.json);
    assert.equal(body.json.widgetType, 'gear', 'Expected a gear widget type');
    assert.equal(body.json.widgetCount, 10, 'Expected 10 widgets');
  }
);