Collection Variables using environments

Is it possible to have collection variables tied to different environments?

If I have a single service and three environments then I can set up the following environment variables:

  • Local
    – BaseUrl

  • Test
    – BaseUrl

  • Production
    – BaseUrl

I create a collection for my service and add a number of requests, to the various endpoints for the service, which reference the variable {{BaseUrl}} in the URL.

I add a couple more collections for two new services Service2 and Service3 which also need a BaseUrl variable:

  • Local
    – BaseUrl
    – Service2BaseUrl
    – Service3BaseUrl

  • Test
    – BaseUrl
    – Service2BaseUrl
    – Service3BaseUrl

  • Production
    – BaseUrl
    – Service2BaseUrl
    – Service3BaseUrl

After setting this all up I now have access to the Service2BaseUrl and Service3BaseUrl variables in the Service 1 collection in spite of the fact that those variables will never be used with that collection.

As more collections are added for new services the number of variables will increase and makes the environment variables page unwieldy since all variables are displayed regardless of if they are relevant to the current collection.

In theory the collection variables looks like it handles my use case better (since variables are defined at a collection level I could just create one variable called BaseUrl which i can administer for the current collection) but I can’t see a way of changing the value based on which environment I have selected.

Is it possible to have collection variables tie into environment or is there another way I could setup postman to handle my usage? I have over 20 services to create collections for and with just one variable for Base URL I’m going to have a lot of variables to scroll through.

Thanks

How about using Globals and Environments?
You could put your common variables in Global and the individual variables in different environments:

pm.globals.get("variable_key");

pm.environment.get("variable_key");

A collection level script or prerequisite script in the first request would set the BaseUrl for the collection if you have an environment variable with the target environment purpose named so that:
pm.environment.get("deploylevel") would return one of local,test,production,
Which would then determine the url to use by way of maybe a select statement

switch (pm.environment.get("deploylevel")){
  case "production":
    theUrl = "yourCollectionProductionUrl";
    break;
  ...Etc
}

pm.environment.set("baseUrl",theUrl);

would set the url used for the request.
That way the correct urls stay documented in the correct collections, and are only changed in the required place

1 Like

Thanks for the responses guys.

Iaingmacg your solution is exactly what I was looking for. I also like the fact that when I export the collection the variables are also exported and the only dependency I have is to ensure that the environments are created with one variable called deploylevel.

Awesome thanks!

1 Like

You are welcome.
It’s nice also to hear that: I knew my solution was working for me So for it to be helpful to others reassures me I My reid ability and practicality thinking isn’t off key!

It is worth considering sometimes storing it in globals, it all depends on your use cases and portability considerations.