How to Use the Results From One API Call in Another API Call

In many cases, the availability of your product depends on a sequence of API calls (to both external and internal APIs). Information retrieved from one API may be a critical input for your subsequent call to a different API. If the first call fails, the second can’t return a valid result.

So, a question is: can you create an API monitor that will test this complexity? Can you create a monitor that uses information contained in the response from one API call to create a new request that you send to either the same or a different API?

The API Science platform supports multi-step monitors that allow you to do this. The Multi-Step Example monitor is a six-step monitor that queries the API. The monitor’s first step is to request an authentication token, which is required for subsequent access to the Wordnik API. Here’s an example Step 1 request:

Here, we are sending a request for a Wordnik authentication token, which we’ll use in our Step 2 request. Note: the “FILTERED” fields in the image represent private information that should not be made public (our API key and password). In creating your own Wordnik request, you’d substitute your Wordnik API key and password.

An example response to this request is:

Here, the response includes a token variable (the variable’s value is overwritten in the image) that allows us to submit additional requests to the API for more specific data.

Now, in the next step for this monitor, we can use this value that was received from the first step. To do this, in Step 2 we create a JavaScript variable named auth_token that is assigned the value of the token element that was returned from the first step:

The context element is a container for information that can be utilized in the Step 2 API call. But, after the Step 1 API call has completed, its response body remains available for interrogation by a JavaScript preprocess script that prepares the Step 2 API call.

Here, in the Step 2 Script:

    context.auth_token = JSON.parse(context.response.body).token

the Step 1 response body is parsed to extract the JSON token value. This value is stored as context.auth_token, then used as a parameter in the Step 2 GET request. Then, for the Parameter field for auth_token, you can use this syntax in the Value side {{context.auth_token}}. This permits Step 2 in this multi-step monitor to succeed:

In this example, we have gathered information from the JSON response to an API request, and integrated that data into a subsequent API request.

This illustrates the potential the API Science platform offers: if a chain of different API calls is required for your customers to consider your application to be up, you can configure your API Science monitors to ensure that your team will be aware of issues when and where they occur at any point in your API processing sequence.

–Kevin Farnham