DEV Community

Cover image for Automatically Set Access Tokens in Postman After Login (No More Copy-Paste)
Khaled Saeed
Khaled Saeed

Posted on

Automatically Set Access Tokens in Postman After Login (No More Copy-Paste)

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:

  1. Send a POST /login request
  2. Copy the access token from the response
  3. Paste it into the Authorization header 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

  1. Click New Environment in the top right of Postman
  2. Add a new environment (e.g., Local API)
  3. Add a variable called accessToken, set the type to secret, and leave the initial value empty

๐Ÿงช 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");
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฌ 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);
Enter fullscreen mode Exit fullscreen mode

๐Ÿงท 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

  1. In your API request, go to the Authorization tab
  2. Select Bearer Token for the Auth Type
  3. 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}}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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)