How do you read the ID of a Folder in script?

Reading the UUID value of a Folder from within that folder’s pre-request script would add value to the test suite I’m building in Postman. I’m aware that clicking on the “i” icon in the GUI will reveal this. My need is to access it within a pre-request script at the folder level.

Use Case

There is a model instance dependency for a group of tests (model A instance must exist in order to perform multiple tests against model B). I want to create an instance of model A on-demand within the pre-request of the group’s folder. For uniqueness, I would like to name it based on that group folder, such as:

let groupVarName = "model-A-for-group-" + folder.guid;
if (! pm.collectionVariables.has(groupVarName) {
  pm.collectionVariables.set(groupVarName, val);
}

I’ve got dozens of group folders like that so I’d like to not need to concern myself with choosing a unique name for each of them. Creating the “model A” instance requires a sendRequest call, so only making one for the group of tests would be far more efficient.

So how would one get the UUID of the folder from within that folder’s pre-request script?

I don’t think you can as the pm.info is limited in what it can return.

Postman JavaScript reference | The PM object

I do something similar with pm.info and pm.request in the pre-request script (which I can then reference in the tests tab).

const method = pm.info.requestName + " - " + pm.request.url.path.join("/").replace(/^api\//, "");
pm.environment.set("currentMethod", method);

pm.test(`${method} - Status code is 200`, () => {
  pm.response.to.have.status(200);
});

pm.test(`${method} - Response time below 500ms`, () => {
  pm.expect(pm.response.responseTime).to.be.below(500);
});

This is a good solution for individual request-level scripts, though it won’t help me in the folder level to create a shared data set for contained tests within. I think there are some highly-voted feature requests for access to folder names out there. I might need to wait for some of those to come online.

@adambellasps

Not sure if this would be acceptable, but you could potentially share the collections and then view the collection via its URL and it will return the JSON for the entire collection. Which includes folder names and the ID’s.

It’s how the training courses check your work. It has a request with the collection URL and a bunch of tests checking for folder names and the existence of element that were covered in the training.

A small snippet of the response.

    "item": [
        {
            "name": "Learn APIs",
            "item": [
                {
                    "name": "Begin learning",
                    "id": "1a3ec290-c2e3-4a2d-8e01-bd543fdaf6dc",
                    "request": {
                        "method": "GET",
                        "header": [],
                        "url": "galaxy-apis-101.glitch.me/begin"
                    },
                    "response": []
                },
                {
                    "name": "Get customers",
                    "id": "1352494a-a947-4394-a1b9-394b845c2d08",
                    "request": {
                        "method": "GET",
                        "header": [],
                        "url": "galaxy-apis-101.glitch.me/customers"
                    },
                    "response": []
                },