Can i automatically update collection token once i've authorized in my project?

I’ve made a project in mern and used postman to test my api’s.
in each main route, i’ve created collection that contains many api calls.
im going to use jwt bearer created from jsonwebtoken in my project,
is it possible to update the header token automatically, instead of updating it manually over each request, each time i have a new token to test out ?
if so, how can i do that ?

thank you and have a nice week

Hi @research-engineer-89 !

Great question and welcome to the community! :wave:

Absolutely, automating token updates can be a real time-saver! With Postman, you can streamline this by using environment or collection variables. Here’s a quick guide:

  1. Capture the JWT Token: After a login/signup API call, add a script in the “Tests” tab to set the JWT token to an environment or collection variable:
var jsonData = pm.response.json();
pm.environment.set('jwtToken', jsonData.token);
  1. Use the Variable in Headers: In your subsequent requests, use the stored token variable in the headers:
Authorization: Bearer {{jwtToken}}
  1. Automate Token Renewal: If your token expires after a certain period, you might have a refresh mechanism in place. If so, you can create a pre-request script (in the collection or just for specific requests) to check if the token is about to expire and then refresh it.

By following these steps, every time you obtain a new JWT token through your login/signup API call, it will automatically update in all the requests where you’ve used the {{jwtToken}} variable.

Hope this helps! Feel free to ask if you have any more questions :slight_smile:

1 Like

do i need to add each request the auth header and the global variable manually after using the script ?

Hello guys I am here again to need your guys help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.