Use result of API call as input to another API

Postman newcomer here.

I am doing a test in which the result of one REST API call(a security token) must be used as a security parameter to a 2nd REST API call.

Is is a pain to manually make API call #1, copy the resulting token, paste that token into the security parms of REST call #2, then call #2.

Can this be automated or scripted??

Can you point me in the right direction or give hints?

1 Like

If you want to be part of the hip new movement, use a collection variable! If a ‘token’ is returned in the response of Request #1, and you need to use that in Request #2, then something like this will do the trick:

In the ‘test’ script for Request #1, get the value of the response body object with something like…

const responseJson = pm.response.json();
const token = responseJson.token

pm.collectionVariables.set('token', token); 

Then, assuming you are referencing this token in the request url/request headers, just reference it there using {{token}} (which will read the collection variable value as long as you don’t have a runtime/env variable by this same name).

Or do the same thing in one line :wink:

pm.collectionVariables.set('token', pm.response.json().token)

I agree, but I like to break that up so it’s easier to follow since this is tagged as “just getting started”

1 Like