# How do I set a Collection variable?

**URL:** <https://community.postman.com/t/how-do-i-set-a-collection-variable/5466>\
**Category:** Help Hub\
**Tags:** variables, collections\
**Created:** [May 16, 2019, 3:14pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466 "2019-05-16T15:14:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![floripare](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/floripare/32/1836_2.png) [@floripare](https://community.postman.com/u/floripare)\
**Post date:** [May 16, 2019, 3:14pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/1 "2019-05-16T15:14:39Z")

</div>

The documentation says I can have variable on several scopes:

> **Scopes**
>
> The following [scopes](https://www.getpostman.com/docs/postman/environments_and_globals/variables) are available for use:
> 
> - [Global](https://www.getpostman.com/docs/postman/environments_and_globals/manage_globals)
> - Environment
> - Collection
> - Local
> - [Data](http://blog.getpostman.com/2014/10/28/using-csv-and-json-files-in-the-postman-collection-runner/)

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?

---

<div class="post-metadata">

**Author:** ![emmaDF](https://avatars.discourse-cdn.com/v4/letter/e/ebca7d/32.png) [@emmaDF](https://community.postman.com/u/emmaDF)\
**Post date:** [May 16, 2019, 3:35pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/2 "2019-05-16T15:35:12Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/6/c6c079083a351c86be7a5fad59126b31e77bfeb5.png)

---

<div class="post-metadata">

**Author:** ![floripare](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/floripare/32/1836_2.png) [@floripare](https://community.postman.com/u/floripare)\
**Post date:** [May 16, 2019, 3:40pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/3 "2019-05-16T15:40:58Z")

</div>

Thank you very much!

---

<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:** [May 16, 2019, 4:29pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/4 "2019-05-16T16:29:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![floripare](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/floripare/32/1836_2.png) [@floripare](https://community.postman.com/u/floripare)\
**Post date:** [May 16, 2019, 4:44pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/5 "2019-05-16T16:44:16Z")

</div>

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?

---

<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:** [May 16, 2019, 6:31pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/6 "2019-05-16T18:31:09Z")

</div>

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

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

> <https://github.com/postmanlabs/postman-app-support/issues/5053>
>
> \*\*Is your feature request related to a problem? Please describe.\*\*
> Not being ab…le to modify collection variables in a dynamic way.
> 
> \*\*Describe the solution you'd like\*\*
> A \`pm.collection.set(...)\` function would be very helpful for some collection-specific work that I'd like to be able to accomplish (for example, setting a \`next\` query string parameter that is specific to the collection the tests are running in).
> 
> \*\*Describe alternatives you've considered\*\*
> I am currently using \`pm.globals.set(...)\` to accomplish my goal, but it makes using semantic names like \`next\` challenging because that is a strange thing to throw into the global scope, but namespacing variables is also a clunky solution.

---

<div class="post-metadata">

**Author:** ![sabuncu](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sabuncu/32/3353_2.png) [@sabuncu](https://community.postman.com/u/sabuncu)\
**Post date:** [May 20, 2020, 7:40pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/8 "2020-05-20T19:40:01Z")

</div>

> [@emmaDF](#):
>
> If you edit the collection (“…” \> Edit), you can see a variables tab for it

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

---

<div class="post-metadata">

**Author:** ![Thriven452](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Thriven452](https://community.postman.com/u/Thriven452)\
**Post date:** [June 14, 2021, 6:41pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/9 "2021-06-14T18:41:55Z")

</div>

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](https://www.postmanlabs.com/postman-collection/VariableScope.html) and has all the methods available like:

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

Thanks !

---

<div class="post-metadata">

**Author:** ![joint-operations-ca4](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@joint-operations-ca4](https://community.postman.com/u/joint-operations-ca4)\
**Post date:** [May 12, 2022, 7:47pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/10 "2022-05-12T19:47:45Z")

</div>

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:

 ![screenshot](https://us1.discourse-cdn.com/getpostman/original/2X/8/8474ca0d87bcc17b06f1b0b47fb19dc24553df2e.png)

---

<div class="post-metadata">

**Author:** ![pooja-mistry](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/pooja-mistry/32/11451_2.png) [@pooja-mistry](https://community.postman.com/u/pooja-mistry)\
**Post date:** [May 13, 2022, 3:25pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/11 "2022-05-13T15:25:57Z")

</div>

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](https://learning.postman.com/docs/getting-started/installation-and-updates/)

---

<div class="post-metadata">

**Author:** ![redshift-rpandey](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@redshift-rpandey](https://community.postman.com/u/redshift-rpandey)\
**Post date:** [January 30, 2024, 6:05pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/12 "2024-01-30T18:05:17Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/c/0c9a227b33170ad66d78e94185df6ad0834a9450.png)

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.

---

<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 30, 2024, 6:16pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/13 "2024-01-30T18:16:57Z")

</div>

Hey @redshift-rpandey 👋

Welcome to the Postman community :postman:

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

> <https://github.com/postmanlabs/postman-app-support/issues/12538>
>
> \### Is there an existing request for this feature?
> 
> \- \[X\] I have searched the ex…isting issues for this feature request and I know that duplicates will be closed
> 
> \### Is your feature request related to a problem?
> 
> We use collection variables to our API's and there are some authorization variables that we need to set to secret, but we can't find a way to do that.
> 
> \### Describe the solution you'd like
> 
> Allow collection variables to be set to secret if need be.
> 
> \### Describe alternatives you've considered
> 
> \_No response\_
> 
> \### Additional context
> 
> \_No response\_

> <https://github.com/postmanlabs/postman-app-support/issues/10580>
>
> \### Is there an existing request for this feature?
> 
> \- \[X\] I have searched the …existing issues for this feature request
> 
> \### Is your feature request related to a problem?
> 
> With the great addition of the \`secret\` variable type in the platform, users can now set the type manually through the UI but having the ability to set them via a script would round out the flow. By manually setting it, you would be exposing that 'secret' value to \`over the shoulder\` attacks until you have set the type and then hit save to mask the value.
> 
> The \`.set()\` function already has a 3rd optional arg for the \`type\` but as 'secret' isn't in the \[Variable Types\](http://www.postmanlabs.com/postman-collection/Variable.html#.types) it would fallback to 'default'.
> 
> !\[Sandbox Environment\](https://user-images.githubusercontent.com/17089546/149930590-6854ccc9-a41a-4c22-b29e-e7b863d6e6da.png)
> 
> 
> \### Describe the solution you'd like
> 
> Using the sandbox environment to set the variable type. A basic example of getting an access token value from a response body and then setting this as a secret variable:
> 
> \`\`\`javascript
> let token = pm.response.json().access\_token;
> pm.environment.set('token', token, 'secret');
> \`\`\`
> 
> \### Describe alternatives you've considered
> 
> The Postman API can be used to programmatically set the \`type\` which could be done in a script using the \`pm.sendRequest()\` function but that seems a bit too long-winded to achieve what could be done in the \`set()\` function.
> 
> \`\`\`javascript
> var options = {
> 'method': 'PUT',
> 'url': 'https://api.getpostman.com/environments/{{environment\_uuid}}',
> 'header': {
> 'X-Api-Key': 'XXXX',
> 'Content-Type': 'application/json'
> },
> body: JSON.stringify({
> "environment": {
> "name": "Test Environment",
> "values": \[
> {
> "key": "token",
> "value": "ABC123",
> "type": "secret"
> }
> \]
> }
> })
> 
> };
> pm.sendRequest(options, function (error, response) {
> if (error) throw new Error(error);
> console.log(response.json());
> });
> 
> \`\`\`
> 
> \### Additional context
> 
> \_No response\_

Please do check them out and upvote/add your usecase to the treads. 🙏🏻

---

<div class="post-metadata">

**Author:** ![redshift-rpandey](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@redshift-rpandey](https://community.postman.com/u/redshift-rpandey)\
**Post date:** [January 30, 2024, 6:27pm UTC](https://community.postman.com/t/how-do-i-set-a-collection-variable/5466/14 "2024-01-30T18:27:53Z")

</div>

Hi @danny-dainton 👋🏼

Thanks for the quick response. I will upvote both the requests 👍
