Postman Environments and Variables
Postman environments, variable scopes, environment variables, global variables, collection variables, dynamic variables, setting variables in scripts, initial value vs current value
Postman Environments and Variables
Postman environments and variables are what transform a set of static API requests into a dynamic, reusable test suite. Instead of manually changing URLs and credentials every time you switch from development to staging to production, environments let you do it with a single dropdown selection. This is one of the most time-saving features in Postman API testing.
Types of Variables in Postman
Postman has five variable scopes, each with different visibility and precedence:
| Scope | Where Defined | Visibility | Priority |
|---|---|---|---|
| Global | Globals menu | All collections, all environments | 4th (lowest) |
| Collection | Collection Variables tab | All requests in collection | 3rd |
| Environment | Environment editor | All collections when env active | 2nd |
| Data | CSV/JSON data file | During Collection Runner with data file | 1st |
| Local | Scripts (pm.variables.set) | Current request only | Highest |
Creating an Environment
- Click the Environments icon in the left sidebar (or the gear icon in older versions).
- Click Add or the + icon.
- Name it "Development".
- Add variables with initial and current values:
base_url = http://localhost:3000/api/v1
auth_token = (leave empty โ will be set by login script)
user_id = 1
Duplicate this environment and name it "Staging" and "Production", changing only the base_url. Now switching environments changes all URLs instantly.
Using Variables in Requests
Reference variables using double curly braces anywhere in your request:
URL: {{base_url}}/users/{{user_id}}
Header: Authorization: Bearer {{auth_token}}
Body: { "email": "{{test_email}}", "name": "{{test_name}}" }
Postman highlights unresolved variables in red and resolved ones in orange โ letting you quickly spot missing values.
Setting Variables Dynamically from Scripts
The most powerful use of variables is setting them programmatically from test scripts. For example, after a login request returns an access token, automatically save it for all subsequent requests:
// In the Tests tab of your Login request:
const response = pm.response.json();
pm.environment.set("auth_token", response.data.token);
pm.environment.set("user_id", response.data.user.id);
Now every subsequent request that uses {{auth_token}} will automatically have the correct token โ no manual copying needed.
Initial Value vs Current Value
Each variable has two values: the Initial Value (synced to the team/cloud) and the Current Value (local to your machine only). Always put secrets like API keys and tokens in the Current Value only โ they will not be accidentally shared with teammates or exposed in version control.
Postman Dynamic Variables
Postman provides built-in dynamic variables that generate random data automatically:
- {{$randomEmail}}: Generates a random email address.
- {{$randomInt}}: Generates a random integer.
- {{$randomUUID}}: Generates a random UUID.
- {{$timestamp}}: Current Unix timestamp.
- {{$isoTimestamp}}: Current ISO 8601 timestamp.
Key Takeaways
- Create separate environments for Development, Staging, and Production with different base_url values.
- Use {{variable_name}} syntax everywhere โ URLs, headers, body, and scripts.
- Set variables dynamically in Tests scripts to chain requests (e.g., save login token for subsequent requests).
- Never put secrets in Initial Value โ use Current Value for credentials that should not be shared.
