How to add Bearer Token to your Postman collection

Khemlall Mangal
2 min readApr 2, 2024

Postman is pretty popular today with API testing and its getting better. A few thigns you would face is how to pass the bearer token to the rest of your request without having to authenticating each time or coping and pasting your expired token every time. Here is how you achieve this.

Create Collection Variables: First, you need to create a collection variable to store the bearer token. Go to your collection and click on the “…” (three dots) menu, then select “Edit”. In the “Variables” tab, add a variable named bearer_token with no initial value.

Extract the Token: Since the token is contained within the access_token field of the response JSON, you can extract it using a script in the "Tests" tab of your token request. Here's how you can do it:

Go to your Generate Token Request and under test tab add the following code:

var jsonData = pm.response.json();
pm.collectionVariables.set("bearer_token", jsonData.access_token);
  1. This script will extract the access token from the response and store it in a collection variable named bearer_token.
  2. Use the Token in Other Requests: For each subsequent request where you need to use the bearer token, you can reference the collection variable. Modify the authorization header of those requests to use the token variable.
  3. In the “Headers” tab of your request, set up the Authorization header like this:
Key: Authorization
Value: Bearer {{bearer_token}}
  1. Postman will automatically replace {{bearer_token}} with the value stored in the collection variable.
  2. Run the Collection: When you run the collection, Postman will execute the token request first, extract the token, and store it in the collection variable. Then, it will automatically use that token in the subsequent requests where needed.

By following these steps, you should be able to successfully extract the bearer token from the response and use it in other requests within your Postman collection.

Hope this helps someone in the future…

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.