0
0
Postmantesting~15 mins

Global variables in Postman - Deep Dive

Choose your learning style9 modes available
Overview - Global variables
What is it?
Global variables in Postman are values stored in a shared space accessible across all collections and requests within the Postman app. They allow you to save data once and reuse it anywhere without redefining it each time. This helps keep your tests and requests consistent and easier to manage. Global variables persist until you delete or overwrite them.
Why it matters
Without global variables, you would have to manually update repeated values in every request or test, which is time-consuming and error-prone. Global variables solve this by centralizing data, making your testing faster and less fragile. This means fewer mistakes and more reliable test runs, especially when working on large projects or with teams.
Where it fits
Before learning global variables, you should understand basic Postman requests and environment variables, which are similar but scoped differently. After mastering global variables, you can explore advanced scripting in Postman, like pre-request scripts and test scripts that dynamically use these variables.
Mental Model
Core Idea
Global variables are like sticky notes on a shared whiteboard that everyone in your Postman workspace can see and use anytime.
Think of it like...
Imagine a family calendar on the fridge where everyone writes important dates. Anyone in the house can check or add to it, so no one forgets important events. Global variables work the same way for your test data in Postman.
┌─────────────────────┐
│   Postman Workspace  │
│  ┌───────────────┐  │
│  │ Global Variables│  │
│  │  key: value    │  │
│  └───────────────┘  │
│  ┌───────────────┐  │
│  │ Collection A   │  │
│  └───────────────┘  │
│  ┌───────────────┐  │
│  │ Collection B   │  │
│  └───────────────┘  │
└─────────────────────┘

