Postman env: set only current value with Postman API

I would love to streamline the on-boarding process for new users on my team.

Requirement: We keep all environment variables in the current value column to avoid syncing the values to the cloud and protect sensitive information.

What we currently do: The new dev manually creates 5 different environments, then switches to each one and runs some scripts given to them by a team mate in the “pre-request script” tab. This populates all variables for that environment. Then they repeat this process for each environment.

What I want to do: Use the Postman Api to create a collection that makes this process simpler. Ideally one request that creates many collections and populates ONLY the current values.

Is there a way to set variables in environments that are not selected?
Can you set ONLY the current value with the API?

I’m open to suggestions.

Hey @KrisBr,

Welcome to the community! :star2:

Creating those different environment files with placeholder variable names should be easy enough using the Create Environment endpoint on the Postman API. You could create a Collection and run that in the Collection Runner or with Newman.

POST https://api.getpostman.com/environments?apikey=<yourAPIKey>

The Request Body could be something simple like:

{
    "environment": {
        "name": "{{envName}}",
		"values": [
			{
				"key": "{{varOne}}",
				"value": "",
				"enabled": true
			},
			{
				"key": "{{varTwo}}",
				"value": "",
				"enabled": true
			}
		]
    }
}

You can then create a Data File containing the different names and the Runner would iterate over those and fill in the variable placeholders in the request Body and create the Environments.

Something simple like this:

[
  {
    "envName": "Environment One",
    "varOne": "username",
    "varTwo": "password"
  },
  {
    "envName": "Environment Two",
    "varOne": "username",
    "varTwo": "password"
  },
  {
    "envName": "Environment Three",
    "varOne": "username",
    "varTwo": "password"
  }
]

I think you’re going to struggle to add only current values, using the Postman API though.

Those values are only supposed to be known locally so once you add these to a request, which calls the Postman API - Those values are no longer just known locally and are synced to the cloud.

1 Like