0
0
Postmantesting~15 mins

Local variables in Postman - Deep Dive

Choose your learning style9 modes available
Overview - Local variables
What is it?
Local variables in Postman are temporary values stored during the execution of a single request or script. They exist only while the current request runs and disappear afterward. These variables help you store and reuse data within the same request or script without affecting other requests. They are useful for managing data that changes often or is only relevant temporarily.
Why it matters
Without local variables, you would have to hardcode values or rely on global or environment variables that persist beyond a single request. This can cause confusion, data leaks, or errors when tests run in parallel or in sequence. Local variables keep data isolated and safe, making your tests more reliable and easier to maintain. They allow you to simulate real user scenarios where data changes dynamically.
Where it fits
Before learning local variables, you should understand what variables are in general and how Postman environments and globals work. After mastering local variables, you can learn about data persistence with environment and global variables, and advanced scripting techniques like chaining requests and dynamic data generation.
Mental Model
Core Idea
Local variables are temporary containers that hold data only during a single request or script execution in Postman.
Think of it like...
Local variables are like sticky notes you write on your desk while working on a task; once you finish, you throw them away so they don't clutter your workspace.
┌───────────────┐
│ Postman Run   │
│ ┌───────────┐ │
│ │ Local Var │ │  <-- Exists only here
│ └───────────┘ │
│               │
│ ┌───────────┐ │
│ │ Env Var   │ │  <-- Exists across requests
│ └───────────┘ │
│               │
│ ┌───────────┐ │
│ │ Global Var│ │  <-- Exists across collections
│ └───────────┘ │
└───────────────┘
Build-Up - 6 Steps
1
FoundationWhat are local variables in Postman
🤔
Concept: Introduce the idea of local variables as temporary data holders during a request.
In Postman, local variables are created and used inside the Pre-request Script or Tests tab of a request. They store data only while the request runs. For example, you can set a local variable using pm.variables.set('key', 'value') and get it with pm.variables.get('key'). These variables do not persist after the request finishes.
Result
You can store and retrieve data within the same request execution without affecting other requests.
Understanding that local variables are temporary helps prevent accidental data sharing between requests, making tests more predictable.
2
FoundationHow to create and access local variables
🤔
Concept: Learn the syntax and methods to set and get local variables in Postman scripts.
Use pm.variables.set('name', 'value') to create or update a local variable. Use pm.variables.get('name') to read its value. For example: pm.variables.set('token', 'abc123'); console.log(pm.variables.get('token')); // prints 'abc123' Local variables can also be used in request URLs or headers with {{variableName}} syntax, but only if they exist in the current scope.
Result
You can dynamically assign and use data within a request's script and reference it in the request itself.
Knowing the exact methods to manipulate local variables empowers you to write flexible and dynamic tests.
3
IntermediateDifference between local and environment variables
🤔Before reading on: do you think local variables persist across multiple requests like environment variables? Commit to your answer.
Concept: Understand the scope and lifetime differences between local and environment variables.
Local variables exist only during the execution of a single request or script. Environment variables persist across multiple requests within the same environment. For example, if you set a local variable in one request, it won't be available in the next request. But environment variables keep their values until changed or cleared.
Result
You learn to choose the right variable type depending on whether data should persist or be temporary.
Recognizing variable scopes prevents bugs caused by unexpected data persistence or loss.
4
IntermediateUsing local variables in request chaining
🤔Before reading on: can local variables be used to pass data between different requests? Commit to your answer.
Concept: Explore how local variables behave when running multiple requests in sequence.
Local variables cannot pass data between requests because they reset after each request finishes. To share data across requests, you must use environment or global variables. For example, if Request A sets a local variable, Request B cannot access it. Instead, Request A should set an environment variable if the data needs to be reused.
Result
You understand the limitations of local variables in multi-request workflows.
Knowing when local variables reset helps design proper data flow in test suites.
5
AdvancedLocal variables and script execution order
🤔Before reading on: do you think local variables set in Pre-request Scripts are available in Tests scripts of the same request? Commit to your answer.
Concept: Learn how local variables behave across different script phases within a request.
Postman runs scripts in this order: Pre-request Script, then the request, then Tests script. Local variables set in Pre-request Script are available in the Tests script of the same request. For example: // Pre-request Script pm.variables.set('startTime', Date.now()); // Tests const start = pm.variables.get('startTime'); const end = Date.now(); console.log('Request duration:', end - start); This allows you to share data within the same request lifecycle.
Result
You can coordinate data within different script phases of a request using local variables.
Understanding script execution order unlocks powerful timing and data tracking techniques.
6
ExpertLocal variables internal precedence and shadowing
🤔Before reading on: if a local variable and an environment variable share the same name, which one does Postman use in {{variable}} syntax? Commit to your answer.
Concept: Discover how Postman resolves variable names when multiple scopes have the same key.
Postman resolves variables in this order: local variables, data variables (from data files), environment variables, then global variables. If a local variable and an environment variable share the same name, the local variable takes precedence when using {{variable}} syntax. This means local variables can shadow environment variables temporarily during a request. For example, if you have an environment variable 'user' = 'Alice' and a local variable 'user' = 'Bob', {{user}} will resolve to 'Bob' in that request.
Result
You can control which variable value is used by setting local variables to override others temporarily.
Knowing variable precedence prevents confusion and bugs caused by unexpected variable values.
Under the Hood
Postman maintains multiple variable scopes in memory during runtime. When a request runs, it creates a local variable store that lives only for that request's duration. Variable lookups first check this local store before falling back to environment or global stores. After the request completes, the local store is discarded. This layered lookup ensures that temporary data can override persistent data without permanent changes.
Why designed this way?
This design allows flexibility and safety. Temporary data can be used without risking accidental overwrites of persistent variables. It also supports concurrent requests and parallel testing by isolating data per request. Alternatives like only having global variables would cause data conflicts and harder test maintenance.
┌─────────────────────────────┐
│ Postman Variable Lookup     │
├─────────────────────────────┤
│ 1. Local Variables (request)│
│ 2. Data Variables (file)    │
│ 3. Environment Variables    │
│ 4. Global Variables         │
└─────────────────────────────┘

