{"id":11766,"date":"2025-10-19T14:23:46","date_gmt":"2025-10-19T14:23:46","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11766"},"modified":"2025-10-19T15:08:22","modified_gmt":"2025-10-19T15:08:22","slug":"creating-my-auth-token-for-use-in-postman","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-my-auth-token-for-use-in-postman\/","title":{"rendered":"Creating my auth token for use in Postman"},"content":{"rendered":"<p>I&#8217;ve a simple set of calls to my application&#8217;s endpoints and occasionally use Postman to test them or to simply call and see what the results look like. However my calls all require authentication tokens.<\/p>\n<p>The aim here is that when I require a authentication token I&#8217;ll call a local app which gets them for me and I want Postman to call my code to retrieve as <em>accessToken<\/em> which can be used by Postman for subsequent calls<\/p>\n<p>Let&#8217;s set up Postman to use a variable named <em>accessToken<\/em>&#8230;<\/p>\n<ul>\n<li>Create a &#8220;Environments&#8221; environment or use Globals<\/li>\n<li>Add a variable named accessToken (you can name yours whatever you want). Do not supply initial or current values and leave types as default<\/li>\n<li>Go to your request and in the Authorization tab, select the auth method. I chose Bearer Token as that&#8217;s what my endpoint uses.<\/li>\n<li>In the token type {{accessToken}}<\/li>\n<\/ul>\n<p>At this point we have a link between the variable <em>accessToken<\/em> and the value sent into the Bearer Token but we need to generate the token and set it&#8217;s value into the variable <em>accessToken<\/em>.<\/p>\n<ul>\n<li>Select the Scripts tab and Pre-request<\/li>\n<li>Add the following\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntry {\r\n  const response = await pm.sendRequest({\r\n    url: &quot;https:\/\/localhost:5000\/gettoken&quot;,\r\n    method: &quot;GET&quot;\r\n  });\r\n\r\n  pm.environment.set(&quot;accessToken&quot;, response.text());\r\n} catch (err) {\r\n  console.error(err);\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Obviously the URL in the above script should be whatever your server is and in my case I return raw text (you could have it deserialize from JSON as well ofcourse).<\/p>\n<p>That&#8217;s it &#8211; Send the request,  Postman calls your service to get the token and assigns it to <em>accessToken<\/em> and your Postman request should be authenticated.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve a simple set of calls to my application&#8217;s endpoints and occasionally use Postman to test them or to simply call and see what the results look like. However my calls all require authentication tokens. The aim here is that when I require a authentication token I&#8217;ll call a local app which gets them for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[763],"tags":[],"class_list":["post-11766","post","type-post","status-publish","format-standard","hentry","category-postman"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11766"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11766\/revisions"}],"predecessor-version":[{"id":11960,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11766\/revisions\/11960"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}