Set Bearer Token as Environment Variable in Postman for All APIs
This article is all about setting up postman configuration for Authentication token as global variable across all api end points.
Create a Environment in Postman
1.Manage Environment
Run postman and go to the manage environment setting tab as shown in following image.
Please refer red color rectangle box.
2. Create New Environment
Then click on Add button to create another custom environment.
3. Environment Details
Once you click on Add button a new window is popped up where you can create a new environment. Enter a environment name in Add Environment text box and enter variable names and initial values. Our environment name is ReportingAPI and have two variable representing localBasedUrl and authToken
4. Create Authentication API
Create a /auth api where authentication http request is generated. At the same time select your custom environment from upper right corner drop down so that custom environment variables which are defined in previous step can be accessed. Please refer below screenshot.
Set Headers
Since authentication request the content-type should be application/json.
Setting up post values
Here we are setting username and password parameters as json format because our content type is json.
Set up Token value to Environment variable
This is the place where we catch the token and store it in environment parameter called authToken. Please note that the token is available in response header but not in response body and the token header name is Token. That is why we use pm.response.headers.get(“Token”) statement.
Test Process
Once you set up everything properly click on send button. Then you will be able to see the token value is properly store in authToken environment variable. Please click on eye button which is prior to setting button so that the environment variable can be seen.
5. Use Token in other API
Create API end point which uses token to communicate the back end. Refer following screenshot.
That’s all you have to do. Then once you call the /auth request the token is available for all other API end points that are in same environment.
Thank You All !!!!