How to Pass Data Between Requests in Postman Easily
In Postman, you can pass data between requests by saving values to
environment or collection variables using scripts in the Tests tab, then accessing those variables in subsequent requests with {{variableName}} syntax. This lets you share data like tokens or IDs across multiple requests automatically.Syntax
To pass data between requests, use Postman's scripting feature to save data into variables and then reference those variables in later requests.
- Set variable in Tests tab:
pm.environment.set('variableName', value); - Get variable in request URL, headers, or body:
{{variableName}} - Use variables: Environment variables, Collection variables, or Global variables.
javascript
pm.environment.set('authToken', pm.response.json().token); // Later in another request, use {{authToken}} in headers or URL
Example
This example shows how to extract a token from a login response and use it in the Authorization header of the next request.
javascript
// In Login request Tests tab const jsonData = pm.response.json(); pm.environment.set('authToken', jsonData.token); // In next request Headers // Authorization: Bearer {{authToken}}
Output
If login returns {"token": "abc123"}, the next request will send header Authorization: Bearer abc123
Common Pitfalls
- Not setting variables in the correct scope (environment vs global) can cause missing data.
- Trying to use variables before they are set leads to empty or undefined values.
- Forgetting to save the environment after setting variables means changes won't persist.
- Using incorrect variable syntax like missing curly braces
{{}}will not substitute values.
javascript
// Wrong: Using variable without setting it // Header: Authorization: Bearer {{token}} // Right: Set variable first pm.environment.set('token', pm.response.json().token);
Quick Reference
| Action | Syntax/Example |
|---|---|
| Set environment variable | pm.environment.set('varName', value); |
| Get environment variable | {{varName}} |
| Set collection variable | pm.collectionVariables.set('varName', value); |
| Get collection variable | {{varName}} |
| Set global variable | pm.globals.set('varName', value); |
| Get global variable | {{varName}} |
Key Takeaways
Use pm.environment.set() in Tests to save data from one request.
Access saved data in later requests with {{variableName}} syntax.
Always set variables before using them to avoid empty values.
Choose the right variable scope: environment, collection, or global.
Remember to save your environment to keep variable changes.