Postman has five types of variables. They form a hierarchy. When two variables have the same name, the narrower scope wins. Think of it like a company: if your team lead says "meeting at 3 PM" and the CEO says "meeting at 2 PM," the CEO wins. In Postman, the most specific variable wins.
Data variables are the most specific — they override everything. Global variables are the broadest — they get overridden by everything. If you have {{baseUrl}} set as a global, collection, AND environment variable, the environment value wins (unless a data file also has baseUrl, then data wins).
| Scope | Set By | Lifetime | Use Case |
|---|---|---|---|
| Global | UI or pm.globals.set() | Until manually deleted | Rarely used — shared API keys across all collections |
| Collection | UI or pm.collectionVariables.set() | Lives inside the collection file | Default values — baseUrl, apiVersion, common headers |
| Environment | UI or pm.environment.set() | Tied to the active environment | Server-specific — dev URL, staging token, prod credentials |
| Local | pm.variables.set() in scripts | Dies after the request completes | Temporary calculations — timestamps, computed hashes |
| Data | CSV/JSON file in Collection Runner | Dies after the iteration completes | Test data — usernames, emails, expected results |
// GLOBAL — available everywhere, across all collections
pm.globals.set("apiKey", "global-key-123");
const key = pm.globals.get("apiKey");
// COLLECTION — shared within this collection only
pm.collectionVariables.set("apiVersion", "v2");
const ver = pm.collectionVariables.get("apiVersion");
// ENVIRONMENT — tied to the active environment (dev/staging)
pm.environment.set("baseUrl", "https://www.testerrank.com/api");
const url = pm.environment.get("baseUrl");
// LOCAL — temporary, dies after execution
pm.variables.set("tempToken", "abc123");
const temp = pm.variables.get("tempToken");
// DATA — read-only, comes from CSV/JSON file
const username = pm.iterationData.get("username");pm.variables.get() searches ALL scopes in order (data > local > environment > collection > global) and returns the first match. If you want a specific scope, use pm.environment.get() or pm.globals.get() explicitly.
Quick memory trick for scope order: "Do Lovely Elephants Cook Globally?" — Data, Local, Environment, Collection, Global. Narrowest to broadest.
Q: What is the variable scope hierarchy in Postman? If two variables have the same name, which one takes precedence?
A: The hierarchy from narrowest (highest priority) to broadest (lowest priority) is: Data > Local > Environment > Collection > Global. If a variable named "baseUrl" exists in both Environment and Collection scope, the Environment value is used. Data variables from CSV/JSON files override everything. This hierarchy lets you set sensible defaults at the collection level while overriding them per environment or per test run.
Key Point: Five scopes: Data > Local > Environment > Collection > Global. Narrower scope always wins when names collide.