Setting/getting collection variable value in script

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.

@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.

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.

1 Like

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.


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 ?


Hey @julienandre,

Welcome to the community! :wave:

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')  




More information can be found on the Learning Center:


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

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. :trophy:

Screenshot 2020-01-15 at 11.04.22

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


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.

1 Like

Hey @tifedada,

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

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

1 Like

Hey @dannydainton

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.

1 Like

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.

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?

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?

Hey @trinigooner

Welcome to the Postman community! :rocket:

That feature wasn’t introduced till v7.9.0

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

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


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:

pm.test('Status code is 200', function () {; 

After doing so paste this code:

var jsonBody = pm.response.json();


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

1 Like