How to persist Postman variables
In programming, variables allow you to store and reuse values throughout your code. You can also use variables in Postman to define and reference values in your collections, environments, requests, and scripts. Variables help you work efficiently, collaborate with teammates, and set up dynamic workflows.
Once you learn the basics of variables in Postman and browse these 10 tips for working with Postman variables, you will see there are many different ways to work with variables in Postman, just like in programming. Let’s reward your persistence, and examine a few scenarios for persisting variable values.
- Persist variables between local sessions
- Persist variables throughout a run
- Persist variables between runs
Persist variables between local sessions
When you edit global, collection, and environment variables in Postman, you have Current Value and Initial Value. The current value exists within your current session. The initial value persists between sessions.
As you’re working locally within your session, the current value takes precedence over the initial value. This is helpful when working with temporary values, or keeping secrets in a team workspace.
If you log into a different session, you will not have access to the previous session’s current values. To persist values between sessions, you can update the initial value to match the current value. In the variable editor, you can Persist or Reset variables individually or all at one time.
Persist variables throughout a run
If you want to persist a variable throughout a collection run, but don’t need access to the variable afterwards, you can use a local variable. These are temporary variables accessed in your request scripts using pm.variables.set(“variableName)
and pm.variables.get(“variableName”)
. This is helpful when you need a value to override all other variable scopes throughout a run, and does not require teardown after the collection run.
If you’re running a collection in the collection runner and want to persist the variable used in the run, select a configuration option to Keep variable values. Deselecting this option means any changes to variable values will not be saved after the run completes.
Try it out in Postman by forking the example collection to your own workspace.
Click the Run in Postman button to fork the example collection to your own workspace:
If you’re running a collection using a collection-based monitor, you can access and update global and environment variables during a monitor run. But those variables will immediately revert to their original values after the collection is finished running. In the next section, we will learn how to persist variable values between runs.
Persist variables between runs
If you want to persist variables from run to run, there’s a few options whether you’re using the collection runner, a collection-based monitor, or running a collection from your command line or CI/CD using Newman.
You can always store data in a database or your local file system by adding a request script or a separate API call within your collection to store the data. You can also persist variables between runs in Postman. Updating variables during the collection run updates the current value only. You can then Persist variables manually in the variable editor (as previously described) and update the initial value to match the current value. Doing this will persist variable values between runs.
To programmatically persist variables in a collection-based monitor or while using Newman, you can include a separate API call to the Postman API within your collection to update the environment storing the variable. Let’s walk through an example scenario and break it down step-by-step.
Fork this collection Persist variables in monitor and environment bitcoin to your own workspace, or click the Run in Postman button below, to try it out in Postman.
Click the Run in Postman button to fork the collection and environment to your own workspace:
- Postman API key: Let’s use the Postman API to access and update our Postman data. Update the environment variable
postman-api-key
. Postman API keys can be found here. Do not leak your secrets! Make sure to use a personal workspace if you’re running a collection-based monitor. Make sure to manage your secrets if you’re using Postman from the command line or as part of your CI/CD pipeline. - Environment UID: Once you fork the collection to your own workspace, find the ID of the environment to update in the context bar to the right. Then update the
environmentUid
environment variable. - Step through each API request: Review the code in the pre-request and test scripts of each request within our collection. Walk through each request, one at a time, and hit Send to see how the variables are updated.
What’s happening in the scripts?
1. GET
Bitcoin: this request retrieves the current rate of bitcoin. Under the Tests tab, the code updates the current value of an environment variable called btc
.
2.GET
Get environment: this request retrieves the environment you forked to your workspace. Inspect the response to see how an environment is formatted, especially the values
array. We will persist the value of a variable called btc
in the next step.
3. PUT
Update environment: this request updates the environment, so we can programmatically persist variables between collection runs.
In our example, we are persisting a single variable called btc
. If we want to persist every variable, we can loop through the values array to update all the variable values. We can also select specific variables to persist.
Let us know in the comments below if I’ve missed your favorite way to persist (or forget) variables.
How to persist these when using postman CLI?
Please contact our support team at http://www.postman.com/support and they’ll be able to help you.