Using Account Global Variables to Receive API Monitor Data in Specific Formats

My last post illustrated that calls to the World Bank Countries API can be configured to output the API data in multiple formats, including XML and JSON.

The API Science platform supports the creation of global variables that are accessible by all API monitors in a user’s account. Global variables are defined and set using a POST call to the API Science variables API (, as illustrated in my post Introduction to API Science’s Global Variables. For example, a monitor could be created that sets the global variable wb_format to have the value json:

With this global variable set, we can create a new multi-step monitor that retrieves the value of the variable, then creates a call to the World Bank Countries API that requests the API response to be returned in the specified format.

Step 1. Access the Response Format Global Variable

The first step in the multi-step monitor retrieves the value of the account’s wb_format variable:

The request asks the API Science variables API to return all variables for the user’s account. Here’s an example response body:

In this example, the account has assigned multiple global variables. But our current multi-step World Bank Countries API monitor requires only the wb_format variable. The Step 1 Validations code shown above verifies that the wb_format variable exists in the response body.

The JavaScript validation does the following:

  • var body = JSON.parse(context.response.body); assigns to JavaScript variable body the JSON response body shown above;
  • var fmt = "json"; identifies that this multi-step monitor expects to receive JSON-formatted data from the World Bank Countries API;
  • assert.equal(, fmt, "Invalid wb_format: " + body.wb_format); applies a JavaScript assertion that will make Step 1 of the monitor fail if the value of the wb_format variable is not equal to the fmt variable (i.e., json).

If Step 1 of the monitor succeeds (global variable wb_format is json), then Step 2 of the monitor will proceed.

Step 2: Call the API, Requesting the Response in the Defined Format

In Step 2 of the monitor, we extract the value of the wb_format variable from the Step 1 response, and use it to create our Step 2 API request.

Step 2 of our multi-step monitor looks like this:

Here, we define a local variable named format for this monitor, and we define it to be the value of resp_format that is defined in this step’s Pre-Process Script. The Pre-Process script accesses the response from Step 1, and assigns the value of that step’s wb_format variable to the local variable resp_format.

This value is assigned to the format parameter, which is sent as part of the request to the World Bank Countries API.

The World Bank Countries API receives this request, with the format variable set to json, and it returns its Step 2 response in JSON format, for example:


In this post, we’ve illustrated how API Science global variables can be used to configure API monitors. The World Bank Countries API provides outputs in various formats. In this example, we define a global variable that selects the requested output format from the World Bank API. The variable is read in Step one of the monitor, then a request to the World Bank API is constructed based on the global variable’s value.

This illustrates how the API Science platform can be utilized to flexibly configure large numbers of monitors that are critical for your company’s product.

–Kevin Farnham