Global variables can be utilized to store any kind of data that is needed as a reference resource within a complex API monitoring process.
For example, global variables can be created by one API monitor, then that data store can be read as input data by another API monitor. In my post Introduction to API Science’s Global Variables I illustrated how to create a monitor that sets global variables. The monitor accesses the API Science variables API using the POST method, and writes a set of variable names and values. Since global variables exist at the account level, these variables and their current values are accessible by any other monitor in the user’s account.
My post Retrieving and Verifying API Science Global Variables illustrates how an account’s global variables can be retrieved by a monitor, and provides a simple example of how a monitor can respond based on the retrieved values.
Deleting API Science Global Variables shows how an API Science monitor can delete global variables.
While these articles use monitors to manage and access an account’s global variables, global variables can also be created, read, updated, and deleted (CRUD) by directly accessing the API Science variables API using modern software platforms including cURL, Java, Python, etc.
Configuring API Response Format
Assume your product depends on a number of APIs, both external and internal, that can provide output in multiple formats (XML, JSON, etc.). If your internal software is optimized to process data in a given format, then ideally your API monitors would return their response data in that format.
The World Bank has an API whereby a call can request and receive basic information about a country. For example, to receive the summary information about Brazil, you can point a web browser to: http://api.worldbank.org/countries/br. If the API is up, entering this into your browser will produce an XML result similar to this:
This may be the information your product requires. But, what if your product is tuned for working with JSON data, not XML? The API: Country Queries page shows that the World Bank’s countries data can be returned in either XML or JSON format. The API: Basic Call Structure page shows that the response format can be selected by adding
jsonP) to the API call.
Given this, we can define the format in which we’d like to receive the response from the World Bank Countries API by appending a parameter to our call to the API, and have our API monitor validate a response delivered in that format.
In the case of the World Bank Countries API, we can create a monitor that explicitly requests JSON responses by defining a “format” parameter as being “json”:
When we run this monitor, a typical response body looks like this:
In this example, we’ve illustrated how to manually configure a monitor to query the World Bank Countries API, requesting that the data be returned in JSON format.
My next blog will illustrate how API Science global variables can be used to automatically configure monitors with respect to parameters that APIs accept.