What if you could change one value and fix all your tests at once?
Why Global variables in Postman? - Purpose & Use Cases
Imagine you are testing an API with many requests. Each request needs the same data, like a user token or a server URL. You write this data again and again in every request.
This manual way is slow and tiring. If the token changes, you must update it in every request. You might forget some places, causing errors and confusion.
Global variables let you store data once and use it everywhere. Change it once, and all requests get the update automatically. This saves time and avoids mistakes.
Set token in each request header manually.Use a global variable {{token}} in headers for all requests.Global variables make your tests faster, cleaner, and easier to maintain across many requests.
When testing a login API, you store the login token as a global variable. All other API calls use this token automatically without rewriting it.
Manual data repetition is slow and error-prone.
Global variables store data once for all requests.
They simplify updates and improve test reliability.
Practice
Solution
Step 1: Understand global variable scope
Global variables in Postman are accessible everywhere, across all requests and tests.Step 2: Compare with other variable types
Unlike environment or local variables, global variables persist and are shared globally.Final Answer:
To store data accessible across all requests and tests -> Option AQuick Check:
Global variables = accessible everywhere [OK]
- Confusing global variables with environment variables
- Thinking global variables are temporary
- Assuming global variables are request-specific
token with value abc123 in Postman?Solution
Step 1: Recall correct Postman global variable syntax
The correct method to set a global variable ispm.globals.set(key, value).Step 2: Identify the correct object and method
Only pm.globals.set('token', 'abc123'); usespm.globals.setcorrectly with proper quotes and parameters.Final Answer:
pm.globals.set('token', 'abc123'); -> Option CQuick Check:
Use pm.globals.set to set global variables [OK]
- Using pm.global instead of pm.globals
- Using .add instead of .set method
- Incorrect object names like pm.globalVariables
pm.globals.set('userId', '42');
const id = pm.globals.get('userId');
console.log(id);What will be printed in the Postman console?
Solution
Step 1: Set global variable 'userId' to '42'
The first line stores the string '42' in the global variable named 'userId'.Step 2: Retrieve and log the global variable
The second line gets the value of 'userId' which is '42', then logs it.Final Answer:
42 -> Option BQuick Check:
pm.globals.get('userId') returns '42' [OK]
- Expecting variable name instead of value
- Confusing undefined with null
- Forgetting to set the variable before getting
pm.globals.set(userId, '100');
But it throws an error. What is the problem?
Solution
Step 1: Check the syntax of pm.globals.set
The first argument must be a string key, so it should be quoted like 'userId'.Step 2: Identify the error cause
Without quotes, userId is treated as an undefined variable causing an error.Final Answer:
The variable name userId should be a string in quotes -> Option AQuick Check:
Keys must be strings in pm.globals.set [OK]
- Not quoting the key name
- Thinking pm.globals.set needs more arguments
- Believing global variables can't be set in scripts
// Request 1
pm.globals.set('sessionToken', 'xyz789');
// Request 2
const token = pm.globals.get('sessionToken');
console.log(token);Solution
Step 1: Set global variable in first request
The first request sets 'sessionToken' globally using pm.globals.set.Step 2: Retrieve global variable in second request
The second request gets the same 'sessionToken' using pm.globals.get and logs it.Final Answer:
This sequence correctly shares the token globally -> Option DQuick Check:
Global variables share data across requests [OK]
- Thinking globals can't share data between requests
- Confusing environment and global variables
- Trying to pass token manually instead of using variables
Ready to prove it?
Pass the test and this pattern moves unknown
