How do I set a Collection variable?

The documentation says I can have variable on several scopes:

Scopes

The following scopes are available for use:

But I can’t find instructions on how to add a Collection variable in the app UI. Is this something I can only do via script, such as a pre-request or test script?

If you edit the collection (“…” > Edit), you can see a variables tab for it :slight_smile:

4 Likes

Thank you very much!

Just to add some additional information here:

Once those are set within that area on the UI, you’re able to use the values in the same way that you would use the other variables.

This doesn’t have a pm.collections.get() type syntax but you can use pm.variables.get("var_name") syntax to programatically access the collection level variable value.

1 Like

Thank you, @danny-dainton.

How about setting a collection variable in a pre-request script? I’m reading the docs to try to figure out why I can’t seem to make pm.variables.set() work.

I mean, if I need to set a variable from within a pre-req script, must I use Globals?

Currently, you can only use the .get() function to access the collection level values. You’re not able to use the .set() function within the prerequest/test scripts. Hence the reason why the manual setting of these variables, was offered as the solution. :grin:

There is an open issue for that enhancement, over on GitHub. :grin:

3 Likes

Can this be done through the web interface as well? If so, how? Thank you.

From Postman v7.9.0 added support for new pm.collectionVariables API which gives more control over interacting with the collection scoped variables.

Just like pm.environment , it’s an instance of VariableScope and has all the methods available like:

pm.collectionVariables.set
pm.collectionVariables.has
pm.collectionVariables.get
pm.collectionVariables.unset

Thanks !

It took me a while to find the correct “…” menu. There are “…” menus next to the tabs and each “folder” in the collection. The “…” menu you need to find is invisible until the mouse cursor is over the collection’s entry in the left panel. This screenshot shows where to look:

1 Like

Hi @joint-operations-ca4
Welcome to our community! Your screenshot looks like it is on an older version of Postman. Feel free to update to the latest version to use some of our latest features : Installing and updating | Postman Learning Center

1 Like

Hi @pooja-mistry

Are there any plans of adding the functionality that allow users to set the variable type (Default or Secret) for Collection Variables? It is not available in the UI as of now.

Also, it would be nice if users could programmatically set the type while creating them through scripts. Currently, I have to manually toggle the type for each variable I wish to hide.

Hey @redshift-rpandey :wave:

Welcome to the Postman community :postman:

We have 2 open feature requests for those points you raised:

Please do check them out and upvote/add your usecase to the treads. :pray:t2:

1 Like

Hi @danny-dainton :wave:t3:

Thanks for the quick response. I will upvote both the requests :+1:

1 Like