# Setting/getting collection variable value in script

**URL:** <https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851>\
**Category:** Help Hub\
**Tags:** variables, collections, scripts\
**Created:** [April 4, 2019, 12:40pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851 "2019-04-04T12:40:23Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![CBoland](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cboland/32/1553_2.png) [@CBoland](https://community.postman.com/u/CBoland)\
**Post date:** [April 4, 2019, 12:40pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/1 "2019-04-04T12:40:23Z")

</div>

I have a collection variable defined (Collection \> Edit \> Variables tab. Variable name: _token_) that I’m using in as request header value. I want to set its value in script. I’m trying to set it with this:

_pm.variables.set(“token”, jsonData.access\_token);_

but the Collection variable value doesn’t change. In the past I’ve used _pm.globals_ and _pm.environment_, and those work here as expected, but this variable is actually scoped to the collection. I’d rather work with it within that scope than use the broader globals or environment scope.

---

<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:** [April 5, 2019, 12:26pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/2 "2019-04-05T12:26:48Z")

</div>

@CBoland First of all, welcome to the community, thanks for coming to ask a good question like this.  
When it comes to Collection Variables they are treated a bit differently than Environment and Global variable.  
[https://learning.getpostman.com/docs/postman/environments\_and\_globals/variables#defining-collection-variables](https://learning.getpostman.com/docs/postman/environments_and_globals/variables#defining-collection-variables)

Hopefully this will help you out a bit. If the variable is not static, and I assume thats the case if its a token. You will likely need to scope this as an environment variable, you can also `unset` it at the end of the tests if need be.

Hopefully this helps.

---

<div class="post-metadata">

**Author:** ![cboland1](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@cboland1](https://community.postman.com/u/cboland1)\
**Post date:** [April 6, 2019, 3:49pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/3 "2019-04-06T15:49:30Z")

</div>

Yes, @tmccann, that helps. It appears then that Collection variables are static whereas Environment and Global variables are dynamic. Perhaps the documentation could be clarified to this effect? Another option is to make Collection variables dynamic (and consistent) with Environment and Globals.

---

<div class="post-metadata">

**Author:** ![julienandre](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@julienandre](https://community.postman.com/u/julienandre)\
**Post date:** [January 15, 2020, 9:42am UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/4 "2020-01-15T09:42:57Z")

</div>

Let me renew this topic, I do love Postman but it comes with lot of frustration about this variables management.

I maintain collections in my workspace, with my environment, I define and then update variables on pre-scripts and tests :

`pm.environment.set("my_token_id", jsonData.uid);`

But when it comes to sharing my collections, if the receiver don’t have any environement, then no variable is set. So I ended setting the environment AND the global variables :

```
pm.globals.set("my_token_id", jsonData.uid);
pm.environment.set("my_token_id", jsonData.uid);

```

But as collections are the way to easily export and share your API or a special workflow, it would be so much elegant to handle all variables into this collection.

How to request for this update ?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 15, 2020, 10:28am UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/5 "2020-01-15T10:28:14Z")

</div>

Hey @julienandre,

Welcome to the community! 👋

The ability to programmatically `get` and `set` variables, at the collection level, has been part of the app for a while now. Hopefully, this new functionality will help solve some of the issues you’re facing.

To use them in the app (Pre-request Script and Tests sandboxes), it’s just a case of using the `collectionVariables` method on the `pm.*` API. This new method works in the same way as you would use the `get`, `set`, `unset` and `clear` functions on the variables, at the Environment and Global level.

```
pm.collectionVariables.set('my_var', 'my_value')  

pm.collectionVariables.get('my_var')

pm.collectionVariables.unset('my_var')

pm.collectionVariables.clear()

```

More information can be found on the Learning Center:

[https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmcollectionvariables](https://learning.getpostman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmcollectionvariables)

---

<div class="post-metadata">

**Author:** ![julienandre](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@julienandre](https://community.postman.com/u/julienandre)\
**Post date:** [January 15, 2020, 10:49am UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/6 "2020-01-15T10:49:43Z")

</div>

Wow, that’s perfect !  
Sorry I missed this section into the doc !

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 15, 2020, 11:06am UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/7 "2020-01-15T11:06:49Z")

</div>

You’re very welcome!

We’re in the process of updating the documentation in the Learning Center so hopefully, this will become a lot clearer soon.

The Learning Center is open source and we’re always looking for willing folks to help improve the content for other users - If you’re up for contributing, that would be awesome. 🏆

![Screenshot 2020-01-15 at 11.04.22](https://us1.discourse-cdn.com/getpostman/original/3X/0/8/08cf5150ba8ad714652a8206ca84ede259188b57.png)

At the top of each page, you will find this button, this will link to the repo! 🙂

---

<div class="post-metadata">

**Author:** ![tifedada](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@tifedada](https://community.postman.com/u/tifedada)\
**Post date:** [January 15, 2020, 2:08pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/8 "2020-01-15T14:08:01Z")

</div>

Hello,

I am trying to set an environment variable for a particular request but after the request is run, the value is not assigned to the variable even though the variable is created from that script, the values are empty. Please advise.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 15, 2020, 3:13pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/9 "2020-01-15T15:13:59Z")

</div>

Hey @tifedada,

Are you able to provide an example of what you’re seeing, please?

An image would be good 🙂 - Maybe also add the code you have in the script, the more information the better 🙂

---

<div class="post-metadata">

**Author:** ![tifedada](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@tifedada](https://community.postman.com/u/tifedada)\
**Post date:** [January 15, 2020, 3:58pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/10 "2020-01-15T15:58:41Z")

</div>

Hey @danny-dainton

I figured out the problem while I was getting the information to send to you together. The response for this particular request is in an array and I didn’t cater for that in the script I wrote to set the variable. Thank you.

---

<div class="post-metadata">

**Author:** ![jesse-dg](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jesse-dg](https://community.postman.com/u/jesse-dg)\
**Post date:** [February 5, 2020, 9:32pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/11 "2020-02-05T21:32:48Z")

</div>

I am using version 7.17.0 on the mac. The pm.collectionVariables.set() simply does not work. Just try something as easy as  
pm.collectionVariables.set(“my\_var”, 'some-value);  
console.log("my\_var = " +pm.collectionVariables.get(“my\_var”));

and you will get an undefined.

---

<div class="post-metadata">

**Author:** ![sagarkm](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sagarkm/32/5273_2.png) [@sagarkm](https://community.postman.com/u/sagarkm)\
**Post date:** [August 14, 2020, 12:08pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/14 "2020-08-14T12:08:15Z")

</div>

I am using Postman v7.30.1. In test script I wrote:  
I want to extract the access\_token from the JSON response and store it in Collections Variable:

> var jsonData = pm.response.json();  
> pm.collectionVariables.set(‘access\_token’, jsonData.access\_token);

Then i tried setting collection variable to a hard coded value, but that too does not work:

> pm.collectionVariables.set(‘access\_token’, ‘abc’);

Am I missing something?

---

<div class="post-metadata">

**Author:** ![trinigooner](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/trinigooner/32/5308_2.png) [@trinigooner](https://community.postman.com/u/trinigooner)\
**Post date:** [August 18, 2020, 3:00pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/15 "2020-08-18T15:00:24Z")

</div>

Was excited to see this but I could not get this to work with Postman 7.8.0 on the Mac. Trying to use the set method I get:

> TypeError: Cannot read property ‘set’ of undefined

I think others have mentioned this as well. Is there some fine detail we’re overlooking here?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 18, 2020, 3:08pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/16 "2020-08-18T15:08:40Z")

</div>

Hey @trinigooner

Welcome to the Postman community! 🚀

That feature wasn’t introduced till v7.9.0

[https://www.postman.com/downloads/release-notes/](https://www.postman.com/downloads/release-notes/)

You would need to update the app version to see this.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 18, 2020, 3:12pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/17 "2020-08-18T15:12:59Z")

</div>

Hey @sagarkm

What does your response body look like?

Did this set a blank variable at the collection level?

Select the 3 dots on the Collection name and hit Edit to see the Collection level elements.

![Screenshot 2020-08-18 at 16.11.52](https://us1.discourse-cdn.com/getpostman/original/3X/e/7/e7ebd2f03a60d3915110f182f7eced240f26a0e0.png)

---

<div class="post-metadata">

**Author:** ![ShamikBanerjee](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@ShamikBanerjee](https://community.postman.com/u/ShamikBanerjee)\
**Post date:** [November 12, 2020, 12:28pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/18 "2020-11-12T12:28:53Z")

</div>

@sagarkm

This seemed to work for me.

In the collections tab on the left panel , firstly click on the 3 dots and choose edit.  
After doing so, already set your variable name as access\_token.  
Then in the test script, I would always suggest to place the following code: this helps to see the status every time:

```auto
pm.test('Status code is 200', function () {
    pm.response.to.have.status(200); 
});

```

After doing so paste this code:

```auto
var jsonBody = pm.response.json();

pm.collectionVariables.set("access_token",jsonBody.access_token);

```

---

<div class="post-metadata">

**Author:** ![Theanderblast](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/theanderblast/32/7885_2.png) [@Theanderblast](https://community.postman.com/u/Theanderblast)\
**Post date:** [March 30, 2021, 3:16pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/19 "2021-03-30T15:16:06Z")

</div>

If I define a collection variable, neither the current value nor the initial value seems to be applied…

---

<div class="post-metadata">

**Author:** ![sean.keegan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sean.keegan/32/6353_2.png) [@sean.keegan](https://community.postman.com/u/sean.keegan)\
**Post date:** [April 27, 2022, 1:20am UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/20 "2022-04-27T01:20:47Z")

</div>

Hi all! I know this topic still sees a good bit of action, so I wanted to provide a hands-on option for a solution!

Check out [this collection](https://www.postman.com/postman/workspace/postman-answers/documentation/19100817-67f061dc-5348-44f0-88ea-0c6d2d37a574) in the Postman Answers public workspace. You can fork it to test it out yourself!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 23, 2023, 3:34pm UTC](https://community.postman.com/t/setting-getting-collection-variable-value-in-script/4851/21 "2023-08-23T15:34:28Z")

</div>

Closed on August 23, 2023, 3:34pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
