
Postman Variables and Environments
Master Postman variables and environments for flexible API testing. Learn to manage different configurations and eliminate hardcoded values.
Postman Variables and Environments
Hardcoding URLs, tokens, and IDs into every request is tedious and error-prone. Postman's variables and environments solve this by letting you define values once and reuse them everywhere.
1. Variable Types in Postman
Postman has five levels of variables, from broadest to most specific:
graph TD
A["Global Variables<br/>Available everywhere"] --> B["Collection Variables<br/>Available in one collection"]
B --> C["Environment Variables<br/>Available for selected environment"]
C --> D["Data Variables<br/>From CSV/JSON files"]
D --> E["Local Variables<br/>Set in scripts, one request only"]
style A fill:#4f46e5,color:#fff
style B fill:#0891b2,color:#fff
style C fill:#059669,color:#fff
style D fill:#d97706,color:#fff
style E fill:#6b7280,color:#fff
Variable Scope Priority
If the same variable name exists at multiple levels, the most specific one wins:
Local > Data > Environment > Collection > Global
2. Collection Variables
Collection variables are shared across all requests in a collection.
Setting Collection Variables
- Click your collection name
- Go to the Variables tab
- Add variables:
| Variable | Initial Value | Current Value |
|---|---|---|
baseUrl | https://jsonplaceholder.typicode.com | https://jsonplaceholder.typicode.com |
testUserId | 1 | 1 |
testPostId | 1 | 1 |
Using Collection Variables
Replace hardcoded values with {{variableName}}:
GET {{baseUrl}}/users/{{testUserId}}
POST {{baseUrl}}/posts
GET {{baseUrl}}/posts/{{testPostId}}/comments
Now if the base URL changes, you update it in one place.
3. Environment Variables
Environments let you switch configurations without changing requests.
Creating Environments
Development Environment:
| Variable | Value |
|---|---|
baseUrl | http://localhost:3000/api |
apiKey | dev-key-abc123 |
debug | true |
Staging Environment:
| Variable | Value |
|---|---|
baseUrl | https://staging-api.example.com |
apiKey | staging-key-def456 |
debug | true |
Production Environment:
| Variable | Value |
|---|---|
baseUrl | https://api.example.com |
apiKey | prod-key-ghi789 |
debug | false |
Switching Environments
Click the environment dropdown in the top-right corner to switch. All your requests instantly use the new environment's values.
graph LR
A["Same Request:<br/>GET {{baseUrl}}/users"] --> B{Environment?}
B -->|Dev| C["http://localhost:3000/api/users"]
B -->|Staging| D["https://staging-api.example.com/users"]
B -->|Production| E["https://api.example.com/users"]
style A fill:#4f46e5,color:#fff
style C fill:#059669,color:#fff
style D fill:#d97706,color:#fff
style E fill:#dc2626,color:#fff
4. Dynamic Variables
Postman has built-in dynamic variables that generate values automatically:
| Variable | Generates | Example |
|---|---|---|
{{$guid}} | UUID | a1b2c3d4-... |
{{$timestamp}} | Unix timestamp | 1708640000 |
{{$isoTimestamp}} | ISO date | 2026-02-22T18:30:00.000Z |
{{$randomInt}} | Random integer | 47 |
{{$randomEmail}} | Random email | user@example.com |
{{$randomFirstName}} | Random name | Sarah |
Use them in request bodies:
{
"name": "{{$randomFirstName}}",
"email": "{{$randomEmail}}",
"requestId": "{{$guid}}"
}
5. Setting Variables from Scripts
The most powerful use: extract values from responses and store them for later requests.
Extract Token from Login Response
Pre-request Script (or Test script on Login request):
// After logging in, save the token
pm.test("Save auth token", function () {
var jsonData = pm.response.json();
pm.environment.set("authToken", jsonData.token);
});
Now use {{authToken}} in the Authorization header of subsequent requests.
Extract Created Resource ID
pm.test("Save created post ID", function () {
var jsonData = pm.response.json();
pm.collectionVariables.set("newPostId", jsonData.id);
});
Use {{newPostId}} in the next request to read or update that post.
Complete Login Flow with Variables
1. POST {{baseUrl}}/auth/login
Body: {"email": "{{userEmail}}", "password": "{{userPassword}}"}
Test: pm.environment.set("authToken", pm.response.json().token)
2. GET {{baseUrl}}/users/me
Header: Authorization: Bearer {{authToken}}
(Token from step 1 is used automatically!)
3. POST {{baseUrl}}/posts
Header: Authorization: Bearer {{authToken}}
Body: {"title": "New Post"}
Test: pm.collectionVariables.set("newPostId", pm.response.json().id)
4. GET {{baseUrl}}/posts/{{newPostId}}
(ID from step 3 is used automatically!)
6. Variable Best Practices
| Practice | Description |
|---|---|
| Never hardcode base URLs | Use {{baseUrl}} |
| Store tokens in environment variables | Easy to refresh |
| Use collection variables for test data | IDs, test user info |
| Use dynamic variables for unique data | Prevents conflicts |
| Set sensitive values as "secret" type | Hidden in Postman UI |
| Document required variables | In collection description |
Summary and Key Takeaways
- Variables eliminate hardcoded values across requests.
- Collection variables are shared within a collection; environment variables switch between configs.
- Variable priority: Local > Data > Environment > Collection > Global.
- Dynamic variables like
{{$guid}}generate unique values automatically. - Scripts can extract values from responses and set them as variables for chaining requests.
- Environments enable testing against Dev, Staging, and Production with zero request changes.
Lesson Review Quiz
?Knowledge Check
If the same variable 'baseUrl' exists in both collection and environment variables, which value is used?
?Knowledge Check
How do you reference a variable named 'apiKey' in a Postman request?
?Knowledge Check
What is the best way to pass a login token from one request to subsequent requests?