Open In App

How to Create Variables in Postman with Different Scopes

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

If you use Postman for testing your APIs and want to learn how to declare variables in it then this article is going to be helpful for you. In this article, you will learn how you can declare variables in Postman step by step. You will also learn about the various scopes of variables, their types, and their use cases in different scenarios. So deep dive into this article and master the variables in Postman.

Introduction to Variables in Postman

Suppose you are using the same base_url for testing various end points of your API. Then you can define that base URL in a variable and then make requests by just changing the end points of your API. Follow the example below for a better understanding.

Let us consider the following base URL -> https://geeksforgeeks.com. Now if we define this base URL in a variable main_url then we can make several different requests using the same variable.

  1. {{main_url}}/get will be converted to https://geeksforgeeks.com/get
  2. {{main_url}}/post will be converted to https://geeksforgeeks.com/post
  3. {{main_url}}/put will be converted to https://geeksforgeeks.com/put
  4. {{main_url}}/patch will be converted to https://geeksforgeeks.com/patch

So, from the above example, it is clear how a variable can reduce the overhead of the tester of writing the same code when the request is being made via the same URL with a different endpoint.

Why Should We Use Variables?

We must use variables in Postman for the following reasons:

  1. Reusability: Variables promote code reusability by eliminating the need for hard-coded values in scripts and requests. Instead of manually updating values throughout the collections in Postman the testers can update values in multiple environments in one go. This reduces the overhead of testers making it easier to maintain and update the APIs.
  2. Dynamic Data Handling: Variables allow testers to store and reuse the data dynamically. This is very useful when the data changes during the testing runtime.
  3. Global Accessibilityenvironment-specific: Variables declared under the global scope in Postman are available throughout the entire workspace. This allows testers to share variables across multiple environments and collections.
  4. Environment-Specific-Configuration: Postman allows testers to define variables at different scopes such as the global, local or environment-specific. This provides flexibility to configure requests in different environments like development, testing and production.
  5. Efficient Debugging: When various issues arise during testing variables can be used for easy debugging. Variables make it easier to debug and troubleshoot issues in the API.

How to Define a Variable in Postman?

For better understanding we are going to use this demo API https://reqres.in/api you can replace this with your API to perform testing with variables.

To define variables in Postman follow the below steps.

Step 1: Type or paste your API in the request tab.

DeclareVariable_Step1

Step 2: Select the type of your request.

DeclareVariable_Step2

Step 3: Select the data you want to store in the variable and click on set as a variable.

DeclareVariable_Step3

Step 4: Click on set as a new variable.

DeclareVariable_Step4

Step 5: Enter your variable’s name and select the variable’s scope. Here we will select global.

DeclareVariable_Step5

Step 6: Then click on the set variable.

DeclareVariable_Step6

Step 7: Now you will see your data is replaced with your variable inside {}. Now click on send to test your API and fetch the data.

DeclareVariable_Step7

Scope of Variables in Postman

Scope defines the context in which a variable is defined and determines where the variable can be accessed. In Postman variable scopes play a crucial role in managing and controlling the visibility and accessibility of the variables throughout the workspace. There are several types of scopes in Postman that provide accessibility according to your needs.

ScopeOfVariables_Postman

Scope of variables in Postman

  1. Global Variables: Global variables have the largest scope throughout the workspace. A variable declared as global can be accessed from anywhere by anybody. Global variables are mainly used when we want to share the data across multiple collections and environments.
  2. Collection Variables: Collection variables don’t change based on the selected environment. They are available throughout the requests in a collection and are independent of environments. These variables are suitable when you are working in a single environment.
  3. Environment Variables: These variables are specific to a single environment only. A variable declared as an environment variable can only be used within the current environment they are not accessible outside the environment.
  4. Data Variables: Data variables come from external sources like CSV or JSON files. Data variables have current values, which don’t persist beyond request or collection runs.
  5. Local Variables: Local variables are temporary variables that are accessed in the request scripts. Local scope variables are used within a specific request, script, or test section and they have the narrowest scope and are accessible only in the context where they are defined

Type of Variables

There are generally two types of variables supported in Postman default and secret.

1. Default Variables

This is the default type of all the variables in Postman, it is automatically assigned when a variable is declared. In this type, the variables are presented in the plain text form normally and don’t have extra properties. You can create default variables in the Postman app by going to the “Variables” tab in the sidebar and adding the desired variables.

2. Secret Variables

This type of variable was introduced in Postman to protect the disclosure of sensitive data in some scenarios. Secret variables hide the initial and current values of the variables for all the workspace members. If the user doesn’t want to disclose some parts of the data during testing then he/she can declare the variable as secret.

Follow the below steps to create a secret variable in Postman:

Step 1: Click on the Environments tab.

DeclareSecretVariable_step1

Step 2: Click on Globals.

DeclareSecretVariable_step2

Step 3: Under globals, all variables will be visible. Click on the type of variable you want to make a secret and select the secret option from the dropdown.

DeclareSecretVariable_step3

Step 4: Now you will see your data will be hidden and your crucial information will not be disclosed if the variable is declared as a secret variable.DeclareSecretVariable_step4

Note: It’s important to note that while regular variables are visible in the Postman UI, secrets are kept confidential and are not displayed in the Postman app.

Conclusion

In this article, we have learned the importance of variables in postman. We learnt how we can declare variables of different types. We understood the concepts of scopes of variables and learned when to use variables with a suitable scope. We also learned how to create secret variables to protect our sensitive data. We hope that this article has helped you to understand the variables in Postman.

FAQ’s

1. Can we change the value of a global variable during the runtime?

Yes, you can update the value of the global variables in environment manager during the runtime.

2. Is it possible to export and import environments with variables?

Yes, Postman allows users to export and import environments along with the variables. This ensures that the data can be shared between different teams.

3. What will happen if a variable with the same name exists in the local and global scope?

The local scope variable will take precedence over the global scope variable and the value of the global scope variable will be used.

4. Are variables limited to storing simple values like string only?

No, variables can also be used to store values like arrays, integers and objects as well.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads