? Variable Scope & Priority in Postman
? Quick Overview
Postman allows variables to be defined at multiple levels. Variable scope determines where a variable is accessible, while priority (resolution order) decides which value is used when the same variable name exists in multiple scopes.
? Key Concepts
- Variables help reuse values like URLs, tokens, and IDs
- Same variable name can exist in different scopes
- Postman follows a fixed resolution order
- Nearest scope takes priority
? Syntax / Theory
Variables are accessed using double curly braces: {{variableName}}. Postman checks scopes in order and resolves the first matching variable found.
? Code Example(s)
? View Code Example
GET {{baseUrl}}/users/{{userId}}
? View Code Example
pm.environment.set("token","abc123");
? Live Output / Explanation
If baseUrl exists in both Environment and Collection scopes, Postman uses the Environment value because it has higher priority.
? Interactive Resolution Order Diagram
- Local (Request / Script)
- Data (Runner)
- Environment
- Collection
- Global
?️ Interactive Scope Simulator
Enter values for the variable {{myVar}} below to see which one takes priority.
? Use Cases
- Environment-specific base URLs
- Dynamic tokens during API testing
- Reusable collection-level constants
? Tips & Best Practices
- Use Environment variables for sensitive data
- Avoid same variable names across scopes unless necessary
- Clear unused Global variables regularly
? Try It Yourself
- Create the same variable in Global and Environment scopes
- Use it in a request and observe which value is resolved
- Change scope values and re-run the request