Persist environment variable automatically after updating it's value

Hello all!

I have an environment variable as a mock response and every time I update the environment variable value, I need to persist it to be able to reflect it as a response form the mock server.

Is there some way I can automate this (persist process) so that I can run all my requests at a time in the collection runner.

Thanks in advance for your replies!

Hi bbahadur,

Welcome to postman community :slight_smile: .
I believe what you are looking for is to set the environment variable automatically rather than manually?

If that is the case you can use

pm.environment.set("<key>",<value>)

Let me know if this helps.

Regards,
Skandh

Hey @skandh-15 ,

Thanks for your response,
I am able to set the environment variable value, what I want to do is to persist the variable value, i.e. have both ā€˜initialā€™ and ā€˜currentā€™ value updated. And I want to do this via code/script, rather than clicking on the ā€˜persist allā€™ button. I hope I made the scenario more clear.

BR,
Bharat.

I somehow missed this in the help center. The problem is now solved.
Sending the PUT request with the details of the same environment every time I update the Environment variableā€™s value. Working well.

1 Like

Hi @bbahadur . I tried this solution and it works fine. Except that if I only wanted to update a subset of variables, the initial values of all the other variables that I did not want to update get deleted.

For example, letā€™s say I have 10 different environment variables. In my script, I only include var1, var2, var3 to persist that current value to the initial values. After the script runs, I see that the initial values of var1 to var3 gets updated correctly. But the initial values of var4 to var10 gets blanked out.

Is there a way to do selective updating of initial values, without affecting all the other variables that I do not want to update?

Iā€™ve done further research, and it seems that there is no direct way to persist current values to initial values of only a subset of variables. The only way I could address this is to get all current environment variables, then update the values of the target subset. After which, I will pass the complete set of environment to the Postman API to persist the values.

Here is an example of what I have created in Github Gist:

Postman - Persist Environment Variables