How to Set Bearer Token as Environment Variable in Postman
This tutorial is for How to Set Bearer Token as Environment Variable in Postman. All APIs in the same Bearer token are used so we can store the Bearer token in a variable and it used Environment Variable. Postman Authentication token as a global variable across all APIs. we cover dynamically changing all API tokens.
Create an Environment
Manage Environment
Open postman and go to the managed environment setting as shown in the following screenshot.
Please click the Manage Environment button on the red color rectangle box.
Create New Environment
Then click on Add button in the right side bottom corner to create another custom environment.
Create environment details
Enter an environment name in Add Environment text box and fill in up environment details.
Create Authentication API
Now create a /login API where an authentication request is generated. so first select your custom environment (Devnote API) from the upper right corner dropdown. Please check the below screenshot.
Set Url and Headers
Mostly use authentication requests the content-type should be application/json.
Set post values
We are setting username and password parameters in json format.
Set Token value to Environment variable
We catch the token and store it in an environment parameter called authToken. We use postman.setEnvironmentVariable("bearer_token", jsonData.data.token)
statement.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("bearer_token",jsonData.data.access_token);
Test
Now you set the proper value and click on the send button. Then you will see the token value is properly stored in the bearer_token environment variable. Check properly set bearer_token so click on the eye button which is prior to setting the button.
Use Token in other API
Create a new API which uses tokens to communicate the back end.