Postman Managing Variable Values Automatically

By Jasmeet Singh
Date 25-12-20
Postman Managing Variable Values Automatically
" startxlabs"

Postman Managing Variable Values Automatically 

A variable is a symbolic name for - or a reference to - information. The variable’s name represents what information the variable contains. Variables allow you to store and reuse values in your requests and scripts. The type of variables in Postman might be confusing as there are quite a few of them, such as environment variables, collection variables, data variables etc. Similar to working with variables in other programming languages, in Postman, all variables conform to a specific hierarchy.

Scope is nothing but the accessibility of a variable. Using variables within specific scopes allows you to reuse values efficiently. All variables defined in the current iteration take precedence over the variables defined in the current environment, which overrides the ones defined in the global scope.

Choosing variables:

  • Global variables allow you to access data between collections, requests, test scripts, and environments. Global variables are available throughout a workspace.
  • Collection variables are available throughout the requests in a collection and are independent of environments, so do not change based on the selected environment.
  • Environment variables allow you to tailor your processing to different environments, for example local development vs testing or production. Only one environment can be active at a time.
  • Local variables are temporary, and only accessible in your request scripts. Local variable values are scoped to a single request or collection run, and are no longer available when the run is complete.

  • Data variables come from external CSV and JSON files to define data sets you can use when running collections via Newman or the Collection Runner.



Manage Variable Values automatically before/after every API request

Once we know what postman are variables and what their respected scopes are. We can easily write a script to manage those variable values automatically. So, suppose you are using a Token based authentication (like: JWT) in your API’s and whenever you call the API for getting a new Access Token you need to manually copy the value and paste into the respected variable value or you need to write a small test case so that whenever the API gives any response you can validate the response values to test the API is giving the expected values in the response or not etc. We can automate that behavior by writing a set of statements. These statements are written in JavaScript but if you don’t know the language then no problem because you can still continue implementing as they are very easy to write statements.


Pre-request Script: Now, if you want to do something like removing the variable value every time before the API request is sent. You can write the code for that by going to the Pre-request Script section. Like this:


Post-request Script: For performing any operation after the API gives a response like setting the token value into the variable directly. We can write the code for that by going to the Tests section. Like this:

We can access the API response by using this statement:

var jsonData = pm.response.json();

And, We can access different types of variables whether they are environment variables, collection variables etc. Using these statements:


We can get or set a variable value using .get and .set methods:

For get:


For set:

pm.environment.set("enviorment_varriable", "value");

We can also check if the value is set successfully in the variable or not by printing the result on the console (Console is given at the bottom in the postman application. You can output or print anything on the consoles by writing console.log("Anything");)



Testing: We can also write some small test cases which would tell us that  the API has given the expected data or not. Like this:

Checking that the API has given a 200 HTTP Status Code or not:



For viewing the Test results you need to navigate to the Test Results section

Setting the Variable value only when API give a 200 HTTP Status Code:


Checking the Response Time of an API:



Postman variables allow you to store and reuse values in your requests and scripts in an effective way. Which variables you use and how you use them can benefit your testing by knowing which scope to use and when to provide a more readable and logical way of accessing data, And by writing a script to manage variables automatically based on the response the API give, Development and testing on postman becomes much more easier and smoother than before.

subscribe to startxlabs