A Guide to Variables in Postman – Postman中的变量指南

最后修改: 2022年 10月 3日

1. Overview


In Postman, variables let us save the values and use them as required in different APIs. We can reference these values by passing them to collections, environments, or any requests. It basically makes it easier to access stored values using these variables more efficiently. We can also collaborate with multiple teammates or set up dynamic workflows over Postman using these variables.


In this article, we’ll understand variables in-depth, their types, and how we can embed them into our APIs.


2. What Are Variables?


Variables symbolize the data representation and let us access a value without manually entering it wherever we need it. They can be useful when we’ve to use the same value in multiple places. Some instances include base URL, token, and path variables. Further, using them also makes the requests more flexible as changing variables will automatically make changes at all places these variables are used.


We can define variables as key-value pairs in Postman. The key defines the variable name that allows us to access its value directly using this key.


In addition, one can also use environments for setting up a group of variables. These variables will have different values depending on the environment.


Let’s dive deep into the variables and understand the different variable scopes.


2.1. Variable Scopes


There are multiple scopes that let us process the development and testing of API in various environments with different values. Below are the variable scopes defined from the broadest to the narrowest area covered:


  • Global variables: These are accessible throughout the workspace and have the broadest scope in Postman. They can be used anywhere among multiple requests and collections within the workspace.
  • Collection variables: These variables are accessible only inside a certain collection. They are available across all the requests within a collection. Also, they don’t change based on the selected environment.
  • Environment variables: These variables let us scope the work as per the different environments. They change along with the change in the environment we are working on like local environment, staging or the production environment.
  • Data variables: These types of variables are external and define the data sets while running collections with the Collection Runner. We can extract this from a CSV or a JSON file. They have current values that don’t persist after a request or collection executes.
  • Local variables: These variables are also known as temporary variables that are only accessible through a request script. They have scope only till the current request or collection. Once the execution completes, they are no longer available.

2.2. Variable Types


Global and Environment variables can further be classified based on their types. Here’s how we can configure them:


  • Default type: Variables are of these types by default. These are shown as plain text with no additional properties.
  • Secret type: These variables hide the initial and current values for all the workspaces, similar to a password. They let us prevent any unintentional disclosure of sensitive data.

3. Defining Variables With Scopes


We can define variables in multiple types and with scopes as we discussed above. Variables can be defined at any scope in the request builder.


Firstly, we select the text and click on Set as a variable. Once done, we have to store it as a new variable. Then, we enter a key for the value and select from the different scopes available in Postman:

首先,我们选择文本并点击Set as a variable。一旦完成,我们必须将其存储为一个新的变量。然后,我们为该值输入一个键,并从Postman的不同作用域中选择。


3.1. Setting the Response Body as Variables


The values from the request’s response body could also be set up into variables and used further in the collection. This helps in running multiple requests in sequence and creating a specific flow.


Firstly, let’s select the text we want to save and press right-click or the Control-click button to store it as a variable.


Once done, we have to choose among the relevant scope from the possible options, i.e., environment, local or global, and name the variable to save it:



3.2. Defining Global Variables


Let’s look at the steps for setting up global variables in Postman:


  1. We start by clicking on Add a new variable in the global variable section, and then entering the variable name to store it as a global variable directly.
  2. Another option is to select the value and store the variable by choosing the Global option as Type.
  3. Once done, we shouldn’t forget to mark Save icon Save and confirm the changes.

We can also download the global variables and share them with others by going through these steps:


  1. First, let’s go to Postman and select the environment type as Global in the sidebar.
  2. Then, we can click on the Export button that will export the variables to a file.
  3. We can store the file once ready by clicking Save icon Save on the system, and sharing it as required.

3.3. Defining Environment Variables


We can go through the following steps to save environment variables in Postman:


  1. Firstly, we click on the Add a new variable, and Enter the variable name.
  2. Before saving the variable, we can select the variable type as the environment.
  3. The variable will need initial and current values to be specified.
  4. Once done, we have to click on Save icon Save to confirm the changes.

3.4. Defining Collection Variables


Postman also provides the opportunity to add collection variables and use them as required.


We have to click and select Collections in the sidebar. Once done, let’s select the Variables tab to store the collection variables:



3.5 Defining Variables in Scripts

3.5 在脚本中定义变量

Similar to defining variables in a collection, an environment, or globally, we can also set variables programmatically in our request scripts.


All of these methods take in (variable_key, variable_value) as input:

所有这些方法都接受(variable_key, variable_value)作为输入。

  1. pm.globals: This method is used for defining the global variables in a request script, e.g., pm.globals.set(“variable_key”, “variable_value”);
  2. pm.collectionVariables: We can define a variable with scope as ‘collection’ with this method, e.g., pm.collectionVariables.set(“variable_key”, “variable_value”);
  3. pm.environment: This can be used for defining an environment variable with scope as current environment, e.g., pm.environment.set(“variable_key”, “variable_value”);
  4. pm.variables: This defines the local variable with local/temporary scope, e.g., pm.variables.set(“variable_key”, “variable_value”);
  5. unset: This method can be used for removing a set variable. The unset can be defined with the above variable instances as per their scope, e.g., pm.environment.unset(“variable_key”, “variable_value”);

3.6 Using Variables in Scripts

3.6 在脚本中使用变量

We’ve stored the variables in scripts by using the above methods. Therefore, we can retrieve their current value using these methods:


  1. pm.variables.get(“variable_key”): This will access a variable at any scope including the local.
  2. pm.globals.get(“variable_key”): This can access a global variable
  3. pm.collectionVariables.get(“variable_key”): This can access a collection variable.
  4. pm.environment.get(“variable_key”): This can access an environment variable.

Here, we can retrieve the values using variable_key. The object represents the scope level and the get() method retrieves the value.


The pm.variables.get() method also provides the option to change variable scopes without actually affecting the script functionality. It returns the variable that currently has the highest precedence.


4. Using Variables


Now, we’ve defined different variable scopes and learned how to store them in Postman. Let’s see how to use these variables in our request paths and bodies.


For referencing a variable throughout Postman, we should use double curly braces around the variable’s name. It automatically fetches the value from different variables as defined. This is standard across all of Postman and doesn’t depend on variable types and scopes:



Additionally, running CURL or hitting the request resolves the variable and updates the variable with the current value. The below request URL references the variable:



Let’s assume studentName is “John”. Postman retrieves the value from its variables and sends whatever value that’s currently stored there. Therefore, on hitting the above request over Postman it automatically converts it to the request:

让我们假设studentName是 “John”。Postman从其变量中检索该值并发送当前存储在那里的任何值。因此,在将上述请求打到Postman上时,它自动将其转换为请求。


5. Conclusion


In this tutorial, we used Postman variables for basic initialization using the GUI mode.


In addition, we learned about their types and scopes. Also, we looked at the different places they can be initialized such as URIs, request body, and headers. Evidently, this lets us save time and space by not initializing the same values multiple times.