Postman Environment and Global Variables
Environment Variables
Scoped to a specific Environment (e.g., "QA" or "Production"), these variables let the same request adapt automatically depending on which environment is currently active.
Global Variables
Available across all Collections and Environments in a Postman workspace, useful for values that should stay constant no matter which environment is selected.
Using Variables in Requests
Variables are referenced with double curly braces, e.g. {{base_url}}/users/{{user_id}}, keeping requests clean, reusable, and easy to maintain.
Variable Scopes and Which Value Wins
Postman supports several scopes, and when the same name exists in more than one, the narrowest scope is used. From broadest to narrowest the order is: global, collection, environment, data (from a data file in a run), and local (set inside a script for one request). So if base_url exists as a global and in the active environment, the environment value is used.
| Scope | Visible to | Typical use |
|---|---|---|
| Global | Every request in the workspace | Shared helper values, rarely secrets |
| Collection | Requests inside one collection | API version, common paths |
| Environment | Requests while that environment is selected | Base URLs, test accounts per QA, staging, production |
| Data | One iteration of a Collection Runner or Newman run | Rows of test data |
| Local | The current request execution only | Temporary values in scripts |
Each variable also has an initial value, which is shared when you sync or export, and a current value, which stays on your machine. Put real tokens and passwords only in the current value, or use Postman's secret type and vault features, so they are not shared with your team by accident.
Worked Example: Reusing a Login Token
Create two environments, QA and Staging, each with base_url, username and password. Then add a login request with this script in its post-response (Tests) tab:
const body = pm.response.json();
pm.test("Login succeeded", () => {
pm.response.to.have.status(200);
});
pm.environment.set("auth_token", body.token);
Every other request in the collection sets its Authorization header to Bearer {{auth_token}}. Switch the environment dropdown from QA to Staging, run the login again, and all requests now talk to the other server with a fresh token, without editing a single URL.
Other useful script methods include pm.globals.set(), pm.collectionVariables.set(), pm.variables.get() (which reads the value that wins by precedence) and pm.environment.unset() for cleanup.
Mistakes Testers Commonly Make
- Running destructive tests while the production environment is still selected. Name environments clearly and check the dropdown before sending.
- Storing the same variable in global and environment scope, then wondering why a change has no effect.
- Saving secrets in the initial value, which gets shared with the workspace.
- Hard-coding a URL in one request, so it ignores the environment switch.
Quick Setup Checklist
- One environment per server, with identical variable names in each.
- Globals kept to a minimum.
- Tokens set by script, not pasted manually.
- Unresolved variables show in red in the URL bar; fix them before running a suite.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.