← Back to Chapters

Variables in Postman

? Variables in Postman

? Quick Overview

Variables in Postman allow you to store and reuse values dynamically across requests. They help avoid hardcoding values like URLs, tokens, IDs, and credentials. Postman supports Global, Collection, and Local variables.

? Key Concepts

  • Variables store key–value pairs
  • Values can change during request execution
  • Variables improve reusability and maintainability
  • Scope determines variable accessibility

? Syntax / Theory

Variables are accessed using double curly braces syntax:

{{variableName}}

Postman resolves variables based on priority: Local → Data → Environment → Collection → Global

? Code Example(s)

? View Code Example
// Using a variable in a request URL
GET {{baseUrl}}/users/{{userId}}
? View Code Example
// Setting a collection variable in Tests tab
pm.collectionVariables.set("token","abc123");

? Live Output / Explanation

Explanation

Postman replaces {{baseUrl}} and {{userId}} with their current values before sending the request. This makes APIs flexible across environments.

? Interactive Example / Diagram

Think of variable scopes as layers. Use the simulator below to understand how Postman decides which value to use when the same variable name exists in different scopes.

  • Global – Available everywhere
  • Collection – Limited to a collection
  • Local – Exists only during request execution

? Scope Priority Simulator

Imagine you have a variable named {{apiKey}} defined in three places. Enter values below to see which one "wins".

Final Resolved Value for {{apiKey}}
undefined
? View Code Example
// Local variable example inside a script
pm.variables.set("tempValue",100);

? Use Cases

  • Switching between dev, test, and prod URLs
  • Storing authentication tokens
  • Passing IDs between requests
  • Dynamic request chaining

? Tips & Best Practices

  • Prefer Collection variables over Global for safety
  • Use meaningful variable names
  • Clear unused variables regularly
  • Use Local variables for temporary values

? Try It Yourself

  • Create a collection variable called baseUrl
  • Use it in multiple requests
  • Set a variable in Tests tab and reuse it
  • Observe scope priority by defining same name variable