Where to set the variables?

Hi there! Hope y’all are doing fine.

So, i used Insomnia at the beginning of my carreer, but for the last couple months i’ve been trying Postman since it’s waaaaaaay better in any point.

I just have some newbie doubts about the general workflow of Variables. I have a couple collections and each of them use differents URLs, but, all of those have the {{base_url}} variable from the “Collection Variable”.

What is the right way to organize Variables? Using Collection Variables or creating envinronments for each one of them, like:

  • Exitlag Prod;
  • Exitlag Staging;
  • Api XPTO Prod;
  • Api XPTO Prod;

This way, wouldn’t i be able to call the wrong API if i don’t switch the envinronment on the top right corner of the screen?

I really don’t know the “ideal way” to manage the Variables.

There are 2 ways

  1. You can set directly in collection (see in the image)
  2. You can set through pm.collectionVariables.set("<variable_name>",<variable_value>)

When will I use collection variable?

  • If I want to set the variable as a dynamic variable

  • Example: I would like to test creating/updating/reading/deleting a record in DB. I will save the id of the record in collection variable

This is my experience. Hopefully it is helpful for you :grin:

Hi @navigation-special15 ,
Yes it is a possibility that you might enter some other environment variable to hit prod, staging, etc. The better approach will be to follow an order, for example whatever is declared at the top of hierarchy will be followed by postman. You can go with staging then prod or whatever sequence you would like to avoid any accidental calls.

Hi @navigation-special15,

For what you are trying to do I would recommend going the environment variables route, as collection variables will not change with the environment.

My general approach is anything environment specific needs to go in an environment variable, anything that remains the same regardless of environment can become a collection variable.

For transient variables I either use pm.variables if I don’t need them persisted and if I need the variable in another request then i make it a global variable as they are easy clear out as part of a new test using pm.globals.clear()

I’m not saying its perfect, but it works for me :slight_smile:
Hope that helps.