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

1 Like

@bbahadur Regarding “Programmatically updates initial values from pre-request or test scripts” you mentioned it was resolved by sending PUT method request. Can you please explain with detailed steps on how to configure this automatic persist?

Hey @Mahesh2212 :wave:

It’s been long since I worked on it, but as far as I remember, I send a PUT method request with the details of the environment ID, X-API-KEY and a body that has an environment object and data to be persisted, here’s the structure:

const requestOptions = {
    url: 'https://api.getpostman.com/environments/[Env ID here]',
    method: 'PUT',
    header: {
        'X-API-Key':'[API Key here]'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            'environment': {
            'name': '[Env name here]',
            'values': [
            {
             'key': '[key name]',
             'value':[value] 
            }
            ]
            } 
            })
    }
}
//The request is ready to send, below method sends the request to POSTMAN and saves the variable in the environment
pm.sendRequest(requestOptions, (error, response) => {
if (error) {
console.log(error)
}
})

Good luck !

Here is a code sample in the Postman Answers workspace of how to GET an environment and PUT update the environment. Look under the Pre-request Scripts and Tests tab.

1 Like