Postman is a great tool for testing and using APIs, in this article, I’ll show how you can use a Global Variable within Postman to store a value for reuse in other requests. I’m using an HPE Nimble Storage (Alletra Storage) array as a example for illustrating this.
The API requires that you login using a username and password to retrieve an Access Token, once you have this token (its only valid for a limited period of time, e.g. 30 minutes) you will require this to then make use of any of the methods/functions of the API, of course you could copy and paste this Access Token into each of your saved requests, but why not use a Global Variable to store this? Once you’ve obtained your token, its automatically ready in all our other requests you want to use, no copy and pasting required.
I’m assuming you know at least something about Postman and what a collection is. We’re using HPE Nimble Storage (Alletra), so your actual API configuration may be different, but the principles are the same when it comes to the use of Variables.
Get Access Token Request
So first we’ll create a request called “ServerName – Get Token” and make this a “POST” request.
First set POST, then put in the URL to the API endpoint, for example in my case it was the below, however in your case it will likely be different so consult your API documentation for full details.
https://servername.domain.com:5392/v1/token
Move onto the tabs, you can leave the Params, Authorisation and Headers details empty, we don’t need these.
However this API requires that you send the username and password within the Body as a Raw item as follows:

Now move to the Scripts tab, this is where the magic happens, under a “Post-Response” script add the following code:
// Parse the response body
let response = pm.response.json();
// Extract the session_token from the JSON
let sessionToken = response.data.session_token;
// Set it as a global variable
pm.globals.set("session_token", sessionToken);
// Optionally, log it to the Postman console
console.log("Session token saved as global variable:", sessionToken);
What this code does is once the request has succeeded, it extracts the “session_token” from the returned JSON (in this case) and then adds it to a Global Variable called “session_token”.
If you run (Send) this request, you’ll now have stored the current Session Token in the session_token Global Variable. If you want to see this variable click on “Environments”, “Globals”, then you’ll see any Variables and their current values.

Use Variable in a Real Request
OK, so now we have obtained our Session Token, let’s make use of it.
We create another request, but this time a GET request, then put in the URL to the API endpoint, for example in my case it was the below, however in your case it will likely be different so consult your API documentation for full details.
https://servername.domain.com:5392/v1/arrays/detail
Leave Params and Authorisation blank, we don’t need to use these, but we do need to add Key:Value into the Headers tab, we’ll be adding the “X-Auth-Token”, key which will have a Value that refers to the Global Variable to retrieve the value of the session_token.
You’ll therefore add a Key and Value as shown below, where the Value contains {{session_token}} which will be filled by its Global Variable value.

And in this case, that is it, just click on “Send”, and you should find that the connection is successful as shown below.

If you want to see the value within the variable, you can just hover over the {{session_token}} with your mouse and see its value.
