Setting variables in scripts helps you save and reuse data during your tests. It makes your tests flexible and easier to manage.
Setting variables in scripts in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Postman
// Set a variable pm.variables.set('variableName', 'value'); // Get a variable let value = pm.variables.get('variableName');
Use pm.variables.set to create or update a variable.
Use pm.variables.get to read the variable's value.
Examples
token with the value 12345abcde.Postman
pm.variables.set('token', '12345abcde');
token and prints it to the console.Postman
let token = pm.variables.get('token');
console.log(token);userId with value 789. Environment variables last longer than local variables.Postman
pm.environment.set('userId', '789');
userId.Postman
let userId = pm.environment.get('userId');Sample Program
This script saves the id from the response JSON into a variable called userId. Then it retrieves and prints it. Finally, it checks if the variable was set correctly.
Postman
// Example: Save a value from response and reuse it // Assume response JSON: { "id": "abc123", "name": "John" } // Save 'id' from response to a variable let jsonData = pm.response.json(); pm.variables.set('userId', jsonData.id); // Later, get the variable and print it let savedId = pm.variables.get('userId'); console.log(`Saved userId is: ${savedId}`); // Test to check if variable is set correctly pm.test('userId variable is set', function () { pm.expect(savedId).to.eql('abc123'); });
Important Notes
Variables set with pm.variables.set exist only during the request execution.
Use pm.environment.set or pm.collectionVariables.set to save variables longer.
Always check if the response contains the data before setting variables to avoid errors.
Summary
Setting variables lets you store and reuse data during tests.
Use pm.variables.set and pm.variables.get for local variables.
Variables help make tests dynamic and easier to maintain.
Practice
1. What is the purpose of using
pm.variables.set in Postman scripts?easy
Solution
Step 1: Understand the function of
This function stores a value in a local variable that can be used later in the same request or script.pm.variables.setStep 2: Differentiate from other actions
Sending requests, deleting variables, or logging are done by other methods, notpm.variables.set.Final Answer:
To store a value in a local variable for later use within the same request -> Option AQuick Check:
pm.variables.set stores local variables [OK]
Hint: Remember: set means save value locally in script [OK]
Common Mistakes:
- Confusing variable setting with sending requests
- Thinking it deletes variables
- Assuming it logs output
2. Which of the following is the correct syntax to set a variable named
token with value abc123 in a Postman test script?easy
Solution
Step 1: Check the correct method signature
The correct syntax usespm.variables.set('variableName', 'value');with the variable name as a string and value as the second argument.Step 2: Identify syntax errors in other options
pm.variables.set(token, 'abc123'); misses quotes around the variable name. pm.variables.set('token' = 'abc123'); uses an invalid assignment inside the method. pm.variables.set['token', 'abc123']; uses incorrect bracket notation.Final Answer:
pm.variables.set('token', 'abc123'); -> Option DQuick Check:
Use quotes for variable name in pm.variables.set [OK]
Hint: Variable names must be strings in quotes [OK]
Common Mistakes:
- Omitting quotes around variable names
- Using assignment inside set method
- Using wrong brackets for method call
3. Consider this Postman test script snippet:
What will be printed in the Postman console?
pm.variables.set('userId', 42);
const id = pm.variables.get('userId');
console.log(id);What will be printed in the Postman console?
medium
Solution
Step 1: Understand variable setting and getting
The script sets 'userId' to the number 42, then retrieves it withpm.variables.get.Step 2: Check the console output
The retrieved value is 42 (a number), soconsole.log(id);prints 42 without quotes.Final Answer:
42 -> Option AQuick Check:
Set and get return the same stored value [OK]
Hint: Get returns exactly what was set, including type [OK]
Common Mistakes:
- Assuming get returns string always
- Expecting undefined if variable not set
- Confusing quotes in console output
4. You wrote this Postman script:
But the variable
pm.variables.set('session', 'abc');
pm.variables.get('session');But the variable
session is not accessible in later requests. What is the likely problem?medium
Solution
Step 1: Understand variable scopes in Postman
pm.variables.setsets a local variable only for the current script execution, not across requests.Step 2: Use environment variables for persistence
To keep variables accessible across requests, usepm.environment.setinstead.Final Answer:
You should usepm.environment.setto make it accessible across requests -> Option BQuick Check:
Local variables are temporary; environment variables persist [OK]
Hint: Use environment.set for cross-request variables [OK]
Common Mistakes:
- Expecting pm.variables.set to persist across requests
- Thinking a save method is needed
- Believing variable names can't be strings
5. You want to set a variable
authToken in a Pre-request Script that depends on the response of a previous request stored in pm.response.json(). Which approach correctly sets authToken for use in the next request?hard
Solution
Step 1: Identify when response data is available
The response data frompm.response.json()is only available in the Tests script after the request completes, not in the Pre-request Script.Step 2: Set variable for next request
To useauthTokenin the next request, set it as an environment variable in the Tests script of the current request usingpm.environment.set.Step 3: Why not local variables?
Local variables set withpm.variables.setdo not persist across requests, so they won't be available in the next request.Final Answer:
Use pm.environment.set('authToken', pm.response.json().token); in the Tests script of the previous request -> Option CQuick Check:
Set environment variables in Tests to share data between requests [OK]
Hint: Set environment vars in Tests script to share between requests [OK]
Common Mistakes:
- Trying to access response in Pre-request Script
- Using pm.variables.set for cross-request data
- Setting variables in wrong script phase
