# Monitor does not see variable's initial value got via Postman API

**URL:** <https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814>\
**Category:** Help Hub\
**Tags:** variables, authentication, environments, monitors, postman-api\
**Created:** [January 4, 2021, 10:52am UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814 "2021-01-04T10:52:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrycjaBalicka](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PatrycjaBalicka](https://community.postman.com/u/PatrycjaBalicka)\
**Post date:** [January 4, 2021, 10:52am UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/1 "2021-01-04T10:52:06Z")

</div>

Hello,

My question is, is there a way to reach the environment variable initial value for a monitor call, possibly without using up any more API calls inside each request? Or is there a better way for me to solve my issue, like using a text file?

Some background:  
I monitor a collection and all the requests inside use the same OAuth 2.0 token. Right now each call runs the same pre-request script to generate a token and then sends the request which “eats up” 2 monitor calls on 1 request - it works but uses precious calls unnecessarily.

To save them up, I want to generate a token once and use it for all the requests in the monitor run - so I created a separate request to generate a token and save it as an environment variable using Postman API. However, the next requests see only the current value of the variable, which is empty as expected.

What I tried so far with no success:

- unsetting and setting the same variable - works OK for collection runner but not for monitor.
- saving the token in local, global and environment variable.
- looking up similar problems in this forum.

Thank you in advance for help and tips.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 4, 2021, 12:33pm UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/2 "2021-01-04T12:33:12Z")

</div>

Would you mind sending a screenshot of your current implementation? This is definitely something that can be done, I’m curious to see what your setup is.

I outline how to do this via a pre-request script (run once, don’t run again until JWT is expired) in this post:

> [@Medium Article: How to Automate OAuth2 Token Renewal in Postman](https://community.postman.com/t/medium-article-how-to-automate-oauth2-token-renewal-in-postman/6630):
>
> Hey everyone, I have seen on a number of forums and stack overflow articles people expressing their frustration with the manual process Postman has for renewing OAuth2 tokens when they expire. I thought I would share a write up I did to help you get past the manual process and have your tokens renewing automatically. Let me know what you think!

---

<div class="post-metadata">

**Author:** ![PatrycjaBalicka](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PatrycjaBalicka](https://community.postman.com/u/PatrycjaBalicka)\
**Post date:** [January 4, 2021, 1:28pm UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/3 "2021-01-04T13:28:26Z")

</div>

Sure! Right now I am working on a simpler collection, kind of a proof of concept:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/3/83c556c698db4408609d243e817dc74f680bb670.png)  
The to-be collection has 20+ GET requests. Tests are trivial, checking only if the status is 200 since it is only a health check.  
Here is the PUT request, to update the selected environment:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/f/2f2ff3023d9a7d34f95d067ee429db188911c97b.png)

Its pre-request script is adapted from [This gist shows how using the pre-request script in Postman, a new Oauth-2 token can be obtained using the a refresh token · GitHub](https://gist.github.com/harryi3t/dd5c61451206047db70710ff6174c3c1) and generates a new token each time, works good for me so far (but I would definitely need to study the article you provided, I only had a glance before).

The most important part is:  
pm.sendRequest(getTokenRequest, (err, response) =\> {  
let jsonResponse = response.json(),  
newAccessToken = jsonResponse.access\_token;  
console.log({ err, jsonResponse, newAccessToken })  
pm.environment.unset(‘accessToken’);  
pm.environment.set(‘accessToken’, newAccessToken);  
});

I tried to use newAccessToken only but then the request updated me only with literal “{{newAccessToken}}”. Now, I would have only a current value in the environment.

In the request body, I reuse this accessToken value like this:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/5/95931b51f9ba6eccdbbade7dfdea77f18f1b3d1a.png)  
(I need to repost all the other env variables or else they are unset/deleted but I can live with it for now).

I send this request, get 200 and the updated environment variable looks like this:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/8/58269dda21b103a6e42f70887f9debb049bc7d5f.png)  
The initial and current value are now the same, I checked.

The next request reuses the accessToken variable:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/c/9cb4eb41468b411e72460a91186074c843918e12.png)  
But it returns expected 200 only when current value is not empty, which is not a thing when running in monitor mode. Initial value is ignored and can contain anything.

Now I am wondering if I saved the token as a global variable and use this global variable in the request body, would Postman consider this updated env variable “untouched” and then get the initial value instead? … I will check it and let know here how it worked.

Also, thank you for the article, I need to consider it when this flow does not work out 🙂

---

<div class="post-metadata">

**Author:** ![PatrycjaBalicka](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PatrycjaBalicka](https://community.postman.com/u/PatrycjaBalicka)\
**Post date:** [January 4, 2021, 1:40pm UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/4 "2021-01-04T13:40:07Z")

</div>

Just checked this:

> Now I am wondering if I saved the token as a global variable and use this global variable in the request body, would Postman consider this updated env variable “untouched” and then get the initial value instead? … I will check it and let know here how it worked.

Now the environment variable has only the initial value, no current value, and the next request gets 401 - both sent as a collection and monitor call.

---

<div class="post-metadata">

**Author:** ![justyna.pietrzynska](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@justyna.pietrzynska](https://community.postman.com/u/justyna.pietrzynska)\
**Post date:** [December 19, 2021, 9:36am UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/5 "2021-12-19T09:36:33Z")

</div>

Hi, I have the same problem. Have you found any solution on how to authorize requests in the monitor?

---

<div class="post-metadata">

**Author:** ![PatrycjaBalicka](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PatrycjaBalicka](https://community.postman.com/u/PatrycjaBalicka)\
**Post date:** [December 19, 2021, 2:16pm UTC](https://community.postman.com/t/monitor-does-not-see-variables-initial-value-got-via-postman-api/18814/6 "2021-12-19T14:16:05Z")

</div>

Hi Justyna, I can authorize the requests all the time, at least for client credentials token type, just wanted to make it more efficient. Frankly, I did not succeed with this optimization and we settled up with staying with the current solution.
