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:
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.
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.
@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)
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}})
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)
@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.
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.
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.
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…
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.
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 if it matters. Waiting for your opinions.
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!
Hi,
The correct order of variable access in Postman, from highest to lowest priority, is as follows:
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.
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.
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.
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:
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.