Skip to content

Chain requests with variables

A common pattern is to log in, capture a token from the response, and use it on the next request. You do this with a post-response script that writes an environment variable, then a {{variable}} placeholder on the following request.

On the login request, add a post-response script that reads the response and stores what you need:

const body = fp.response.json();
fp.environment.set('token', body.accessToken);
fp.environment.set('userId', body.user.id);

fp.environment.set persists the value after the send, so later requests see it.

Reference the captured value with a placeholder anywhere interpolation applies — URL, headers, body:

GET {{baseUrl}}/api/users/{{userId}}
Authorization: Bearer {{token}}

If you only need a value within a single send (for example, to pass data from the pre-request to the post-response script of the same request), use the ephemeral scratch space instead of polluting the environment:

// pre-request
fp.variables.set('startedAt', Date.now());
// post-response
const elapsed = Date.now() - fp.variables.get('startedAt');
fp.log(`round trip: ${elapsed} ms`);

fp.variables.* is discarded when the send completes. See Variables and Variable precedence for how names resolve.