# Set initial value of collection variables

**URL:** <https://community.postman.com/t/set-initial-value-of-collection-variables/56274>\
**Category:** Help Hub\
**Tags:** tests, variables, collections\
**Created:** [January 7, 2024, 5:11pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274 "2024-01-07T17:11:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 7, 2024, 5:11pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/1 "2024-01-07T17:11:07Z")

</div>

Hi team,

How can I provide and set initial values for the **collection variables** from test and pre-request scripts so they are configured during run time? I want to do this at the collection level, and change the **initial values** during runtime not their **current values.** Because when I export a collection the current values are not exported.

Please guide me

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 7, 2024, 5:42pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/2 "2024-01-07T17:42:33Z")

</div>

The only way that you can do that within a script, is to use the Postman API to update the Collection.

[https://www.postman.com/postman/workspace/postman-public-workspace/collection/12959542-c8142d51-e97c-46b6-bd77-52bb66712c9a?action=share&source=copy-link&creator=8213448](https://www.postman.com/postman/workspace/postman-public-workspace/collection/12959542-c8142d51-e97c-46b6-bd77-52bb66712c9a?action=share&source=copy-link&creator=8213448)

You can use the `pm.sendRequest()` from the script sandbox to send that request.

It’s either that or manually persist all the values before exporting the Collection.

 ![Screenshot_20240107_174438](https://us1.discourse-cdn.com/getpostman/original/3X/f/d/fd2f9ebafa9bf7cb11d2e40d41bb1a9691b45818.jpeg)

As you have found out already, the `.set()` will only update the current value. This is to prevent inadvertently syncing sensitive data like api keys or tokens.

---

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 7, 2024, 7:26pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/3 "2024-01-07T19:26:08Z")

</div>

Hi Danny,  
I will be triggering the postman API every 3 hours from azure by **exporting Postman collection**. Using Postman API would be most suitable for me in this case.

How can I update the collection using PostmanAPI? Any blogs or examples on this? There are postman blogs on updating the environment and using that to persist values. [Guide on How to Persist Postman Variables | Postman Blog](https://blog.postman.com/how-to-persist-postman-variables/#persist-variables-between-runs)  
Are these the same things?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 8, 2024, 11:13am UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/4 "2024-01-08T11:13:20Z")

</div>

Depending on the plan that you’re on, there are limits placed on how many calls that can be made to the Postman API within a month. Worth checking to see if that will cover your use case first.

You can get your full Collection JSON using this [GET Collection endpoint](https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-a6a282df-907e-438b-8fe6-e5efaa60b8bf) and the response will be required to update the Collection with the new initial variable values.

Using the [PUT Collection endpoint](https://www.postman.com/postman/workspace/postman-public-workspace/request/12959542-bc8b292b-ffbb-4c67-a2a1-2fc416e2aef8), you can use the Collection JSON as the request body and amend the `variable` value in the structure to set the initial state.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/7/476b9e8d6ae2fa5908c7f4b3d411358007163377.png)

You will need to ensure that the full Collection JSON is passed in the request when updating or this will replace the whole Collection with what it sent.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 8, 2024, 3:05pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/5 "2024-01-08T15:05:01Z")

</div>

After talking to the team, I realised I gave you the wrong method of doing this. ☹

Using the `PATCH /collections/id` endpoint is the correct way of doing this and you would only need to pass in the variable information and not the whole collection.

Request Body Example:

```auto
{
    "collection": {
        "variables": [
            {
                "key": "id",
                "type": "integer",
                "value": 1111
            }
        ]
    }
}

```

A full example of this can be found here:

[https://www.postman.com/postman/workspace/postman-public-workspace/example/12959542-beac0544-499c-4aad-ae7e-f0be7a1522db](https://www.postman.com/postman/workspace/postman-public-workspace/example/12959542-beac0544-499c-4aad-ae7e-f0be7a1522db)

---

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 8, 2024, 10:43pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/6 "2024-01-08T22:43:45Z")

</div>

Hi Danny,  
Thank you for the message, however I am getting an error with the Patch script.

1. First I executed **GET Collection endpoint** and got the response.
2. Then I ran the second script \*\* PATCH /collections/id \*\* with the response from above script, that is

```auto
{
    "collection": {
        "variables": [
            {
                "key": "ID",
                "value": "",
                "type": "string"
            },
            {
                "key": "alertIDnow",
                "value": ""
            }
        ]
    }
}

```

the values of the collection variables ID and alertIDnow are in the current values. I am trying to get them into the initial value. I got the above response from running the first script.

When I ran the 2nd script I got the below error:

```auto
{
    "error": {
        "name": "badRequest",
        "message": "Parameters required: ('type') for key: 'collection.variables'"
    }
}

```

How can I resolve this error?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 8, 2024, 10:46pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/7 "2024-01-08T22:46:03Z")

