How to dynamically set the initial value for a variable
Using the Postman API, you can modify the initial value of a variable from a script. This article explains how to use the endpoint Replace an environment’s data to replace an existing environment.
- Get the Environment ID of the target environment and copy it.
- Create a fork of the Postman API collection in your workspace.

- When the fork is created in your workspace, go to the Postman API collection > Environments > PUT Replace an environment’s data .
- Hover over the unresolved variable {{environmentId}} in the Path Variables Section.

- Paste the Environment ID copied in step 1 in the Enter Value field.
- Generate a Postman API key for your account and add it to the Authorization tab to authorize this request.
- Open the Body tab and duplicate the format to match the existing number of variables currently in your environment, plus the number of variables you would like to add.
- Enter the Name, Key, Value, Type, and enabled status for each of the variables.
- Note: The PUT request will delete everything that is not specified in the request, and only the key-value pairs that are passed will be stored in the environment. Make sure that any existing variables are added with the same name to avoid breaking any requests already using the environment and its variables.
- Select Send Request .
- Note: The
pm.environment.setcode snippet only exposes access to the current value of a variable.
Have more questions? Submit a request