{"id":4550,"date":"2025-06-21T15:35:09","date_gmt":"2025-06-21T15:35:09","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=4550"},"modified":"2025-06-21T15:35:10","modified_gmt":"2025-06-21T15:35:10","slug":"postman-api-global-variables-example","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=4550","title":{"rendered":"Postman API &#8211; Global Variables Example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Postman is a great tool for testing and using APIs, in this article, I&#8217;ll show how you can use a Global Variable within Postman to store a value for reuse in other requests. I&#8217;m using an HPE Nimble Storage (Alletra Storage) array as a example for illustrating this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;ve obtained your token, its automatically ready in all our other requests you want to use, no copy and pasting required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;m assuming you know at least something about Postman and what a collection is. We&#8217;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. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get Access Token Request<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So first we&#8217;ll create a request called &#8220;ServerName &#8211; Get Token&#8221; and make this a &#8220;POST&#8221; request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>https:\/\/servername.domain.com:5392\/v1\/token<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Move onto the tabs, you can leave the Params, Authorisation and Headers details empty, we don&#8217;t need these.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However this API requires that you send the username and password within the Body as a Raw item as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"237\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image.png\" alt=\"\" class=\"wp-image-4552\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image.png 719w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-300x99.png 300w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now move to the Scripts tab, this is where the magic happens, under a &#8220;Post-Response&#8221; script add the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Parse the response body\nlet response = pm.response.json();\n\n\/\/ Extract the session_token from the JSON\nlet sessionToken = response.data.session_token;\n\n\/\/ Set it as a global variable\npm.globals.set(\"session_token\", sessionToken);\n\n\/\/ Optionally, log it to the Postman console\nconsole.log(\"Session token saved as global variable:\", sessionToken);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What this code does is once the request has succeeded, it extracts the &#8220;session_token&#8221; from the returned JSON (in this case) and then adds it to a Global Variable called &#8220;session_token&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you run (Send) this request, you&#8217;ll now have stored the current Session Token in the session_token Global Variable. If you want to see this variable click on &#8220;Environments&#8221;, &#8220;Globals&#8221;, then you&#8217;ll see any Variables and their current values.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"186\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1-1024x186.png\" alt=\"\" class=\"wp-image-4553\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1-1024x186.png 1024w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1-300x55.png 300w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1-768x140.png 768w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1-1536x279.png 1536w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-1.png 1649w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Use Variable in a Real Request<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">OK, so now we have obtained our Session Token, let&#8217;s make use of it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>https:\/\/servername.domain.com:5392\/v1\/arrays\/detail<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Leave Params and Authorisation blank, we don&#8217;t need to use these, but we do need to add Key:Value into the Headers tab, we&#8217;ll be adding the &#8220;X-Auth-Token&#8221;, key which will have a Value that refers to the Global Variable to retrieve the value of the session_token.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-2-1024x502.png\" alt=\"\" class=\"wp-image-4555\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-2-1024x502.png 1024w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-2-300x147.png 300w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-2-768x377.png 768w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-2.png 1105w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And in this case, that is it, just click on &#8220;Send&#8221;, and you should find that the connection is successful as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"809\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-3.png\" alt=\"\" class=\"wp-image-4557\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-3.png 896w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-3-300x271.png 300w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-3-768x693.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"264\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-4.png\" alt=\"\" class=\"wp-image-4558\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-4.png 902w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-4-300x88.png 300w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2025\/06\/image-4-768x225.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Postman is a great tool for testing and using APIs, in this article, I&#8217;ll show how you can use a Global Variable within Postman to store a value for reuse in other requests. I&#8217;m using an HPE Nimble Storage (Alletra Storage) array as a example for illustrating this. The API requires that you login using &#8230; <a title=\"Postman API &#8211; Global Variables Example\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=4550\" aria-label=\"Read more about Postman API &#8211; Global Variables Example\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4195,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,20],"tags":[],"class_list":["post-4550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nimble-storage","category-random"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4550"}],"version-history":[{"count":4,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4550\/revisions"}],"predecessor-version":[{"id":4559,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/4550\/revisions\/4559"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4195"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}