What Are Postman Environments?
A Postman Environment is a named set of key-value variables — like base_url or api_key — that you can switch between using the environment dropdown in the top-right corner, so the same collection of requests can target dev.example.com in one click and prod.example.com in the next without editing a single request. Each environment is isolated, so a token stored in 'Staging' never leaks into a request run under 'Production'.
Cricket analogy: Switching a Postman environment is like a team choosing between a red-ball Test squad and a white-ball T20 squad before a series — same core players and processes, but different conditions, so you pick the right squad sheet rather than rewriting the whole team from scratch.
Creating and Referencing Environment Variables
You create an environment from the Environments panel, add variables with initial and current values, then reference them anywhere in a request — URL, headers, body, even scripts — using double curly braces like {{base_url}}/users. Postman resolves the placeholder at send time using whichever environment is currently active, which is what lets one request definition work across every stage of your pipeline.
Cricket analogy: Referencing {{base_url}} in a request is like a scorer writing 'today's venue' on a scoresheet instead of hardcoding 'Eden Gardens' — the same scoresheet template works at every ground the team plays.
// Tests tab of the "Login" request
const response = pm.response.json();
pm.environment.set("auth_token", response.token);
// Any subsequent request's URL and Authorization header
// GET {{base_url}}/api/v1/orders?status=pending
// Authorization: Bearer {{auth_token}}Variable Scope and Resolution Order
Postman resolves variables in a strict priority order — local, then data, then environment, then collection, then global — so if the same variable name exists at two scopes, the narrower scope wins; an environment-level api_key overrides a global one with the same name. Understanding this order matters when debugging why a request is hitting the wrong host despite the environment looking correct.
Cricket analogy: Variable scope priority is like DRS overruling the on-field umpire's call, which itself overruled the fielder's appeal — the narrowest, most specific authority, the replay, wins over broader defaults.
Always mark secrets like API keys and passwords as type 'secret', not 'default', when creating an environment variable. Secret values are masked in the UI and excluded when a collection is shared or synced, while default-type values are stored and displayed in plain text.
Dynamic and Scripted Variables
Beyond static values, you can set environment variables at runtime from a script using pm.environment.set('token', response.token) inside the Tests tab of a login request, so every subsequent request in the collection automatically picks up a freshly issued auth token without manual copy-pasting.
Cricket analogy: Setting a token via script after login is like a fielding captain automatically updating the field placement chart the instant a bowling change happens, rather than manually redrawing it every over.
pm.environment.get() reads only from the active environment, while pm.variables.get() reads whichever scope resolves first across local, environment, collection, and global — reach for pm.variables.get() in scripts when you don't want to hardcode which scope a value should come from.
- An Environment is a named set of key-value variables you can switch between with one click.
- Variables are referenced anywhere in a request using {{variableName}} syntax.
- Postman resolves variables in priority order: local > data > environment > collection > global.
- Secret-type variables are masked in the UI and excluded from shares/syncs; default-type values are stored in plain text.
- Scripts can set environment variables at runtime, e.g. capturing an auth token from a login response.
- pm.environment.get() reads only the active environment; pm.variables.get() checks the full resolution order.
- Switching environments lets one collection safely target dev, staging, and production without editing requests.
Practice what you learned
1. How do you reference an environment variable named base_url inside a request URL?
2. What is the correct variable resolution priority in Postman, from highest to lowest?
3. Why should an API key be stored as a 'secret' type variable rather than 'default'?
4. What does pm.environment.set('auth_token', response.token) do inside a Tests script?
5. What is the key difference between pm.environment.get() and pm.variables.get()?
Was this page helpful?
You May Also Like
Collection and Global Variables
Learn the difference between collection variables, scoped to one collection, and global variables, available everywhere, and when to use each.
Collections Basics
Learn how Postman Collections group related API requests into a single organized, shareable, and runnable unit.
Importing and Exporting Collections
Learn how to move Postman Collections between teammates, tools, and environments using export files, Postman links, and API-spec imports.