# Introduce a new Variable Scope - Folder Variable

**URL:** <https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715>\
**Category:** Help Hub\
**Tags:** variables, collections, folders\
**Created:** [June 29, 2020, 4:09am UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715 "2020-06-29T04:09:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandemonium](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Pandemonium](https://community.postman.com/u/Pandemonium)\
**Post date:** [June 29, 2020, 4:09am UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/1 "2020-06-29T04:09:22Z")

</div>

Hi, I have a use case where I have an API collection that was used for one product (Assume product A). Inside the API collections, there are finer categorization of different API calls categorized by their subpaths as follows:  
`http://{{host}}:{{port}}/{{subpath}}`

where each subpath is categorized by the folders shown in screenshot below.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/d/5d93925ab6ae7df13c7273fcaead729a2a7764a8.png)

Each folder in the API collection is referring to a different microservice, and they would have different subpaths, eg.

```auto
/ProductA/Auth
/ProductA/Asset

```

As a user, i also have a lot of other API collections for other products, so it would be best (if possible) if i do not use different API collections for different microservices, since they are all related to a single product in this case.

Hence, imo, it would be best if there’s a finer variable management at folder level as well, so that users can use Collections for storing product based APIs and Folder for storing microservices based APIs.

---

<div class="post-metadata">

**Author:** ![wendy.cook](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@wendy.cook](https://community.postman.com/u/wendy.cook)\
**Post date:** [June 29, 2020, 11:20pm UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/2 "2020-06-29T23:20:48Z")

</div>

This already exists, unless I misunderstand your question.

You can set your url for each endpoint to something like

> https://{{Product}}/{{FolderName}}

Then, right click on the folder, click on Edit and then in the Pre-Request tab, add this code:

> pm.globals.set(“FolderName”, “Auth”); // (or whatever the folder name is).

Do this Pre-Request add for each of your folders, setting the var to the Folder’s name.

Poof. Done.

---

<div class="post-metadata">

**Author:** ![Pandemonium](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Pandemonium](https://community.postman.com/u/Pandemonium)\
**Post date:** [July 23, 2020, 8:49pm UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/3 "2020-07-23T20:49:12Z")

</div>

Thanks @wendy.cook . I just tried this and it seems to work. But i am curious if this means that if the {{FolderName}} needs to be an unique name, because you’re setting this as global variable and if I’m having the same FolderName for different Product, would the variable will be able to resolve correctly.

Eg.  
/{{ **ProductA** }}/{[Auth}}  
/{{ **ProductB** }}/{[Auth}}

---

<div class="post-metadata">

**Author:** ![Pandemonium](https://avatars.discourse-cdn.com/v4/letter/p/8797f3/32.png) [@Pandemonium](https://community.postman.com/u/Pandemonium)\
**Post date:** [July 23, 2020, 9:04pm UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/4 "2020-07-23T21:04:21Z")

</div>

Alright, seems that everytime you run an API from a folder, the Pre-request scripts will always get triggered and this will overwrite the last known global {{FolderName}} variable set. This allows {{FolderName}} to be a non-unique name and can be re-used on some other collections as well.

Thanks for the wonderful tips.

---

<div class="post-metadata">

**Author:** ![flight-candidate-858](https://avatars.discourse-cdn.com/v4/letter/f/94ad74/32.png) [@flight-candidate-858](https://community.postman.com/u/flight-candidate-858)\
**Post date:** [July 27, 2022, 1:54pm UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/5 "2022-07-27T13:54:42Z")

</div>

To use a pre-request script amounts to a workaround. Why not have an actual variable set whose scope level is per-folder, in the way that is presently implemented with Globals, Workspaces, and Collections?

---

<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:** [December 12, 2024, 12:54pm UTC](https://community.postman.com/t/introduce-a-new-variable-scope-folder-variable/13715/6 "2024-12-12T12:54:13Z")

</div>

Closed on December 12, 2024, 12:54pm UTC

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