# How to automatically set OAuth 2.0 Access Token as variable

**URL:** <https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295>\
**Category:** Help Hub\
**Tags:** authentication, oauth2\
**Created:** [February 7, 2022, 11:20am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295 "2022-02-07T11:20:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrabetr](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mrabetr/32/11062_2.png) [@mrabetr](https://community.postman.com/u/mrabetr)\
**Post date:** [February 7, 2022, 11:20am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/1 "2022-02-07T11:20:34Z")

</div>

I have set up OAuth 2.0 Authorization with Authorization Code flow (i.e. user needs to authenticate) for a collection and i would like to automatically set the response, which includes access token, refresh token and other properties, as variables. Currently i am having to copy things manually from the UI.

after clicking on Get New Access Token

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/d/d2bf278ee4b8efc374376eaa412d6450b45fbde9.png)

I get the access token in the UI but i also want to be able to automatically save the tokens into variables

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/1/1e66df78ac3bd452590e7cf26ecdf6066e27ecab.png)

If i go to the console i can see the json response so i would like to find a way to access that response and save the tokens please

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/6360cb98feb1fabb22c859be2e6cf9b940f101a1.png)

Since I am using Authorization Code flow, it requires a user to sign in so it won’t be possible to just have a post request in the pre-script, which works if it’s a Client Credentials flow

thanks

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 7, 2022, 10:09pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/2 "2022-02-07T22:09:17Z")

</div>

Hey there! First, I’d like to welcome you to the Postman community and congrats on your first post! 🎊

