How to get the name of the current selected environment collection?

Is there a built in variable to get the name of the currently selected Environment collection?

@lisamariechooose Welcome to the Community :partying_face:

I believe you are looking for the current environment name.

If so simply this will give you:

console.log(pm.environment.name);

If you need the complete details about the current environment you simple do as:

console.log(pm.environment);

This will return a big JSON with Environment ID, name and the list of variables under the environment :blush:

1 Like

Hi…
so I would like to use it like this: {{environment.name}}
so not in a script. Actually as part of the Naming of OAuth token. Is that possible?

In that case you can assign it to a variable and use it:

pm.collectionVariables.set("currentEnvironmentName", pm.environment.name);

Use {{currentEnvironmentName}} in naming the token. Is this helpful? :innocent:

2 Likes

Thank you so much. Very helpful! :slight_smile:

Actually… testing it out it does not work.

I on the Authorization page of a collection and trying to get an access token. Even thought I added the code to Pre-request script… that script is not run when pressing “Get new Access Token” button.
So the variable is not set with the currently selected Environment collection when fetching a new token.

Any idea on how I can do this without having to add an environment variable to all environment collections?