# Conflict of same variable in collection and environment scopes

**URL:** <https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [September 4, 2024, 6:15am UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958 "2024-09-04T06:15:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![whimet](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/whimet/32/30073_2.png) [@whimet](https://community.postman.com/u/whimet)\
**Post date:** [September 4, 2024, 6:15am UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/1 "2024-09-04T06:15:33Z")

</div>

Hi Postman community,

I run into a problem of having the same `baseUrl` variable defined in both environment and collection scopes. The environment value is something like `https://example.com` while the collections scope values are like `/api/foo` or `/api/bar`.

I wonder if there’s a way to avoid such conflict? i.e. explicitly addressing the scope such as `{{pm.environment.baseUrl}}`

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 4, 2024, 2:14pm UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/2 "2024-09-04T14:14:59Z")

</div>

Environment variables are a narrower scope than Collection, so if a variable exists in both places, the Environment variable will win.

In scripts, you can call the variables directly, by pm.environment.get or pm.collectionVariable.get, but you can’t do this in places like the URL or body where you just call the variable by its name enclosed in curly brackets {{variableName}}.

If they contain different data for different purposes, then I don’t think there is anyway around this apart to rename one of your variables.

It looks like you need to refactor your requests. One of your variables seems to be the host, and the other variable is the path. So maybe split the variables accordingly.

---

<div class="post-metadata">

**Author:** ![whimet](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/whimet/32/30073_2.png) [@whimet](https://community.postman.com/u/whimet)\
**Post date:** [September 5, 2024, 8:00am UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/3 "2024-09-05T08:00:29Z")

</div>

In my case the collection variable `baseUrl` is created by Postman after importing an OpenAI specification, while the environment `baseUrl` variable already exists in multiple environments. I’m not sure if there’s a way to change the collection variable name before the importing, and it doesn’t feel good to change the environment variable.

Another point is - as the explictly scoped variable is supported in scripts, why not supporting that everywhere when a variable is used to make it more consistent?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 5, 2024, 3:11pm UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/4 "2024-09-05T15:11:44Z")

</div>

I don’t know the technical reasons, but I guess you could put in a feature request on the Postman Github.

> **[Build software better, together](https://github.com/postmanlabs/postman-app-support/issues/new?assignees=&labels=feature&projects=&template=feature_request.yml&title=%3Ctitle%3E)**
>
> GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.

I’m still not getting the variables though. `/api/foo` or `/api/bar` are not baseUrl’s. They are paths, which could be part of a baseUrl, but would not make a full base URL without the host and protocol.

Is it your environment or collection variable set this way? I’d be surprised if importing an OpenAI specification would generate a baseUrl this way.

In a pre-request script, you can use pm.request.url to return a object with fields protocol, host, path, and params.

You can overwrite those elements using the following…

```auto
pm.request.url.protocol = "https";
pm.request.url.host = "postman-echo.com";

```

You might be able to create a pre-request script that takes both variables and then sets the URL as a workaround.

---

<div class="post-metadata">

**Author:** ![whimet](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/whimet/32/30073_2.png) [@whimet](https://community.postman.com/u/whimet)\
**Post date:** [September 8, 2024, 12:13pm UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/5 "2024-09-08T12:13:34Z")

</div>

Thanks Mike, the workaround you suggested works for me. Here’s the pre-request script:

```auto
pm.request.url.update(`${pm.environment.get('baseUrl')}${pm.collectionVariables.get('baseUrl')}${pm.request.url.getPath()}`)

```

Btw, here’s the OpenAPI specification: [https://ea1.aconex.com/api/package-management/docs/openapi/package-management-api-v1.yaml](https://ea1.aconex.com/api/package-management/docs/openapi/package-management-api-v1.yaml). After imported, a collection variable `baseUrl=/api/package-management` is created.

I’ll raise a feature request later.

Cheers.

---

<div class="post-metadata">

**Author:** ![whimet](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/whimet/32/30073_2.png) [@whimet](https://community.postman.com/u/whimet)\
**Post date:** [September 10, 2024, 11:30am UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/6 "2024-09-10T11:30:59Z")

</div>

Submitted feature request: [Explicitly scoped variables in URL · Issue #13128 · postmanlabs/postman-app-support · GitHub](https://github.com/postmanlabs/postman-app-support/issues/13128)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 10, 2024, 11:31am UTC](https://community.postman.com/t/conflict-of-same-variable-in-collection-and-environment-scopes/67958/7 "2024-10-10T11:31:43Z")

</div>

Closed on October 10, 2024, 11:31am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