Local variables exist only during request execution and override others.
Myth Busters - 4 Common Misconceptions
Quick: Do local variables keep their values after the request finishes? Commit to yes or no.
Common Belief:Local variables persist across multiple requests like environment variables.
Tap to reveal reality
Reality:Local variables exist only during the current request execution and are discarded afterward.
Why it matters:Assuming local variables persist can cause tests to fail when data unexpectedly disappears between requests.
Quick: If a local variable and an environment variable have the same name, which one does Postman use? Commit to your answer.
Common Belief:Environment variables always override local variables because they are more global.
Tap to reveal reality
Reality:Local variables have higher precedence and override environment variables during a request.
Why it matters:Misunderstanding precedence can lead to confusing bugs where the wrong variable value is used.
Quick: Can local variables be used to share data between requests in a collection run? Commit to yes or no.
Common Belief:Local variables can pass data between requests during collection runs.
Tap to reveal reality
Reality:Local variables reset after each request and cannot share data between requests; environment or global variables must be used instead.
Why it matters:Trying to use local variables for data sharing causes tests to fail or behave unpredictably.
Quick: Are local variables accessible in the Tests script if set in the Pre-request Script? Commit to yes or no.
Common Belief:Local variables set in Pre-request Scripts are not available in Tests scripts of the same request.
Tap to reveal reality
Reality:Local variables set in Pre-request Scripts are accessible in Tests scripts within the same request execution.
Why it matters:Knowing this allows you to coordinate data and timing measurements within a single request.
Expert Zone
1
Local variables can shadow environment and global variables temporarily, allowing fine-grained control without permanent changes.
2
Local variables are isolated per request, which enables safe parallel execution of tests without data collision.
3
Using local variables for temporary data reduces the risk of polluting environment variables, which can affect other tests.
When NOT to use
Do not use local variables when you need data to persist across multiple requests or collection runs. Instead, use environment or global variables. Also, avoid local variables for data that must be shared between team members or across different machines; use environments or external data files for that.
Production Patterns
In real-world Postman test suites, local variables are used to store temporary tokens, timestamps, or intermediate calculations within a request. Environment variables hold user credentials or base URLs. Testers often combine local variables with environment variables to create flexible, reusable tests that adapt to different scenarios without side effects.
Connections
Variable Scope in Programming
Local variables in Postman follow the same concept of variable scope as in programming languages like JavaScript or Python.
Understanding how variable scope works in programming helps grasp why local variables in Postman are temporary and isolated to a single execution context.
Session Storage in Web Browsers
Local variables in Postman are similar to session storage in browsers, which stores data temporarily during a browsing session.
Knowing session storage behavior clarifies why local variables disappear after a request, just like session data clears after a browser tab closes.
Ephemeral Memory in Operating Systems
Local variables act like ephemeral memory that exists only during a process's lifetime and is cleared afterward.
This connection helps understand the importance of temporary data isolation to prevent interference between processes or requests.
Common Pitfalls
#1Expecting local variables to persist across requests.
Wrong approach:pm.variables.set('sessionId', '12345'); // In next request console.log(pm.variables.get('sessionId')); // undefined
Correct approach:pm.environment.set('sessionId', '12345'); // In next request console.log(pm.environment.get('sessionId')); // '12345'
Root cause:Confusing local variable scope with environment variable scope leads to lost data between requests.
#2Using the same variable name in local and environment scopes without understanding precedence.
Wrong approach:pm.environment.set('user', 'Alice'); pm.variables.set('user', 'Bob'); console.log(pm.variables.get('user')); // 'Bob' // But using {{user}} in request resolves to 'Bob' unexpectedly
Correct approach:Use distinct variable names or be aware that local variables override environment variables when using {{variable}} syntax.
Root cause:Not knowing variable precedence causes unexpected variable values in requests.
#3Trying to share data between requests using local variables.
Wrong approach:// Request 1 pm.variables.set('token', 'abc'); // Request 2 console.log(pm.variables.get('token')); // undefined
Correct approach:// Request 1 pm.environment.set('token', 'abc'); // Request 2 console.log(pm.environment.get('token')); // 'abc'
Root cause:Misunderstanding that local variables reset after each request.
Key Takeaways
Local variables in Postman exist only during a single request execution and are discarded afterward.
They have higher precedence than environment and global variables when resolving variable names.
Local variables cannot share data between requests; use environment or global variables for persistence.
You can set and get local variables using pm.variables.set() and pm.variables.get() in scripts.
Understanding local variables helps write safer, more predictable, and maintainable tests.