← Back to Chapters

Variable Scope & Priority in Postman

? 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
// Accessing a variable inside a request URL
GET {{baseUrl}}/users/{{userId}}
? View Code Example
// Setting an environment variable in a test script
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

  1. Local (Request / Script)
  2. Data (Runner)
  3. Environment
  4. Collection
  5. Global

?️ Interactive Scope Simulator

Enter values for the variable {{myVar}} below to see which one takes priority.

Postman uses:
undefined
(No variables set)

? 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