</div>

Isn’t it telling you what the error is here?

The second object is missing the `type` property.

---

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 14, 2024, 5:54pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/8 "2024-01-14T17:54:07Z")

</div>

Hi Danny,

I have taken the above steps as recommended and my collection is working on postman as expected.  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/4/04793b53c04a285c777dc554f53e687f8ed33202.png)

But how can I confirm that initial values are being set? I cannot see the values being updated to the initial value column for collection variables **ID** and **alertIDnow**. The initial value is still empty and i believe that even after taking the recommended steps my Postman collection is still running only on current values, as previously.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/0/101bd5dfce6c1c6085e774e13f8a9af986b2859e.png)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/0/6/0652942f49834da7a82e308dcf3e842da823a280.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 15, 2024, 10:58am UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/9 "2024-01-15T10:58:45Z")

</div>

I can’t answer why its not showing in the GUI as I don’t use this feature.

What status code are you getting for that patch request?

How about it you log out and back on, do the initial values show then? (Is it a refresh issue?).

On a side note, the current values if they exist will always be what is used in requests.

The initial values are only pulled across if the current values are blank.

If you have initial and current values, then its the current values that are used.

---

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 15, 2024, 2:41pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/10 "2024-01-15T14:41:19Z")

</div>

![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/5/e5bb2b37f01e6903082b39ea504c5a87f612ca1e.png)  
The Patch request is working. All the scripts and everything is working as per the recommendations above yet the initial value is not being updated for collection variables.

I am doing this as I am importing the Postman collection onto Azure logic app. I have been told that Azure picks on the initial values for collection variables hence i need initial values to show up. And with the present configurations azure is still not getting collection variable values.

---

<div class="post-metadata">

**Author:** ![anchalsingh12](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anchalsingh12](https://community.postman.com/u/anchalsingh12)\
**Post date:** [January 15, 2024, 2:54pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/11 "2024-01-15T14:54:43Z")

</div>

I get an error 404 on azure as the collection variable value for **alertIDnow** can be fetched in the requested **GET URL**

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/4/2457187d9bf00af8ad0492a1958495f14801cfc7.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 15, 2024, 3:07pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/12 "2024-01-15T15:07:01Z")

</div>

I’ve just tested a quick patch request to one of my collections and it shows immediately after you refresh the page. (This is using the web app).

```auto
{
    "collection": {
        "variables": [
            {
                "key": "var1",
                "value": "value3",
                "type": "string"
            },
            {
                "key": "var2",
                "value": "value4",
                "type": "string"
            }
        ]
    }
}

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/2/22d776f0882f00928e80496ac40266fde5901536.png)

If the variables don’t exist, then it will create two new ones and copy the initial value to the current value.

If the variables exist, then it will only update the initial value.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [February 14, 2024, 3:07pm UTC](https://community.postman.com/t/set-initial-value-of-collection-variables/56274/13 "2024-02-14T15:07:29Z")

</div>

Closed on February 14, 2024, 3:07pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
