Validating API Responses Using JavaScript

Determining the present state of an API that’s critical to your product’s performance is the reason why companies apply API monitoring on a 24/7 basis. If your product depends on a sequence of calls to multiple APIs (perhaps internal as well as external), your product could appear “down” to your customers if even one of those critical APIs is down.

But how can you know for sure if one of your critical APIs is down? It’s certainly down if your call returns an invalid HTTP return code. If the API returns JSON, it’s down if the response body is not valid JSON. If the received response body doesn’t include a mandatory REGEX text sequence, then the API is down.

The API Science Validations documentation points to these means for validating your API checks. But in addition, the API Science platform supports validation of API responses using JavaScript:

Execute your own custom JavaScript for advanced testing of call results. By default API Science embeds the Chai JavaScript Library for running test assertions. Any assertion failure will flag this validation as failed and in turn will cause any user-defined alert rules to be triggered.

Using JavaScript lets you program as detailed an investigation as you need into an API’s response content to be assured that the API really is presently “up” with respect to how your product uses it.

In this blog series, I’ll introduce some of the ways you can use JavaScript to validate responses from a simple API monitor that calls a single API. First, we’ll simply create a new API monitor and look into the possible ways JavaScript might be used to validate the API’s responses.

xkcd.com is “a WebComic of romance, sarcasm, math, and language.” They also have an API. I’ll use this for my initial JavaScript demo.

To initiate this investigation, I went to my API Science Dashboard, and clicked on the “Add Monitor” button in the upper right of that page. Then, I created a monitor that calls the xkcd.com API:

xkcd_create_monitor

When this API call runs successfully, it returns a JSON response body similar to this:

{
  "month": "2",
  "num": 1481,
  "link": "",
  "year": "2015",
  "news": "",
  "safe_title": "API",
  "transcript": "((This is a faux-screenshot of a technical document))\n[[A figure sits at a computer upon a desk, apparently engrossed in the document which we now see before us.]]\nTITLE: API GUIDE\nRequest URL Format: domain\nuser\nitem\nServer will return an XML document which contains the requested data and documentation describing how the data is organized spatially.\nAPI KEYS: To obtain API access, contact the x.509-authenticated server and request an ECDH-RSA TLS key...\n\nCaption: If you do things right, it can take people a while to realize that your \"API documentation\" is just instructions for how to look at your website.\n\n{{Title text: ACCESS LIMITS: Clients may maintain connections to the server for no more than 86,400 seconds per day. If you need additional time, you may contact IERS to file a request for up to one additional second.}}",
  "alt": "ACCESS LIMITS: Clients may maintain connections to the server for no more than 86,400 seconds per day. If you need additional time, you may contact IERS to file a request for up to one additional second.",
  "img": "http://imgs.xkcd.com/comics/api.png",
  "title": "API",
  "day": "2"
}

The API Science JavaScript validation feature can be applied to investigate this JSON response to determine if a valid response was returned. To implement JavaScript validation testing, I edit my XKCD monitor and click the “Show Settings” link:

xkcd-editClick the “Show Settings” link and select “JavaScript” in the “Validations” column:

xkcd-edit-javascriptNow we’re ready to enter JavaScript code that will validate the API’s response text.

I’ll talk about that in my next post.

–Kevin Farnham

One thought on “Validating API Responses Using JavaScript

Comments are closed.