Token management in variables in Postman - Build an Automation Script
/* Login Request Tests */ pm.test('Login status code is 200', () => { pm.response.to.have.status(200); }); const jsonData = pm.response.json(); pm.test('Token is present in response', () => { pm.expect(jsonData.token).to.exist; }); pm.environment.set('authToken', jsonData.token); /* Protected API Request Tests */ pm.test('Protected API status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Protected API response contains expected data', () => { const data = pm.response.json(); pm.expect(data).to.have.property('protectedData'); pm.expect(data.protectedData).to.be.a('string').that.is.not.empty; });
This script is split into two parts for two requests in Postman.
For the login request, it checks the status code is 200, then parses the JSON response to get the token. It asserts the token exists, then saves it to an environment variable authToken using pm.environment.set().
For the protected API request, it uses the saved authToken in the Authorization header as Bearer {{authToken}}. The tests verify the status code is 200 and the response contains the expected protected data property.
This approach ensures the token is dynamically managed and reused securely between requests.
Now add data-driven testing with 3 different user credentials to login and verify token management
Practice
Solution
Step 1: Understand token reuse in Postman
Storing a token in an environment variable allows multiple requests to access it easily without needing to get a new token each time.Step 2: Evaluate other options
Making the token visible to all users or automatic refresh without scripting is not true by default. Encryption is not automatic either.Final Answer:
To reuse the token across multiple requests without re-authenticating each time -> Option DQuick Check:
Token reuse = B [OK]
- Thinking tokens auto-refresh without scripts
- Assuming variables encrypt tokens automatically
- Believing tokens are shared with all users by default
Solution
Step 1: Identify the current Postman syntax for setting environment variables
The correct method is pm.environment.set('variableName', value) in Postman scripts.Step 2: Check other options for correctness
pm.setEnvironmentVariable is deprecated, direct assignment is invalid, and pm.variables.set sets local variables, not environment variables.Final Answer:
pm.environment.set('token', response.token); -> Option AQuick Check:
Use pm.environment.set() to set env variables [OK]
- Using deprecated pm.setEnvironmentVariable method
- Trying to assign variables directly like pm.environment.token
- Confusing local and environment variables
let jsonData = pm.response.json();
pm.environment.set('authToken', jsonData.token);What will be the value of
{{authToken}} in the next request if the response JSON is {"token": "abc123"}?Solution
Step 1: Extract token from response JSON
The script gets the token value "abc123" from the response JSON using pm.response.json().token.Step 2: Set environment variable 'authToken'
The token value "abc123" is stored in the environment variable 'authToken' using pm.environment.set.Final Answer:
"abc123" -> Option CQuick Check:
Stored token = "abc123" [OK]
- Assuming variable is undefined if not explicitly declared
- Confusing variable name with function call
- Expecting null instead of actual token string
let jsonData = pm.response.json();
pm.environment.set('token', jsonData.authToken);But the token is not saved. What is the most likely reason?
Solution
Step 1: Check the JSON key used in script
The script tries to access jsonData.authToken, so the response must have that key.Step 2: Verify if the response JSON contains 'authToken'
If the response uses a different key like 'token', jsonData.authToken will be undefined and nothing is saved.Final Answer:
The response JSON does not have a key named 'authToken' -> Option BQuick Check:
Key mismatch causes undefined token [OK]
- Assuming pm.environment.set is deprecated
- Using pm.variables.set for environment variables
- Believing tokens can't be saved in environment variables
Solution
Step 1: Understand token expiry handling
Tokens expire, so scripts must check expiry and refresh tokens automatically to avoid failures.Step 2: Use pre-request scripts to automate token refresh
Pre-request scripts can check if the token is expired and call the authentication endpoint to get a new token, then update the environment variable.Step 3: Evaluate other options
Manual updates are error-prone, global variables without updates cause failures, and hardcoding tokens is insecure and inflexible.Final Answer:
Use a pre-request script in all requests to check token expiry and request a new token if expired, then update the environment variable -> Option AQuick Check:
Automate token refresh with pre-request scripts [OK]
- Relying on manual token updates
- Using global variables without refresh logic
- Hardcoding tokens in requests
Ready to prove it?
Pass the test and this pattern moves unknown
