API monitor- Script not working

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);
});

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');
  }
);
1 Like