Set the bearer token automatically to each request in the postman

Ravindra Devrani
CodeX
Published in
3 min readSep 22, 2024
Set bearer token automatically to each request in postman

When we work with postman to test the endpoints, and those endpoints are authorized, each time (until it expires) we need to pass the token in the authorization header. To get the token, we need to call the login or authentication API . This process feels quite irritating , because we programmers hates this manual working. We always want an easy and automatic workflow. Let’s see how can we achieve this with postman.

Option 1: Using environment variable

Creating the environment variable

First and foremost we need to create an environment.

001
1.1
  • Select the Environments tab in the left side.
  • Click on the plus icon, as shown in diagram 1.1 , create a new environment and give it a name.
  • Create a new variable and name it token.
1.2
1.2
  • Select the environment , you have recently created from the dropdown at the top right corner, as shown in diagram 1.2.

Setting the environment variable through the script

1.3
  • Go to to login API request
  • Select the script section
  • Select Post-response tab.
  • Enter the script shown in diagram 1.3. Since I am getting a token as text so I am using pm.response.text() . If you are getting the json as response, parse it accordingly.

Using the environment variable in the authorization header

1.4
1.4
  • In the protected api, go to the authorization section.
  • Select Bearer Token as Auth Type and set its value to {{token}}. Now you can send the request.

Option 2: Using Collection variable

This option is very much similar to the previous one. We will be using collection variable instead of environment variable. But this option is only application for the collection. If you don’t use collection don’t read further.

Creating a collection variable

2.1
  • Double click on the collection.
  • You will see a Variables section as shown in the diagram 2.1. Click on the variables.
  • Create a new collection variable and name it token.

Setting the collection variable through script

2.2
2.2
  • Go to the login API request
  • Select the script section
  • Select Post-response tab.
  • Enter the script shown in diagram 1.3. Since I am getting a token as text so I am using pm.response.text() . If you are getting the json as response, parse it accordingly.

If you close the collection and re-open it. You can see the current value of the token as shown below.

2.2

Using the collection variable

2.4
  • In the protected api, go to the authorization section.
  • Select Bearer Token as Auth Type and set its value to {{token}}. Now you can send the request.

Please consider to clap and share if this post helped you.

Connect with me

👉 YouTube
👉 Twitter
👉 GitHub

Become a supporter 🍵

--

--