# Passing environment variable between monitor runs

**URL:** <https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194>\
**Category:** Help Hub\
**Tags:** monitors, variables\
**Created:** [November 6, 2018, 7:46am UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194 "2018-11-06T07:46:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/victor/32/625_2.png) [@Victor](https://community.postman.com/u/Victor)\
**Post date:** [November 6, 2018, 7:46am UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194/1 "2018-11-06T07:46:13Z")

</div>

Hello,

I am a bit puzzled about how the environment variables are preserved between the monitor runs. So, I have a collection that contains a chain of requests, collects the JSON responses and stores some of the obtained IDs as environment variables at the end of the “Tests” stage of the requests:

```
pm.environment.unset("purchasedOrderId");
pm.environment.set("purchasedOrderId", jsonData.id);

```

Passing the environment variables works just fine within a single collection run performed by a Postman monitor. However, when I try to get environment variable value from the last run, I get the **initial** value stored into the environment configuration, just as every monitor run would start from scratch. That makes the precautions “if it failed last time, cancel last flow before continue any further” useless.

Am I missing something obvious here? Could that be a side effect of the fact that the environments are shared within the team?

Any ideas or suggestions are very much welcome!

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [November 7, 2018, 5:56pm UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194/2 "2018-11-07T17:56:55Z")

</div>

This is one of [the differences when running collections in a monitor vs. in the collection runner](https://www.getpostman.com/docs/v6/postman/monitors/intro_monitors#running-collections-in-a-monitor-vs-the-postman-app-collection-runner). Global and environment variables are NOT persisted across collection runs **using a monitor** the same way they can be persisted across collection runs **in the collection runner**.

To pass updated environment variables across monitor runs, you can add another call to your collection to update the environment in use. In other words, submit a `PUT` request using the [Postman API](https://docs.api.getpostman.com/) to update the environment.

---

<div class="post-metadata">

**Author:** ![Arun\_Saxena](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Arun\_Saxena](https://community.postman.com/u/Arun_Saxena)\
**Post date:** [October 2, 2019, 12:42pm UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194/3 "2019-10-02T12:42:45Z")

</div>

Hi,  
I notice that you said env vars are persisted (if the setting is on) across collection runs. I am trying to do that to persist JWT Tokens in env vars. Not happening.

I set the env var as:  
pm.environment.set(‘accessTokenLearner’, jsonData.accessToken);

it works within the collection run but not across it.

---

<div class="post-metadata">

**Author:** ![Arun\_Saxena](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Arun\_Saxena](https://community.postman.com/u/Arun_Saxena)\
**Post date:** [October 2, 2019, 12:48pm UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194/4 "2019-10-02T12:48:20Z")

</div>

Oh btw I just got it. Didn’t notice the check box while starting the run. Thanks. Great product.

---

<div class="post-metadata">

**Author:** ![brambarnard](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@brambarnard](https://community.postman.com/u/brambarnard)\
**Post date:** [July 29, 2022, 7:24am UTC](https://community.postman.com/t/passing-environment-variable-between-monitor-runs/3194/6 "2022-07-29T07:24:44Z")

</div>

Hey Joyce,

Thanks for clarifying this.  
I did try to use the POSTMAN API:  
-I created a fork op Postman API  
-I called the PUT Update Environment.

BUT this replaces the whole set of variables which is not what I expect.  
We have a lot of predefined variables and we simply need to update / add 1.

How can we do this?

My request looks like this:

[https://api.getpostman.com/environments/](https://api.getpostman.com/environments/)[MY GUID]  
{  
“environment”: {  
“name”: “HIP TEST”,  
“values”: [  
{  
“key”: “ab”,  
“value”: “cd”  
}  
]  
}  
}

Thanks!  
Bram
