How to Use Global Variables in Postman for API Testing
In Postman, you use
pm.globals.set('variableName', 'value') to create or update a global variable and pm.globals.get('variableName') to access its value. Global variables are accessible across all requests and collections within your Postman workspace.Syntax
Postman provides simple commands to work with global variables:
pm.globals.set('variableName', 'value'): Creates or updates a global variable.pm.globals.get('variableName'): Retrieves the value of a global variable.pm.globals.unset('variableName'): Deletes a global variable.pm.globals.clear(): Removes all global variables.
Use these commands inside the Pre-request Script or Tests tabs of your requests.
javascript
pm.globals.set('token', '12345abcde'); const tokenValue = pm.globals.get('token'); console.log(tokenValue);
Output
12345abcde
Example
This example shows how to save a token from a login response as a global variable and then use it in another request's header.
javascript
// In the Tests tab of the login request const jsonData = pm.response.json(); pm.globals.set('authToken', jsonData.token); // In the Headers tab of another request // Key: Authorization // Value: Bearer {{authToken}}
Output
If login response contains {"token": "abc123"}, the global variable 'authToken' is set to 'abc123'. Subsequent requests use it in the Authorization header.
Common Pitfalls
- Forgetting to set the variable: If you try to use a global variable that was never set, it will be
undefined. - Scope confusion: Global variables override environment variables with the same name, which can cause unexpected values.
- Not clearing old variables: Old global variables persist until manually cleared, which can cause stale data issues.
Always check if the variable exists before using it and clear unused globals regularly.
javascript
/* Wrong way: Using a global variable without setting it first */ const token = pm.globals.get('missingToken'); console.log(token); // undefined /* Right way: Check before use */ if(pm.globals.has('authToken')) { const token = pm.globals.get('authToken'); console.log(token); } else { console.log('authToken is not set'); }
Output
undefined
authToken is not set
Quick Reference
| Command | Description | Example |
|---|---|---|
| pm.globals.set('key', 'value') | Create or update a global variable | pm.globals.set('userId', '123') |
| pm.globals.get('key') | Get the value of a global variable | const id = pm.globals.get('userId') |
| pm.globals.unset('key') | Delete a global variable | pm.globals.unset('userId') |
| pm.globals.clear() | Remove all global variables | pm.globals.clear() |
Key Takeaways
Use pm.globals.set() to create or update global variables accessible across all requests.
Access global variables with pm.globals.get() inside scripts or use {{variableName}} in request fields.
Always verify a global variable exists before using it to avoid undefined errors.
Clear unused global variables regularly to prevent stale data issues.
Global variables override environment variables with the same name, so manage scopes carefully.