Postman Tutorial Part 47 – Local or Temporary Variables in Postman

We have seen several types of variables in Postman already in previous posts. In this post, we will learn about Local or Temporary variable in Postman.

A Local or temporary variable is created in to memory only during run time and removed once execution is done. As scope of local variable is limited to duration of execution, we can only create and update it in Scripts of Postman.

We can use local variables for below scenarios:-

  1. Temporarily store a value to be used/reused in other calculation/requests.
  2. Printing/Debugging the request execution
  3. Override the global, environment, data and collection variables value as local variable will have highest priority always.

Creating local variables in Postman:-

We can create local variables in Postman in two ways:-

  1. Using JavaScript way ( var or let)
  2. Using pm sandbox APIs

Using JavaScript :-

See the JavaScript syntax below:-

This way is good if we want to store some variable temporary. Like you would like to add prices of all books. You need to have some variable to store current addition. You can create local variables in Pre-request Script and Tests.

There is an important points about local variables created through JavaScript. You can not use this variable outside of current scope. It means if we create a local variable in “Pre-request Script” of a request, we can not reuse it in Tests tab of same request and in Pre-request and Tests script of any other request and vice – versa. It will give reference error. It restricts scope of local variable again.

Try to use local variable created named “localVar” in Tests tab:-


Using Postman Sandbox api:-

If we want to have some temporary variable which can be used within collection or shared temporary variables across all request of collection, we can go for this way.

Syntax to create local variable using postman sandbox APIs:-


Syntax to get value of created local variable:-

pm.variables.get(“<varName>” )

Unlike JavaScript way of creating local variable, postman sandbox API way gives wider scope to local variable. We can use these local variables within collection.

We can use this local variable in other request as well. I just made a copy of above request.

But remember, if local variables are shared with another request, you must need to run using Collection runner. Otherwise it will give you undefined value not reference error like JavaScript way.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

1 thought on “Postman Tutorial Part 47 – Local or Temporary Variables in Postman

Leave a Reply

Your email address will not be published. Required fields are marked *