Set variable's current value instead of initial value in script


Is it somehow possible to set in a pre-request script the current value of a global/environment variable instead of the initial value?

My script:

const refreshRequest = {
  url: '' + pm.globals.get("webApiKey"),
  method: 'POST',
  header: 'Content-Type:application/json',
  body: {
    mode: 'application/json',
    raw: JSON.stringify(
        	grant_type: 'refresh_token',
        	refresh_token: pm.globals.get("refreshToken")

var getToken = true;

if (!pm.globals.get('expirationTime') || 
    !pm.globals.get('idToken')) {
    console.log('Token or expiry date are missing')
} else if (pm.globals.get('expirationTime') <= (new Date()).getTime()) {
    console.log('Token is expired')
} else {
    getToken = false;
    console.log('Token and expiry date are all good');

if (getToken) {
    pm.sendRequest(refreshRequest, (err, res) => {
        console.log(err ? err : res.json());
        if (!err) {
            console.log('Saving the tokens and expiry date')
            var responseJson = res.json();
            pm.globals.set('idToken', responseJson.id_token)
            pm.globals.set('refreshToken', responseJson.refresh_token)
            var expiryDate = new Date();
            expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in);
            pm.globals.set('expirationTime', expiryDate.getTime());

As you can see I have several pm.globals.set calls in the script, which set rather security relevant variables, like id and refresh tokens.
I don’t want these variables to be synced to all colleagues.
When I run pm.globals.set('refreshToken', responseJson.refresh_token) this does not only set the current value, it set’s the initial value as well, what I try to prevent.

How can I achieve this?

I hope for some good tips,

Hi Dennis,

From any script (collection/request pre-request/test), it is only possible to set the current value. Depending on your setting auto persist variables values the modified current value will be copied to the initial value. If that is something you don’t want, you can turn it off.

Here’s a simple flowchart from our docs explaining it visually

Here’s where you can find this setting


Oh, thanks,

this is exactly what I was looking for :slight_smile:

I looked in the documentation for something like this.
Unfortunately, the documentation is even if it’s easily understandable, sometimes a bit unintuitively organized.

I often had to look through a lot of pages to find the relevant info.
I hope that Postman gets it’s already good documentation as good as the awesome Postman product! :smiley:

1 Like

Thanks, Dennis

Sure, I will let our team know about this. We are currently working on improving our docs.

Is there a way to enforce this setting for a shared workspace?
Otherwise if anyone in the team forgets this setting, tokens might be uploaded right (if you have a test, which uses setEnvironmentVariable)?

@clemens.peters currently there’s no way to enforce this for a shared workspace.
There’s already a feature request which has been filed for this.

You can subscribe to the thread for any future updates around that.