So currently we are VERY close to getting auto update/refresh of OAuth2 access tokens feature [implemented](https://github.com/postmanlabs/postman-app-support/issues/10112).

As of now, the only way to do this is as you described, generate an access token, copy-paste it into a variable and then use that variable under the access token field.

There is also an excellent post by @allenheltondev about a workaround for token renewal that uses pre-request scripts:

> **[How to Automate OAuth2 Token Renewal in Postman](https://allenheltondev.medium.com/how-to-automate-oauth2-token-renewal-in-postman-864420d381a0)**
>
> Discover how to automatically renew your OAuth2 access token in Postman so you never have to click the “Get New Access Token” button again.

Hope this helps! 😄

---

<div class="post-metadata">

**Author:** ![mrabetr](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mrabetr/32/11062_2.png) [@mrabetr](https://community.postman.com/u/mrabetr)\
**Post date:** [February 7, 2022, 11:37pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/3 "2022-02-07T23:37:01Z")

</div>

ok thanks for letting me know. Looking forward to the new feature being deployed!

---

<div class="post-metadata">

**Author:** ![4aemarco](https://avatars.discourse-cdn.com/v4/letter/4/5e9695/32.png) [@4aemarco](https://community.postman.com/u/4aemarco)\
**Post date:** [October 7, 2022, 7:15pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/4 "2022-10-07T19:15:55Z")

</div>

Had the same question, and found a nice workaround…

In the request tab, where you use the oauth flow, the token will be saved when you click “Use token”…  
Therefor in the same tab, I setup a request, and added following pre request script…

var token = pm.request.auth.oauth2.get(‘accessToken’);  
pm.environment.set(“accessToken”, token);

Means, after clicking on “Use token”, I just fire the request once, then the token will be in the variable for other tabs to consume.

---

<div class="post-metadata">

**Author:** ![latha7](https://avatars.discourse-cdn.com/v4/letter/l/df705f/32.png) [@latha7](https://community.postman.com/u/latha7)\
**Post date:** [February 10, 2023, 9:56am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/5 "2023-02-10T09:56:52Z")

</div>

I tried your solution. But it doesn’t save it in Environment variables. Can you elaborate your solution? Maybe sample screenshot.

---

<div class="post-metadata">

**Author:** ![4aemarco](https://avatars.discourse-cdn.com/v4/letter/4/5e9695/32.png) [@4aemarco](https://community.postman.com/u/4aemarco)\
**Post date:** [February 26, 2023, 7:53pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/6 "2023-02-26T19:53:52Z")

</div>

First of all, I have a global variable called “token”

Now, I have a request where I setup OAuth to get a token…

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/0/30a0a781ed5fdc749c98559f8f86269fc5c0692f.png)

Browser opens for login…  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/b/4b1ebf91c3f3585b26345675a673434a2818d8b9.png)

Login in Browser is complete… Postman showing success, Click Proceed  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/e/dec51aaba14cda1cf246d523c12e0dccd2bd37a8.png)

see next post

---

<div class="post-metadata">

**Author:** ![4aemarco](https://avatars.discourse-cdn.com/v4/letter/4/5e9695/32.png) [@4aemarco](https://community.postman.com/u/4aemarco)\
**Post date:** [February 26, 2023, 7:54pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/7 "2023-02-26T19:54:38Z")

</div>

I get a management screen showing previous tokens, Click Use token

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/0/304d99214e1b68a3fde4ce8b907aced8e04fb99e.png)

Postman showing “Token added”… to the tab as per my understanding.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/2/a224149335480f26a175fe4d710175c548e57f9b.png)

Now the tricky part. In this tab, I also have the Pre-request Script defined, to set the global variable before request…

… I Send a request, which actually don´t matter.  
What matters is, that the script does write the token from the request, to my global variable.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/8/28c2f937e9113d8692c831d251f8ad824baf5407.png)

continue in next post…

---

<div class="post-metadata">

**Author:** ![4aemarco](https://avatars.discourse-cdn.com/v4/letter/4/5e9695/32.png) [@4aemarco](https://community.postman.com/u/4aemarco)\
**Post date:** [February 26, 2023, 7:55pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/8 "2023-02-26T19:55:13Z")

</div>

At another spot (a folder with many reuests in my case)… I use the global variable as “Bearer token”

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/2/d2bd2a3451da6c95315addcc90e76a4e59d490a6.png)

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![hkt05](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@hkt05](https://community.postman.com/u/hkt05)\
**Post date:** [September 21, 2023, 4:50am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/9 "2023-09-21T04:50:48Z")

</div>

I got the same scenario and your solution works for me. Thank you.  
BTW, do you know how to get refresh\_token from oauth2.get() ? I tried oauth2.get(“refreshToken”) but it returned NULL.  
Thanks

---

<div class="post-metadata">

**Author:** ![aerospace-technolog1](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aerospace-technolog1](https://community.postman.com/u/aerospace-technolog1)\
**Post date:** [November 17, 2023, 7:12pm UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/10 "2023-11-17T19:12:51Z")

</div>

Is there an update on this functionality? Or is there a different work around? It seems like this would be a common use case to automatically populate a variable when authenticating.

I’ve tried the solution listed above but i get an error when trying to access pm.request.auth.oauth2.get(‘accessToken’).

If I log the pm.request.auth.oauth2 it’s undefined.

---

<div class="post-metadata">

**Author:** ![drobin-lacoste](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@drobin-lacoste](https://community.postman.com/u/drobin-lacoste)\
**Post date:** [November 24, 2023, 10:39am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/11 "2023-11-24T10:39:25Z")

</div>

Same need here, it’s nonsense to have to create a dummy request in Postman just to manually save the token in an environment/collection variable…  
I’m using it in a pre-request script, where I need to fetch data before triggering the main request.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/f/6f518622b9f45dc31432da28aeac4fe24abd7a99.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/6/46107f4fc9329ac0970eda3eb30d362a504a7a1f.png)

---

<div class="post-metadata">

**Author:** ![satellite-participa7](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/satellite-participa7/32/28027_2.png) [@satellite-participa7](https://community.postman.com/u/satellite-participa7)\
**Post date:** [April 11, 2024, 10:02am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/12 "2024-04-11T10:02:52Z")

</div>

I have managed to created three tokens (DEV, STAGING, PROD) :

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/6/7682ab876368f4931469fdb48a2d6d7da4089d8b.png)

But how to invoke them (the tokens) dynamically when I change the testing environment (I have also three env: DEV, STAGING, PROD in Postman).

It looks like that these token names are static : here is an example : I choose the testing env “01-PROD” on the top right but the token that been used is still “TOKEN DEV” !

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/1/015824e88f90b5591a20bbd7857129f265988e54.png)

---

<div class="post-metadata">

**Author:** ![justjimlego](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/justjimlego/32/33459_2.png) [@justjimlego](https://community.postman.com/u/justjimlego)\
**Post date:** [September 5, 2025, 8:45am UTC](https://community.postman.com/t/how-to-automatically-set-oauth-2-0-access-token-as-variable/32295/13 "2025-09-05T08:45:21Z")

</div>

I have the same problem as @Pierre, I have all the token created for the various environments, but there is no way to automatically change the token when an environment is selected! Is there a plan to add this feature?
