Answer
Variable Scopes in Postman
Postman has 5 variable scopes listed from highest to lowest priority:
CODE
Local > Data > Environment > Collection > Global
When variables share the same name, the higher-priority scope wins.
✦
1. Global Variables
- ✓Available across all workspaces, collections, and environments
- ✓Useful for values shared across everything
- ✓Set in: Environments → Globals
JavaScript
// Set global variable in script
pm.globals.set("global_token", "abc123");
// Get global variable
const token = pm.globals.get("global_token");
✦
2. Environment Variables
- ✓Available within the selected environment only
- ✓Ideal for: base URLs, API keys, user credentials per environment
- ✓Change by switching the active environment
JavaScript
pm.environment.set("base_url", "https://api.staging.com");
const url = pm.environment.get("base_url");
✦
3. Collection Variables
- ✓Scoped to a specific collection
- ✓Good for variables shared across all requests in a collection
JavaScript
pm.collectionVariables.set("user_id", "123");
const id = pm.collectionVariables.get("user_id");
✦
4. Data Variables
- ✓Come from external CSV or JSON files used in Collection Runner
- ✓Only available during a collection run
- ✓Example CSV row:
username,password
✦
5. Local Variables
- ✓Only available in the current script execution (Pre-request or Test)
- ✓Highest priority but temporary
JavaScript
pm.variables.set("temp_value", "xyz");
const val = pm.variables.get("temp_value");
✦
When to Use Which
| Scope | Use For |
|---|---|
| Global | Tokens shared across all collections |
| Environment | Base URLs, API keys per environment |
| Collection | Values specific to one API project |
| Data | Data-driven testing with CSV/JSON |
| Local | Temporary calculations within a script |
