# Fix or workaround for Collection Runner & variable issue

**URL:** <https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078>\
**Category:** Help Hub\
**Created:** [August 9, 2019, 2:57pm UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078 "2019-08-09T14:57:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![evroom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/evroom/32/2508_2.png) [@evroom](https://community.postman.com/u/evroom)\
**Post date:** [August 9, 2019, 2:57pm UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078/1 "2019-08-09T14:57:40Z")

</div>

Hi,

My first post here, so please don’t be upset when things are not clear or wrongly formatted 🙂

Description:

I am not able to set and get a variable in requests when using the Collection Runner.  
Those requests are outside the currently running Collection Runner.  
The Collection Runner only uses local/temporary variables and is only capable of saving variables, after the Collection Runner has finished (‘Keep variable values’), not during.

Background:

I run a Collection that retrieves an access\_token and a refresh\_token, and it should keep the REST session running.  
Every 60 minutes it needs to get a new access\_token (using the refresh\_token), that needs to be used by other REST calls (Authorization - Token: {{access\_token}}) .  
The other REST calls are in other Collections (run as single requests).

I ran extensive tests in order to find out that  
pm.variables.set()  
pm.environment.set()  
pm.globals.set()  
and their ‘get’ counterparts have no influence whatsoever on the environment Variables and Globals whilst the Collection Runner is active.

It would be nice if this could be fixed / enhanced or perhaps a workaround exists to use dynamic variables in a Collection Runner environment.

Postman for Windows  
Version 7.3.6  
win32 10.0.16299 / x64

Best regards,

E.J.

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 12, 2019, 5:09pm UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078/2 "2019-08-12T17:09:25Z")

</div>

Welcome to the Postman community! I think your question was well formulated. 🤓

If I understand you correctly, you are running multiple instances of the collection runner at the same time (one the for keeping the token valid, and others running actual collections).

If you ask me, you are kind of stretching the capabilities of the runner. I would rather go in the direction of using newman and a CI server to automate this.

If this the above does not work for you, consider putting everything in a single collection and handling token renewal from there. I am not sure what is the advantage of having two collections.

Does this help?

---

<div class="post-metadata">

**Author:** ![evroom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/evroom/32/2508_2.png) [@evroom](https://community.postman.com/u/evroom)\
**Post date:** [August 13, 2019, 7:18am UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078/3 "2019-08-13T07:18:24Z")

</div>

Thank you for replying.

Actually I am running 1 instance of the collection runner. This runner gets the access token and refreshes it regularly. Sort of a background job to keep the session open/valid. Then I have several other collections where I run requests as single requests or as a collection (mostly as a single request). These requests need the access token in order to work. At this moment the collection runner for the session cannot pass the refreshed access token to the other requests by means of setting it as a variable. As a result I get 401 Unauthorized when the token is expired and the game is over.  
Till now I have used this method for similar testing, but here I only need to refresh the session and do not need a new token in my authorization string.  
Overall, it means that I only can test for the period the first token is valid and when I get 401 I need to get a new token manually or refresh it in time manually. Perhaps newman is a solution, but I like using Postman because then I can alter the requests on-the-fly and save the ones I would like to use later.  
Newman is more for when the requests have been build and tested successfully.  
I was a bit surprised to notice that variables in runners cannot be saved whilst the runner is active, probably due to the sand-boxed manner of operating, Being able to save a variable really dynamical, during run-time, would help me a lot here.  
Hopefully it is a bit more clear now.

---

<div class="post-metadata">

**Author:** ![tmccann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/tmccann/32/3550_2.png) [@tmccann](https://community.postman.com/u/tmccann)\
**Post date:** [August 14, 2019, 1:42pm UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078/4 "2019-08-14T13:42:39Z")

</div>

@evroom Just jumping in here to add to what @vdespa touched on. I think this is a bit of a different scenario that what we have typically seen other users do with the Collection runner.

I think there are various approaches here but adding in an idea of my own.  
You could potentially add these refresh call(s) to the collection and using a combination of simple conditional checks and the Postman Sandbox, make a call to get a new token when needed.

To give a bit of an example, in the `Test` tab you could add an `if` statement to check the response code that comes back. If its a 401, then you could make a call to get a new token via `pm.sendRequest` More info [here](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/#pmsendrequest). If its anything else, keep executing as expected.

Hopefully this helps. Let us know how you make out 🙂

---

<div class="post-metadata">

**Author:** ![evroom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/evroom/32/2508_2.png) [@evroom](https://community.postman.com/u/evroom)\
**Post date:** [August 15, 2019, 8:36am UTC](https://community.postman.com/t/fix-or-workaround-for-collection-runner-variable-issue/7078/5 "2019-08-15T08:36:08Z")

</div>

@tmccann  
The pm.sendRequest function is interesting, but unfortunateley will not help for 100% in my case.  
When 401 is returned, it is already too late; the server will have terminated the session and will send a 401 for the refresh request.  
In most REST API interfaces, you can just send refresh using the valid API key, which resets the expiration timer. In this particular case, the REST API is using this access\_token & refresh\_token method, expecting new requests to use the new access\_token.  
I will however use your suggestion to send a refresh in every manual request, just to minimise the risk of getting a 401 whilst testing / developping. The real interface is written in PHP anyhow and there I do not have this issue as I can save the new token at run-time.  
Thanks for taking time to think of a solution.
