Eric Rybarczyk

Eric Rybarczyk

Enthusiastic and motivated software engineer with diverse experience

AWS & Java Certified Developer

Save JWT in a Postman Environment Variable

Automatically save your updated bearer token for use in other API calls

Eric Rybarczyk

Postman IDE Screenshot

When using Postman to access or test API endpoints that require authentication, such as a JWT or Bearer Token, you will quickly grow tired of manually updating the token value in multiple Postman request tabs, particularly if it expires in a short time period. Fortunately it is simple to automate this process, and this quick tip will show you how.

If you have not defined an Environment, start by creating one from the Environments tab in the left-side navigation in Postman. An environment is essentially a container for variables, and I typically use them to represent the target system I’m connecting to, such as a local endpoint or a QA server.

postman create environment

Next, define a variable in the Postman environment. You do not need to assign an initial value, though doing so may be useful for other scenarios.

postman define variable

Now, find your saved Postman request which obtains the Token. And example is shown below.

postman sample request

Switch to the Tests tab, where we will add some JavaScript which will run after the request executes. Obviously we can write tests here, and we will. However, we can also use this area to store the received Token in the environment variable we defined earlier. You may need to adjust the identifiers based on the specific response for your particular authentication response, but the general approach will be the same as you see below.

const environmentToken = "bearerToken"; // the name of the environment variable
const newToken = pm.response.json().access_token;

// verify that we have a token value
pm.test("access token value should be present", function() {
  pm.expect(newToken.length > 0);
});

// save the new token in our environment variable
pm.environment.set(environmentToken, newToken);

// simple verification that the value was saved as intended
pm.test("Token environment variable should be updated", function() {
  const checkToken = pm.environment.get(environmentToken);
  pm.expect(checkToken).to.eql(newToken);
});

Below is an example of using the environment variable in the Authorization tab of a Postman request. The {{bearerToken}} is the Postman syntax to reference these variables, using the double-brace delimiters. By consistently using this environment variable in all your requests, you can easily update it by running the one request which obtains the token, and not have to manipulate it manually in any other saved requests.

postman environment variable usage

There is always more to learn, and you can read more about scripting in Postman and also review the Postman JavaScript reference material.


Article Series

Software Projects

Recent Posts

Categories

About

Enthusiastic and motivated software engineer