Chaining API calls in postman
In one of my projects, I had to develop some API calls. I used Symfony as the backend for developing APIs and Postman for testing. I struggled by pasting the response to the request of next API call and it was time consuming also. After some sort of research I found out that Postman has the feature of chaining API calls. Here, I will explain about how one can chain API calls and test them hassle free.
Let’s create a collection first. A collection in Postman are a group of saved requests. and these requests scan be arrange to folders.
A collection will look like this.

Here most of the requests should be authenticated with an access token. The login API would get the token and developers will copy paste these token in most cases.
The response of the login API is something like this
{
"data": {
"token_type": "Bearer",
"expires_in": 28800,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJmYWFhOTkxNGJmNGVlYWU1NWZhMjU4MzQwYTczYjJiOSIsImp0aSI6ImFhMWVmYjFjOGZ...",
"refresh_token": "def5020012fbe01444c8991cb0395ce6afaec94f2093f43bec1b95e7147ae49a809b41f3afac80c769b58cbfbaffcd5b54fd1a8dbb4f980ac9828a98.."
},
"error": false,
"message": "Successfully logged in"
}
Now under the tests
tab, add the following code
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("env_access_token", jsonData.data.access_token);
The code itself explains well. First it fetches the response body and parse it, then assigns value of access_token environment variable env_access_token
.

Now when you look at the environment variables sections, you can see a new data with key env_access_token
and value in it. Next time when you want to create an API request that should be authenticated you can simply pass the environment variable {{env_access_token
}} which will get the latest access token from login request.
Likewise you can chain many requests.