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:
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:
- 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.
- Another option is to select the value and store the variable by choosing the Global option as Type.
- Once done, we shouldn't forget to mark Save and confirm the changes.
We can also download the global variables and share them with others by going through these steps:
- First, let's go to Postman and select the environment type as Global in the sidebar.
- Then, we can click on the Export button that will export the variables to a file.
- We can store the file once ready by clicking 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:
- Firstly, we click on the Add a new variable, and Enter the variable name.
- Before saving the variable, we can select the variable type as the environment.
- The variable will need initial and current values to be specified.
- Once done, we have to click on 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
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:
- pm.globals: This method is used for defining the global variables in a request script, e.g., pm.globals.set(“variable_key”, “variable_value”);
- pm.collectionVariables: We can define a variable with scope as ‘collection' with this method, e.g., pm.collectionVariables.set(“variable_key”, “variable_value”);
- 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”);
- pm.variables: This defines the local variable with local/temporary scope, e.g., pm.variables.set(“variable_key”, “variable_value”);
- 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
We've stored the variables in scripts by using the above methods. Therefore, we can retrieve their current value using these methods:
- pm.variables.get(“variable_key”): This will access a variable at any scope including the local.
- pm.globals.get(“variable_key”): This can access a global variable
- pm.collectionVariables.get(“variable_key”): This can access a collection variable.
- 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:
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.
res – REST (eBook) (cat=REST)