# Variable Scope Precedence

**URL:** <https://community.postman.com/t/variable-scope-precedence/8450>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [October 18, 2019, 8:42pm UTC](https://community.postman.com/t/variable-scope-precedence/8450 "2019-10-18T20:42:23Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![morleym\_cubic](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@morleym\_cubic](https://community.postman.com/u/morleym_cubic)\
**Post date:** [October 18, 2019, 8:42pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/1 "2019-10-18T20:42:23Z")

</div>

If I understand correctly, if you reference a variable in a request URL / body then the precedence for resolving that variable based on the name should be:

1st: Runtime/local variable  
2nd: Collection variable  
3rd: Environment variable

I’m seeing environment take precedence over collection when both are defined. I just wanted to check if this was a bug before I file on GitHub. I have created a test collection/env file for this but I am not sure how to attach it.

---

<div class="post-metadata">

**Author:** ![morleym\_cubic](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@morleym\_cubic](https://community.postman.com/u/morleym_cubic)\
**Post date:** [October 18, 2019, 8:46pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/2 "2019-10-18T20:46:20Z")

</div>

I poked around and found this: [https://learning.getpostman.com/docs/postman/environments\_and\_globals/variables/](https://learning.getpostman.com/docs/postman/environments_and_globals/variables/)

According to this, env is a more ‘specific’ scope than collection.

I’d like to challenge that, considering many requests in many collections can use the same environment variables, but many requests in many collections **cannot** use the same collection variables.

---

<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:** [October 18, 2019, 9:32pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/3 "2019-10-18T21:32:24Z")

</div>

Hey @morleym_cubic,

What command are you using to call the Collection Variable in this case?

---

<div class="post-metadata">

**Author:** ![morleym\_cubic](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@morleym\_cubic](https://community.postman.com/u/morleym_cubic)\
**Post date:** [October 18, 2019, 10:58pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/4 "2019-10-18T22:58:27Z")

</div>

@danny-dainton I have shared a test collection for this purpose here (instructions are in the collection description for creating the environment and adding the environment variable)

[https://www.getpostman.com/collections/25608afef3e1d2cd1fcc](https://www.getpostman.com/collections/25608afef3e1d2cd1fcc)

To directly answer your question: I’m not using any command to call the variable. I am referencing it in a request body/url (body, in this case, using {{variableName}})

---

<div class="post-metadata">

**Author:** ![morleym\_cubic](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@morleym\_cubic](https://community.postman.com/u/morleym_cubic)\
**Post date:** [October 18, 2019, 11:02pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/5 "2019-10-18T23:02:14Z")

</div>

Again, I understand now that this is expected behavior according to [https://learning.getpostman.com/docs/postman/environments\_and\_globals/variables/](https://learning.getpostman.com/docs/postman/environments_and_globals/variables/)

But hopefully this use case (especially with the recent change to enable write access to collection variables from scripts) highlights my argument for collection variables being a more specific scope than environment variables (and thus, being _used_ when there are variables of the same name for both scopes)

---

<div class="post-metadata">

**Author:** ![morleym\_cubic](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@morleym\_cubic](https://community.postman.com/u/morleym_cubic)\
**Post date:** [October 30, 2019, 10:44pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/6 "2019-10-30T22:44:13Z")

</div>

@danny-dainton I’d like to revisit this topic because I’ve been seeing my teammates run into this collision problem frequently. For context, they’re working to replace local variable usage with collection variable usage (since this allows for ‘clicking through’ a collection one request at a time, something impossible with local variables). This is for collections where we’re:

- Generating a unique value at runtime. e.g. creating a value for email address for an account creation request, and then referencing it in the tests of later requests in that collection
- Storing something from one request’s response and then using it in a later request in that collection. e.g. storing an auth token value from an authenticate response to reference in the headers for an account details request afterwards

Our only solution currently for env/collection collisions is “just don’t have any collisions” (for example, if someone needs an environment-specific valid password to use while testing an account creation endpoint, then the env value should be called `valid_password` and not `password`). We will make do with that.

Still, I’d like to make the case that when resolving a variable reference in a request url/body with `{{variableName}}` the order of precedence should go:

1st: local, which can be used across an execution (as specific as a single request)  
**2nd: collection,** which can be used across a collection  
**3rd: environment,** which can be used across collections  
4th: global, which can be used across collections and across environments

Not sure where ‘data’ falls in this list (I suppose after local and before collection), but isn’t super relevant to our team right now. **The main disagreement I have with the current behavior** is that environment variables, which can be used in a wider scope (across collections), are being considered more specific than collection variables (which can only be used within a collection). Conceptually, I find that incorrect.

I understand this would be an impactful change if anyone’s relying on env \> collection in their teams today, so it’s not a light decision to make. But, given the recent addition of `pm.collectionVariables.set()` (and thank you so much for doing so!), this feels like the next step towards minimizing scope pollution.

---

<div class="post-metadata">

**Author:** ![cohen1](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cohen1](https://community.postman.com/u/cohen1)\
**Post date:** [April 26, 2020, 12:46pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/7 "2020-04-26T12:46:45Z")

</div>

Hi there  
I to think it not very usefull, that environment variables take precedens over collection variables. As @morleym_cubic expresses, collection is a narrower scope than environment. By the current behavior, it is not possible to override an evironment variable with a collection variable. So we can not have different collections that uses local values to replace default values in the environment.

---

<div class="post-metadata">

**Author:** ![cohen1](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cohen1](https://community.postman.com/u/cohen1)\
**Post date:** [April 26, 2020, 12:57pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/8 "2020-04-26T12:57:38Z")

</div>

According to Postman docs: [https://learning.postman.com/docs/postman/variables-and-environments/variables/#variable-scopes](https://learning.postman.com/docs/postman/variables-and-environments/variables/#variable-scopes) , collections are a narrower scope than environments and a collection variable **should** take precedence over an enviroment variable. The principle is mentioned in the docs for global and local:

> If a variable with the same name is declared in two different scopes, the value stored in the variable with narrowest scope will be used—for example if there is a global and a local variable both named `username` , the local value will be used when the request runs.

So it seems clearly to be a bug that environment variables override collection variables.

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [April 26, 2020, 3:24pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/9 "2020-04-26T15:24:38Z")

</div>

Hi there, the part of the docs you’ve linked to indicates environment variables are closer scope than collection and therefore should override collection values - is there somewhere in the docs you’ve seen the info imply that collections are narrower scope? Just in case we need to update the docs…

---

<div class="post-metadata">

**Author:** ![cohen1](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cohen1](https://community.postman.com/u/cohen1)\
**Post date:** [April 26, 2020, 8:34pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/10 "2020-04-26T20:34:01Z")

</div>

Oh, you are right. Sorry, I was wrong about the docs - thanks for pointing that out.

But like @morleym_cubic, I really think collection variables naturally fits as a more specific scope than environment variables. As @morleym_cubic puts it:

> …many requests in many collections can use the same environment variables, but many requests in many collections **cannot** use the same collection variables

In my opinion, that observation alone sets the hierachy.

---

<div class="post-metadata">

**Author:** ![cohen1](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@cohen1](https://community.postman.com/u/cohen1)\
**Post date:** [April 29, 2020, 9:54am UTC](https://community.postman.com/t/variable-scope-precedence/8450/11 "2020-04-29T09:54:27Z")

</div>

I just realized, that global scope is local to workspace, which makes it what I wanted environment scope to be.

So, @morleym_cubic, I’m not shure if there would still be a reason to promote environment scope?

---

<div class="post-metadata">

**Author:** ![csgo.expertt](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@csgo.expertt](https://community.postman.com/u/csgo.expertt)\
**Post date:** [May 8, 2020, 2:46pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/12 "2020-05-08T14:46:32Z")

</div>

Hi guys! I have the same issue. I see many suggestions and explanations here, but who tries them all? Have you found the most appropriate solution? In my case, nothing helps actually, and it is very disappointing for me. It can also mean that I am doing something wrong. I am from Spain, [https://worldcams.tv/spain/lanzarote/airport](https://worldcams.tv/spain/lanzarote/airport) if it matters. Waiting for your opinions.

---

<div class="post-metadata">

**Author:** ![ObjectIsAdvantag](https://avatars.discourse-cdn.com/v4/letter/o/ee59a6/32.png) [@ObjectIsAdvantag](https://community.postman.com/u/ObjectIsAdvantag)\
**Post date:** [May 28, 2020, 8:20am UTC](https://community.postman.com/t/variable-scope-precedence/8450/13 "2020-05-28T08:20:02Z")

</div>

hi Sue, I believe these 2 articles present diverging statements regarding precedence:

- incorrect, needs a fix: [https://support.getpostman.com/hc/en-us/articles/115003703205-What-are-the-different-variable-scopes-](https://support.getpostman.com/hc/en-us/articles/115003703205-What-are-the-different-variable-scopes-)
- correct: [https://learning.postman.com/docs/postman/variables-and-environments/variables/#variable-scopes](https://learning.postman.com/docs/postman/variables-and-environments/variables/#variable-scopes)

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [May 28, 2020, 8:23am UTC](https://community.postman.com/t/variable-scope-precedence/8450/14 "2020-05-28T08:23:56Z")

</div>

Hi @ObjectIsAdvantag! Thanks for reporting this, we are actually in the process of migrating our support FAQs into the learning center so I’ll make sure this gets addressed in the process! 🙌

---

<div class="post-metadata">

**Author:** ![ecv\_zen](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@ecv\_zen](https://community.postman.com/u/ecv_zen)\
**Post date:** [October 26, 2020, 1:56am UTC](https://community.postman.com/t/variable-scope-precedence/8450/15 "2020-10-26T01:56:33Z")

</div>

Hi Sue,

This article also has the incorrect scope description:

> **[Postman JavaScript reference | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-variables-in-scripts)**
>
> Postman JavaScript reference: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

Thanks.

---

<div class="post-metadata">

**Author:** ![suesmith](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/suesmith/32/2307_2.png) [@suesmith](https://community.postman.com/u/suesmith)\
**Post date:** [October 26, 2020, 9:57am UTC](https://community.postman.com/t/variable-scope-precedence/8450/16 "2020-10-26T09:57:18Z")

</div>

Hi @ecv_zen, can you be more specific about what you’re finding incorrect in that page?

---

<div class="post-metadata">

**Author:** ![prakashvarma](https://avatars.discourse-cdn.com/v4/letter/p/35a633/32.png) [@prakashvarma](https://community.postman.com/u/prakashvarma)\
**Post date:** [May 25, 2023, 6:52am UTC](https://community.postman.com/t/variable-scope-precedence/8450/17 "2023-05-25T06:52:05Z")

</div>

Hi,  
The correct order of variable access in Postman, from highest to lowest priority, is as follows:

1. **Runtime/Local Variable** : Runtime or local variables have the highest priority and are accessible only within the specific request or script where they are defined. They override variables of the same name in any other scope.
2. **Environment Variable** : If a variable with the same name is not found as a runtime/local variable, Postman will look for an environment variable. Environment variables are specific to an environment and are accessible to all requests within that environment. They override variables of the same name at the collection and global scope.
3. **Collection Variable** : If a variable is not found as a runtime/local or environment variable, Postman will check if it exists as a collection variable. Collection variables are defined at the collection level and are accessible to all requests within that collection. They override variables of the same name at the global scope.
4. **Global Variable** : If a variable is not found as a runtime/local, environment, or collection variable, Postman will look for it as a global variable. Global variables have the lowest priority and are accessible across all collections and environments in Postman. They serve as default values and are overridden by variables of the same name in any other scope.

Therefore, if the same variable name exists in all four scopes, the order of access will be:

1. Runtime/Local Variable (highest priority)
2. Environment Variable
3. Collection Variable
4. Global Variable (lowest priority)

---

<div class="post-metadata">

**Author:** ![msawired](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@msawired](https://community.postman.com/u/msawired)\
**Post date:** [September 11, 2023, 5:02pm UTC](https://community.postman.com/t/variable-scope-precedence/8450/18 "2023-09-11T17:02:02Z")

</div>

Actually, I do agree that Environment variables take precedence over Collection. We set the default (and published as public) variables in Collection. But for our testing and staging servers etc, we can override these by creating Environment variables, which are all non-public.  
I think the problem is more about how the software/UI is designed. It gives a sense that Environments are the top level, because they are right there in the main navigation.
