Set initial value of collection variables

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

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

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.

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.

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
Are these the same things?

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 and the response will be required to update the Collection with the new initial variable values.

Using the PUT Collection endpoint, you can use the Collection JSON as the request body and amend the variable value in the structure to set the initial state.

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.

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

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:

{
    "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

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
{
    "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:

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

How can I resolve this error?

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

The second object is missing the type property.

Hi Danny,

I have taken the above steps as recommended and my collection is working on postman as expected.
image

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

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.


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.

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

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).

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

image

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.

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