Environments & Variables
The Problem Without Environments
Imagine you're testing an API that runs locally at http://localhost:3000 during development, at https://staging.api.example.com in staging, and https://api.example.com in production.
Without environments, you'd manually change the base URL in every request whenever you switch. With 20 requests in a collection, that's 20 edits every time.
Environments solve this by letting you define variables once and switch the entire context with a click.
What are Environments?
An environment is a named set of key-value pairs (variables). You create one environment per context (development, staging, production), define the same variable names in each, and switch between them from the top-right dropdown.
Environment: Development
BASE_URL = http://localhost:3000
API_KEY = dev_key_123
Environment: Production
BASE_URL = https://api.example.com
API_KEY = prod_key_abc
In your requests, you use {{BASE_URL}} instead of a hardcoded URL — Postman substitutes the value from the active environment.
Creating an Environment
- Click the Environments tab in the left sidebar (or the grid icon)
- Click + to create a new environment
- Name it (e.g.,
Development) - Add your variables:
| Variable | Initial Value | Current Value |
|---|---|---|
| BASE_URL | http://localhost:3000 | http://localhost:3000 |
| API_KEY | your_actual_key |
- Click Save
- Initial Value is shared with your team when you export/share the environment. Don't put secrets here.
- Current Value is only on your machine, never synced. Put sensitive values (API keys, passwords) here.
Using Variables in Requests
Reference any variable with double curly braces: {{VARIABLE_NAME}}
URL: {{BASE_URL}}/api/users
In the Headers tab:
Authorization: Bearer {{TOKEN}}
Content-Type: application/json
In the Body (raw JSON):
{
"api_key": "{{API_KEY}}",
"userId": "{{USER_ID}}"
}
Switching Environments
In the top-right corner of Postman, you'll see a dropdown showing the active environment (or "No Environment"). Click it to switch. Your entire collection instantly uses the new environment's values.
Variable Scope
Postman has four levels of variables, from narrowest to widest scope:
| Scope | Where set | Use for |
|---|---|---|
| Local | Script only | Temporary values within a single request |
| Data | Collection runner CSV/JSON | Test data in automated runs |
| Collection | Collection settings | Values shared across the whole collection |
| Environment | Environment settings | Values that change per environment |
| Global | Postman globals | Values shared across all collections |
When the same variable name exists at multiple scopes, the narrower scope wins.
Setting Variables with Scripts
You'll often want to capture a value from a response and use it in later requests — like capturing a JWT token after login.
In the Tests tab of your login request:
const response = pm.response.json();
if (pm.response.code === 200) {
pm.environment.set("TOKEN", response.token);
pm.environment.set("USER_ID", response.user.id);
}
Now every subsequent request that uses {{TOKEN}} in its Authorization header will automatically use the token from the login response.
A Complete Environment Setup Example
Here's a realistic setup for a REST API:
Environment: Development
| Variable | Current Value |
|---|---|
BASE_URL | http://localhost:3000 |
TOKEN | (set by login script) |
USER_ID | (set by login script) |
ADMIN_EMAIL | admin@test.com |
ADMIN_PASSWORD | test-pass-123 |
Login Request (POST {{BASE_URL}}/auth/login):
Body:
{
"email": "{{ADMIN_EMAIL}}",
"password": "{{ADMIN_PASSWORD}}"
}
Tests script (captures the token):
pm.environment.set("TOKEN", pm.response.json().token);
Protected Request (GET {{BASE_URL}}/api/profile):
Header:
Authorization: Bearer {{TOKEN}}
Now your entire workflow is environment-driven: run the login request once, and all subsequent requests automatically use the captured token.
Exporting and Sharing Environments
To share an environment with your team:
- Click the ... menu next to the environment
- Click Export
- Save the JSON file and share it (via Git, Slack, etc.)
Your teammates import it (File → Import) and fill in their own Current Values for secrets.
Export your environment files (without sensitive current values) and commit them to your repository. This way new team members can quickly import the correct environment setup.