# Persisting (Global/Environment) Variables During Runtime

**URL:** <https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879>\
**Category:** Help Hub\
**Tags:** variables, pre-request-script, globals\
**Created:** [January 25, 2022, 2:09pm UTC](https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879 "2022-01-25T14:09:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier.gutierrez](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xavier.gutierrez/32/10944_2.png) [@xavier.gutierrez](https://community.postman.com/u/xavier.gutierrez)\
**Post date:** [January 25, 2022, 2:09pm UTC](https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879/1 "2022-01-25T14:09:23Z")

</div>

**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](https://support.postman.com/hc/en-us/articles/4409005403031-How-to-persist-variable-values-#If-you-want-to-persist-only-some-of-the-current-values)

However, the link it references ([Update Environment endpoint of Postman API](https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-ece4c556-18ee-4baa-ac05-9c492caf6a97?ctx=documentation)) 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.

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [January 26, 2022, 10:19am UTC](https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879/2 "2022-01-26T10:19:44Z")

</div>

Hey @xavier.gutierrez 👋

> [@xavier.gutierrez](#):
>
> I was searching for this everywhere and I found this:  
> [How to persist variable values - Only some of the current values](https://support.postman.com/hc/en-us/articles/4409005403031-How-to-persist-variable-values-#If-you-want-to-persist-only-some-of-the-current-values)

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:

```auto
  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.

> [@xavier.gutierrez](#):
>
> However, the link it references ([Update Environment endpoint of Postman API](https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-ece4c556-18ee-4baa-ac05-9c492caf6a97?ctx=documentation)) leads to a **Request not found** in the Postman Public Workspace.

For this link, seems like the request ID has changed, this is the new link to the public workspace’s request:  
[Update Environment](https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-9f175bea-1df5-44e6-af33-bf2de7f85bb5)

Hope it helps,  
Good luck!

---

<div class="post-metadata">

**Author:** ![xavier.gutierrez](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/xavier.gutierrez/32/10944_2.png) [@xavier.gutierrez](https://community.postman.com/u/xavier.gutierrez)\
**Post date:** [January 26, 2022, 11:54am UTC](https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879/3 "2022-01-26T11:54:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bbahadur](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@bbahadur](https://community.postman.com/u/bbahadur)\
**Post date:** [January 26, 2022, 2:42pm UTC](https://community.postman.com/t/persisting-global-environment-variables-during-runtime/31879/4 "2022-01-26T14:42:52Z")

</div>

Just to clarify,

```auto
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.

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/5/5c01a033b2b5b73bbacf3f7b784f8a181a0f21eb.png)  
More about variables scope here: [https://learning.postman.com/docs/sending-requests/variables/#choosing-variable-scope](https://learning.postman.com/docs/sending-requests/variables/#choosing-variable-scope)

Sorry if i made it more complex for you.
