How to dynamically set the initial value for a variable

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.

  1. Get the Environment ID of the target environment and copy it.
  2. Create a fork of the Postman API collection in your workspace.
  3. When the fork is created in your workspace, go to the Postman API collection > Environments > PUT Update Environment.
  4. Hover over the unresolved variable {{environment_uid}} in the URL and select Add new variable.
  5. Paste the Environment ID copied in step 1 in the field Value, choose a Scope and select Set variable.
  6. Generate a Postman API key for your account and add it to the Authorization tab to authorize this request.
  7. 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.
  8. 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.
  9. Select Send Request.

Note: The pm.environment.set code snippet only exposes access to the current value of a variable.

Have more questions? Submit a request