# Retrieve token from response body and save it to env variable

**URL:** <https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380>\
**Category:** Help Hub\
**Created:** [September 30, 2020, 8:44pm UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380 "2020-09-30T20:44:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralexSD7](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@ralexSD7](https://community.postman.com/u/ralexSD7)\
**Post date:** [September 30, 2020, 8:44pm UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380/1 "2020-09-30T20:44:06Z")

</div>

I am trying to save a token after a requests is made so I can refresh the env token variable each run. How do I save the token from the responses body into a env variable for use on other requests. I have this so far.

 ![Screen Shot 2020-09-30 at 4.22.31 PM](https://us1.discourse-cdn.com/getpostman/original/3X/e/6/e6dab32fce845bfe784b42d7deaed1c7c1c8ecef.png)

---

<div class="post-metadata">

**Author:** ![jeevananthank](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jeevananthank/32/4075_2.png) [@jeevananthank](https://community.postman.com/u/jeevananthank)\
**Post date:** [October 1, 2020, 6:50am UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380/2 "2020-10-01T06:50:59Z")

</div>

use the below code as your test script

```auto
const responseJson = pm.response.json();
var bodyToken = responseJson.access_token;
pm.environment.set("bodyToken", bodyToken)

```

---

<div class="post-metadata">

**Author:** ![ralexSD7](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@ralexSD7](https://community.postman.com/u/ralexSD7)\
**Post date:** [October 1, 2020, 1:35pm UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380/3 "2020-10-01T13:35:46Z")

</div>

That worked beautifully, thank you!

---

<div class="post-metadata">

**Author:** ![gpub1](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@gpub1](https://community.postman.com/u/gpub1)\
**Post date:** [October 1, 2020, 6:27pm UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380/4 "2020-10-01T18:27:07Z")

</div>

Once setting the variable, You can further get it with  
`pm.variables.get("bodyToken");`  
use it for further request for chaining

---

<div class="post-metadata">

**Author:** ![ofemjoseph1](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@ofemjoseph1](https://community.postman.com/u/ofemjoseph1)\
**Post date:** [July 31, 2022, 10:40am UTC](https://community.postman.com/t/retrieve-token-from-response-body-and-save-it-to-env-variable/16380/5 "2022-07-31T10:40:38Z")

</div>

This worked perfectly
