0
0
PostmanHow-ToBeginner ยท 4 min read

How to Chain Requests in Postman: Step-by-Step Guide

To chain requests in Postman, use pm.environment.set() or pm.variables.set() in the Tests tab of one request to save data, then access it in the next request using {{variableName}} syntax. This lets you pass data like tokens or IDs between requests automatically.
๐Ÿ“

Syntax

Chaining requests in Postman involves saving data from one request and using it in another. Use the following syntax in the Tests tab of the first request:

  • pm.environment.set('variableName', value); - saves a value to an environment variable.
  • pm.variables.set('variableName', value); - saves a value to a local variable for the current collection run.

Then, in the next request, use {{variableName}} in the URL, headers, or body to use the saved value.

javascript
pm.environment.set('authToken', pm.response.json().token);
pm.variables.set('userId', pm.response.json().id);
๐Ÿ’ป

Example

This example shows how to chain two requests: first, login to get a token, then use that token in the next request's header.

javascript
// First request: Login
// In Tests tab
const jsonData = pm.response.json();
pm.environment.set('authToken', jsonData.token);

// Second request: Get user data
// In Headers tab
Authorization: Bearer {{authToken}}
Output
First request passes if login is successful and token is saved; second request uses token and returns user data.
โš ๏ธ

Common Pitfalls

Common mistakes when chaining requests include:

  • Not saving the variable correctly in the Tests tab.
  • Using the wrong variable syntax in the next request (forgetting the double curly braces {{}}).
  • Using environment variables without selecting the correct environment in Postman.
  • Trying to access variables before they are set (order of requests matters).

Always check the Postman console for errors and confirm variables are set using the Environment Quick Look.

javascript
/* Wrong way: missing pm.environment.set */
// pm.response.json().token; // This does nothing

/* Right way: */
pm.environment.set('authToken', pm.response.json().token);
๐Ÿ“Š

Quick Reference

ActionSyntaxDescription
Save variable in Tests tabpm.environment.set('varName', value);Stores value in environment variable
Use variable in request{{varName}}References saved variable in URL, headers, or body
Check variablesEnvironment Quick Look (eye icon)View current environment variables
Clear variablepm.environment.unset('varName');Removes variable from environment
โœ…

Key Takeaways

Use pm.environment.set() in the Tests tab to save data from a response.
Reference saved variables in subsequent requests with {{variableName}} syntax.
Always select the correct environment to ensure variables are accessible.
Check the Postman console and Environment Quick Look to debug variable issues.
Order your requests properly so variables are set before use.