DEV Community

Cover image for 1 Tip to Double Your Productivity in Postman
Jordan Burroughs
Jordan Burroughs

Posted on • Edited on

1 Tip to Double Your Productivity in Postman

I've noticed when testing authenticated endpoints in Postman that every time my token expired, I needed to take a new token from my POST /authenticate endpoint and copy-and-paste it into each of my requests. This can quickly become a tedious process when your token's TTL is very short.

So I came across a quick-fix that has helped me save time by completing this process in just one click, and I wanted to share it.

The Solution

1) Start using environment variables!

Rather than explicitly entering the value into fields, take advantage of Postman's environment variables. There are different levels of variables (see here, but I like to keep my projects organized by creating different environments.

You can even set up for different environments (i.e. dev, sandbox, prod)

Add Environment

Add Variables

To add to a field, just surround the variable name with double curly brackets: {{token}}.

Add Variables to Fields

Now all of your variables live in one place.

2) Create a post-request script

Now you can set the environment variable using a post-request script. Under the endpoint tab where you get your token (usually a POST to an authentication endpoint), click the "Tests" tab.

Click on Tests tab

This is where you can do all kinds of things (check out the snippets on the right side). However, in this case, we are just setting an environment variable.

Add this script to the panel:

const token = pm.response.json().token;
pm.environment.set("token", token);
Enter fullscreen mode Exit fullscreen mode

Enter Script into Panel

Note: Your authentication endpoint response may have a different structure so make sure in Line 1 you are referencing the correct token property.

Now any time your token expires, you just call this endpoint and the token returned will be used everywhere it is referenced (i.e. all your protected endpoints).

Conclusion

I understand that this probably isn't the best way to do this. Please share a better solution in the comments! It would be extremely helpful for everyone.

Also, I know that I never use Postman to its full potential, so if you have any useful tips on Postman in general, please share those as well!


Thanks for reading! If you want more tech tips, software stuff, and bussin' blogs, you can throw me a follow on Twitter🔥🤘🏽🐶

Top comments (4)

Collapse
 
ssianik profile image
Syed Sirajul Islam Anik

Not flexing but I am doing this for years now. All my possible values are taken from variables.
Anyway, there there three types of environments in PM. local variable, global variable and collection variable. you may mention if you want to.

Collapse
 
jburroughs profile image
Jordan Burroughs

Appreciate it! Definitely gonna add that little note 🙂

Collapse
 
jonrandy profile image
Jon Randy 🎖️

This is wayyyyy easier in Insomnia

Collapse
 
jburroughs profile image
Jordan Burroughs

I'm not familiar with Insomnia. Definitely gotta check it out