</>

Technology

Postman

Difficulty

Intermediate

Interview Question

What are Global, Environment, and Collection variables in Postman?

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

ScopeUse For
GlobalTokens shared across all collections
EnvironmentBase URLs, API keys per environment
CollectionValues specific to one API project
DataData-driven testing with CSV/JSON
LocalTemporary calculations within a script

Follow AutomateQA

Related Topics