Variable assignment in flows helps you store and reuse data during your API tests. It makes your tests flexible and easier to manage.
Variable assignment in flows in Postman
pm.variables.set('variable_name', value); pm.variables.get('variable_name');
Use pm.variables.set to assign a value to a variable.
Use pm.variables.get to retrieve the value later in the flow.
userId to 12345 and then gets it to use later.pm.variables.set('userId', 12345); const id = pm.variables.get('userId');
pm.variables.set('token', pm.response.json().token); const token = pm.variables.get('token');
This script saves the user ID from the API response, retrieves it, prints it, and checks that it is a number.
// Save user ID from response pm.variables.set('userId', pm.response.json().id); // Use saved user ID in this request const userId = pm.variables.get('userId'); console.log(`User ID is: ${userId}`); // Check if userId is set correctly pm.test('User ID is assigned', function () { pm.expect(userId).to.be.a('number'); });
Variables set with pm.variables.set last only during the current request execution.
To keep variables across requests, consider using pm.environment.set or pm.collectionVariables.set.
Always check if the value you want to assign exists in the response to avoid errors.
Variable assignment lets you save and reuse data during API tests.
Use pm.variables.set to assign and pm.variables.get to retrieve values.
This makes your tests flexible and easier to maintain.
Practice
pm.variables.set('token', 'abc123') do in a Postman test script?Solution
Step 1: Understand the method
This method assigns a value to a variable in Postman scripts.pm.variables.setStep 2: Analyze the parameters
The first parameter is the variable name 'token', the second is the value 'abc123' to store.Final Answer:
It saves the value 'abc123' to a variable named 'token' for later use. -> Option AQuick Check:
pm.variables.set saves data [OK]
- Confusing set with get method
- Thinking set deletes variables
- Assuming set sends data in requests
Solution
Step 1: Recall the correct method for assigning variables
The method to assign a variable ispm.variables.set(name, value).Step 2: Check each option's syntax
pm.variables.set('userId', 42); usespm.variables.set('userId', 42);which is correct syntax.Final Answer:
pm.variables.set('userId', 42); -> Option DQuick Check:
Use pm.variables.set to assign [OK]
- Using get instead of set to assign
- Incorrect method names like assign or store
- Wrong parameter syntax with equals sign
pm.variables.set('count', 5);
pm.variables.set('count', pm.variables.get('count') + 3);
console.log(pm.variables.get('count'));What will be printed in the console?
Solution
Step 1: Analyze the first assignment
The variable 'count' is set to 5 initially.Step 2: Analyze the second assignment
It gets the current 'count' value (5), adds 3, and sets 'count' to 8.Step 3: Check the console.log output
It prints the updated value of 'count', which is 8.Final Answer:
8 -> Option BQuick Check:
5 + 3 = 8 [OK]
- Assuming the first value stays unchanged
- Thinking get returns undefined
- Confusing set and get usage
pm.variables.set('status', 'active')
pm.variables.get('status')But the test fails because the variable 'status' is undefined. What is the likely cause?
Solution
Step 1: Understand variable scopes in Postman
Variables set withpm.variables.setare local to the current request script and do not persist across requests.Step 2: Identify why 'status' is undefined
If you try to get 'status' in a different request without using environment or global variables, it will be undefined.Final Answer:
Variables set in one request are not available in another without environment or global scope. -> Option AQuick Check:
Local variables don't persist across requests [OK]
- Thinking semicolons affect variable persistence
- Believing get waits for set automatically
- Assuming variable names can't be strings
Solution
Step 1: Identify variable scope for cross-request use
To reuse data across requests, environment variables must be used, not local variables.Step 2: Match correct set and get methods
In first request test script:pm.environment.set('token', pm.response.json().token);
In next request pre-request script:const token = pm.environment.get('token');usespm.environment.setandpm.environment.get, which correctly share data between requests.Final Answer:
Use environment variables to save and retrieve token across requests. -> Option CQuick Check:
Environment variables persist across requests [OK]
- Using pm.variables for cross-request data
- Mixing environment and local variable methods
- Not retrieving variables in the correct scope
Ready to prove it?
Pass the test and this pattern moves unknown
