Persisting (Global/Environment) Variables During Runtime

How do I persist Global variable values programmatically?

I was searching for this everywhere and I found this:
How to persist variable values - Only some of the current values

However, the link it references (Update Environment endpoint of Postman API) leads to a Request not found in the Postman Public Workspace.

Does anybody have experience doing this? Is the coding example in the page above still valid? The page seems to have been updated 3 months ago but the fact that it leads to a non-existing request does not inspire a lot of confidence in me…

Thanks a lot in advance!
BR,
Xavier.

Hey @xavier.gutierrez :wave:

This link shows how you can update / persist your environment variables, if you see the code, in the body’s raw you update the Environment name and values:

  raw: JSON.stringify({
            'environment': {
            'name': 'New Env Name',
            'values': [
            {
             'key': 'var-one',
             'value': 'one'
            },

I am not sure if the same will work Global variables, if you want to update the global variable, just use the ‘pm.globals.set’ method with the same global variable name, and it will update the variable.

For this link, seems like the request ID has changed, this is the new link to the public workspace’s request:
Update Environment

Hope it helps,
Good luck!

Hey! Thanks for the reply. We are indeed using pm.globals.set but that only sets the current value of the global variables, not the initial value (i.e. it is not persisted).

Will try the PUT request to the Postman API. I infer from your answer that you do not know if it will take ‘Globals’ as the environment name and effectively update the global variables, right?

Thanks!

Just to clarify,

const requestOptions = {
   url: 'https://api.getpostman.com/environments/<environment_uid>',
   method: 'PUT',
   header: {
       'X-API-Key':'<your-API-key>'
   },
   body: {
       mode: 'raw',
       raw: JSON.stringify({
           'environment': {
           'name': 'New Env Name',
           'values': [
           {
            'key': 'var-one',
            'value': 'one'
           },
           {
            'key': 'var-two',
            'value': 'two'
           }]
           } 
           })
   }
}

pm.sendRequest(requestOptions, (error, response) => {
if (error) {
console.log(error)
}
})

The above request is used to update the Environment which you specify in the ‘url’ with the <environment_uid>.
Here: raw: JSON.stringify({ 'environment': { 'name': 'New Env Name',
If you set the environment name as ‘Globals’, it will update the environment with the name Globals, but the variables you set will be the environmnet variables and not global variables.

Ususally when you have Environment variables and you would like to update/persist them manually, you can use the above set of code, but for global variables I am not aware/sure if the same can be used to update and persist the global variables as they are seprate from environment variables.


More about variables scope here: https://learning.postman.com/docs/sending-requests/variables/#choosing-variable-scope

Sorry if i made it more complex for you.