You can modify the initial value of a variable from a script using the Postman API. This article explains how to use the endpoint Update Environment, which consists of replacing 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 Update Environment.
- Hover over the unresolved variable {{environment_uid}} in the URL and select Add new variable.
- Paste the Environment ID copied in step 1 in the field Value, choose a Scope and select Set variable.
- 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, and Value 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 request already using the environment and its variables.
- Select Send Request.
Note: The pm.environment.set
code snippet only exposes access to the current value of a variable.