Service chaining with Postman

Lakshani Gamage
2 min readMar 2, 2021

--

When we are calling an APIs with an access token, first we have to get the access token by calling the token endpoint. Then we have to copy the access token from the token response and set it as a header variable of the API. To prevent that manual process we are going to automate this.

Now let’s see how we can do it.

  1. Create an Environment

Click on “Environment options” and select “Manage Environments”.

Then add the environment. Here, I added Test as the environment name. I add “token” as a key.

2. Get response body and set to the environment variable

Select the previously created test environment.

The /token API returns a JSON body with an access_token.

To extract the access_token, we need the following code.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.access_token);

Add the above code to “Tests” tab and hit send button.

If you check the variable “token” which we made on creating the environment has extracted the value from the response.

3. Use token in another API

We can use the token in other APIs using {{token}}

--

--

Lakshani Gamage
Lakshani Gamage

Written by Lakshani Gamage

I am Lakshani Gamage, a post graduate from the University of Colombo, School of Computer. I’m working as a Senior Software Engineer at Mitra Innovation.

No responses yet