Automatically Set Access Tokens in Postman After Login (No More Copy-Paste!)
If you're working with APIs that use access tokens (like JWTs), you've probably dealt with the annoying routine:
- Send a
POST /loginrequest - Copy the access token from the response
- Paste it into the
Authorizationheader of every other request
π© Repeating this every time the token expires gets old fast.
π What if you could automate it?
This guide shows you how to extract the token automatically after login and set it as a dynamic environment variable in Postman. Then you can reuse it in any request, no manual copying needed!
π¦ Step 1: Create or Select a Postman Environment
- Click
New Environmentin the top right of Postman - Add a new environment (e.g.,
Local API) - Add a variable called
accessToken, set the type tosecret, and leave theinitial valueempty
π§ͺ Step 2: Add a Script to Your Login Request
In your POST /api/v1/auth/login request, go to the Scripts section, then select the Post-response tab and add this:
const res = pm.response.json();
if (res.success && res.data && res.data.accessToken) {
pm.environment.set("accessToken", res.data.accessToken);
console.log("β
accessToken saved to environment");
} else {
console.log("β Failed to extract accessToken");
}
π¬ What does this script do?
- It parses the JSON response returned from your login request.
- It checks if the response contains a valid
accessToken. - If so, it stores the token in your environment as
accessToken, making it available across all your other API requests. - Youβll see a confirmation in the Postman console.
You can also save more fields if needed:
pm.environment.set("refreshToken", res.data.refreshToken);
pm.environment.set("userId", res.data.user.id);
π§· Step 3: Use the Token in Other Requests
Once the accessToken is saved as an environment variable, you can inject it automatically into your other requests.
β Option 1: Use the Authorization Tab
- In your API request, go to the Authorization tab
- Select Bearer Token for the Auth Type
- Set the token to
{{accessToken}}
β Option 2: Set the Header Manually
Instead of using the Authorization tab, you can manually add a header:
Authorization: Bearer {{accessToken}}
Thatβs it! Now your token will automatically update after each login and be used wherever {{accessToken}} is referenced.
π Bonus: Automate Token Refresh (Optional)
If your API provides a refreshToken flow (e.g., POST /refresh-token), you can automate that too!
Create a separate request for refreshing the token, and add a Post-response script:
const res = pm.response.json();
if (res.newAccessToken) {
pm.environment.set("accessToken", res.newAccessToken);
console.log("π accessToken refreshed and updated");
}
Then you can call this request manually or as a pre-request step if a 401 is detected.
π§ Summary
Hereβs what youβve accomplished:
β
Automatically saved your accessToken from the login response
β
Injected the token into future requests using dynamic variables
β
Removed the need for manually copying and pasting tokens
β
Optionally prepared for refresh token logic
π Conclusion
This small but powerful automation trick can save you a lot of time, especially when working with authenticated APIs during development or testing.
No more expired tokens or constant back-and-forth between tabs!
Let me know if youβd like a follow-up guide on chaining token refresh logic or fully automating session flows in Postman and Iβd love to hear how youβre using Postman in your workflow.
Happy testing! π
Top comments (0)