Global variables are accessible inside Collection A and B requests and tests.
Build-Up - 6 Steps
1
FoundationWhat are global variables
🤔
Concept: Introduce the idea of global variables as shared data storage in Postman.
Global variables store values that can be used anywhere in Postman. For example, you can save an API base URL as a global variable and use it in many requests without typing it repeatedly.
Result
You can reuse the same value across multiple requests easily.
Understanding that global variables hold shared data helps you avoid repeating information and reduces errors.
2
FoundationCreating and accessing global variables
🤔
Concept: Learn how to create, read, and update global variables in Postman.
You create global variables in the Postman app under the 'Globals' section or via scripts using pm.globals.set('key', 'value'). To read a global variable, use pm.globals.get('key'). You can also delete variables with pm.globals.unset('key').
Result
You can programmatically manage global variables during test runs.
Knowing how to manipulate global variables with scripts lets you automate data sharing across requests.
3
IntermediateUsing global variables in requests
🤔Before reading on: do you think global variables can be used directly inside request URLs and headers? Commit to your answer.
Concept: Learn how to insert global variables into request fields using Postman's variable syntax.
You can use {{variableName}} syntax in URLs, headers, or body fields. For example, if you have a global variable named 'baseUrl', you can write {{baseUrl}}/users to use its value dynamically.
Result
Requests become flexible and adapt to variable values without manual changes.
Understanding variable interpolation in requests makes your tests adaptable and easier to maintain.
4
IntermediateDifference between global and environment variables
🤔Before reading on: do you think global variables and environment variables behave the same way in Postman? Commit to your answer.
Concept: Distinguish global variables from environment variables by scope and use cases.
Global variables are accessible everywhere in Postman, while environment variables are limited to the selected environment. Environments help switch contexts like dev, test, or production easily, whereas globals are constant across all environments.
Result
You know when to use globals for universal data and environments for context-specific data.
Knowing the scope difference prevents confusion and helps organize your variables effectively.
5
AdvancedManaging global variables with scripts
🤔Before reading on: do you think global variables can be changed during a test run? Commit to your answer.
Concept: Use pre-request and test scripts to dynamically set or update global variables based on test results or responses.
In a test script, you can write pm.globals.set('token', pm.response.json().token) to save a token from a response. This token can then be used in subsequent requests automatically.
Result
Your test flow becomes dynamic and can adapt to changing data during execution.
Understanding dynamic updates to globals enables complex test scenarios and data-driven testing.
6
ExpertPitfalls and best practices with global variables
🤔Before reading on: do you think overusing global variables is always good for test clarity? Commit to your answer.
Concept: Learn the risks of excessive global variable use and how to avoid conflicts and confusion.
Using too many global variables can cause unexpected overwrites and make tests hard to debug. It's best to limit globals to truly shared data and prefer environment or local variables for scoped data. Also, clear globals when no longer needed to avoid stale data.
Result
Your tests remain clear, maintainable, and less error-prone.
Knowing when and how to use globals prevents common bugs and improves test reliability.
Under the Hood
Postman stores global variables in a shared JSON object within the app's local storage. When a request runs, Postman replaces {{variableName}} placeholders with the current value from this object. Scripts can read and write to this object at runtime, allowing dynamic updates. The global scope means these variables are accessible regardless of the active environment or collection.
Why designed this way?
Global variables were designed to provide a simple, universal data store accessible across all requests and collections. This avoids duplication and supports reuse. Alternatives like environment variables offer scoped data for flexibility. The design balances ease of use with scope control, allowing users to choose the right level of sharing.
┌───────────────────────────────┐
│       Postman Application      │
│ ┌───────────────┐             │
│ │ Global Store  │◄────────────┤
│ │ {key: value}  │             │
│ └───────────────┘             │
│        ▲                      │
│        │                      │
│ ┌───────────────┐             │
│ │ Request Runner│             │
│ └───────────────┘             │
│        │                      │
│  Replaces {{key}} with value  │
└───────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Do global variables automatically update across all running requests if changed mid-run? Commit to yes or no.
Common Belief:Global variables update instantly everywhere during a test run without any delay or caching.
Tap to reveal reality
Reality:Global variables update only after the current request finishes; running requests use the value at their start time.
Why it matters:Assuming instant updates can cause flaky tests where some requests use old values, leading to confusing failures.
Quick: Are global variables safer than environment variables because they are shared? Commit to yes or no.
Common Belief:Global variables are safer because they are shared and consistent everywhere.
Tap to reveal reality
Reality:Global variables can cause conflicts and accidental overwrites if many tests use the same keys without coordination.
Why it matters:Misusing globals can cause hard-to-find bugs and test interference, especially in team environments.
Quick: Can you use global variables to store sensitive data like passwords securely? Commit to yes or no.
Common Belief:Global variables are secure places to store sensitive information like passwords or tokens.
Tap to reveal reality
Reality:Global variables are stored locally and visible in Postman; they are not encrypted and can be exposed accidentally.
Why it matters:Storing sensitive data in globals risks leaks; better to use environment variables with proper access controls.
Quick: Do global variables persist forever unless manually deleted? Commit to yes or no.
Common Belief:Global variables stay forever once created, even if you close Postman or restart your computer.
Tap to reveal reality
Reality:Global variables persist across sessions but can be cleared by user action or app resets; they are not permanent storage.
Why it matters:Relying on globals for permanent data storage can cause unexpected data loss.
Expert Zone
1
Global variables are shared across all workspaces in Postman, which can cause unexpected data leaks if not managed carefully.
2
Scripts that update global variables run synchronously after a request completes, so timing of updates matters in chained requests.
3
Using descriptive and unique keys for globals prevents collisions and makes debugging easier in complex test suites.
When NOT to use
Avoid using global variables when data should be isolated per environment or collection; use environment or collection variables instead. For sensitive data, use Postman's secret management or environment variables with restricted access. For temporary data within a single request, use local variables.
Production Patterns
In real projects, global variables often store constants like API base URLs or shared tokens. Teams use environment variables for environment-specific data and reserve globals for truly universal values. Automated tests dynamically update globals to share data like authentication tokens between requests. Proper naming conventions and cleanup scripts keep globals manageable.
Connections
Environment variables
Complementary concept with different scope
Understanding environment variables helps clarify when to use globals versus scoped data, improving test organization.
Session management in web browsers
Similar pattern of scoped versus global storage
Knowing how browsers use sessionStorage (scoped) and localStorage (global) helps grasp variable scopes in Postman.
Shared memory in operating systems
Analogous concept of shared data accessible by multiple processes
Recognizing global variables as shared memory helps understand risks like data conflicts and the need for careful management.
Common Pitfalls
#1Overwriting global variables unintentionally
Wrong approach:pm.globals.set('token', 'abc123'); pm.globals.set('token', 'xyz789'); // overwrites without warning
Correct approach:if (!pm.globals.get('token')) { pm.globals.set('token', 'abc123'); } // only set if not already set
Root cause:Not checking existing values leads to accidental overwrites and test failures.
#2Using global variables for environment-specific data
Wrong approach:pm.globals.set('baseUrl', 'https://dev.api.com'); // used in production tests accidentally
Correct approach:pm.environment.set('baseUrl', 'https://dev.api.com'); // environment variable for dev only
Root cause:Confusing global and environment scopes causes wrong data usage in tests.
#3Not clearing stale global variables
Wrong approach:pm.globals.set('tempData', '123'); // never removed, accumulates stale data
Correct approach:pm.globals.unset('tempData'); // clean up after use
Root cause:Leaving old data causes unexpected behavior and test flakiness.
Key Takeaways
Global variables in Postman store shared data accessible across all requests and collections.
They help avoid repetition and make tests easier to maintain but require careful management to prevent conflicts.
Use {{variableName}} syntax to insert global variables into requests dynamically.
Distinguish global variables from environment variables by their scope and use cases.
Avoid overusing globals; prefer environment or local variables when appropriate to keep tests clear and reliable.