Postman Variables

Shiljo Paulson
5 min readDec 11, 2021

As we all know Postman is an application used for testing APIs. In short an application for maintaining & running unit tests. Over the years it has evolved and introduced mocking servers, integration with Pipelines, support for GraphQL queries, workspace & many other features. Along with its evolution it has introduced various types of variables. Most of you might be aware of Global & Environment variables but there are many other types.

Photo by Kristina Tripkovic on Unsplash

Here we are going to see about various types of variables & when to use which variable types. Here are the various types variables some are based on scope & some are based on the purpose. Those who are familiar with scope you can skip to last section (Session variable)

  • Global
  • Collection
  • Environment
  • Data
  • Local
  • Session

Scope

Here is how the scope of Postman Variable looks like but you might not find Session Variables in the below diagram which I have answered at the last section.

Postman Variable Scope

Global Variables

The word global means it is accessible everywhere & its no rocket science for any programmer 🧑‍💻

  • Use it when you really want values to be shared across multiple collections.
  • It can be overridden by Environment, Collection, Data & Local variables.
  • You should only use them sparingly meaning try to avoid using it.
  • Values can be modified using the scripts.
  • Clear or Remove variables when not needed.

There are two ways by which you can add, edit & remove

At the right side of the Postman application, watch out for eye icon there if you click add then you can add your variables

Another way is by using the scripts & here is how the syntax looks like

Collection Variables

  • Tied to the collection and cannot be shared across multiple collections.
  • It can be overridden by Environment, Data & Local variables.
  • Values can be modified using the scripts.

Just like Global variables there are two ways by which you can add, edit & remove the collection variables

Just click on any collections then which you usually find at the left side of the Postman application then choose variables tab and then there you can perform everything.

Choose Variables tab from Collection

Another way to perform is by using the scripts & here is how the syntax looks like

Environment Variables

As the name suggest incase if you have multiple environments like local, development, testing and production it can have different URLs.

  • Use it when you want to things to be specific to the environment like URLs etc...
  • It can be overridden by Data & Local variables.
  • Avoid mixing Global & Collection variables.
  • Values can be modified using the scripts.

Similar to Global variable there are two ways by which you can add, edit & remove

At the right side of the Postman application watch out for eye icon there you can click on add then you can add your variables

Another way to perform is by using the scripts & here is how the syntax looks like

Data Variables

The Data Variables comes into picture when running collections with Newman or the Collection Runner.

  • Scope exists only during the execution of an iteration.
  • Can only be set via a CSV or JSON Format file.
  • It can override Global, Collection & Environment variables.

Navigate on any collection and click on & then pop up menu will appear then click on Run Collection. A New window appear something like below then click the Select File button and pick CSV or JSON Format file.

Note: Column name of CSV should match with the variable name used in Postman. Variable names are case sensitive.

Here is the syntax for accessing the variables & this cannot be modified or deleted using script

Local Variables

  • It can be override all other scopes temporarily, during the execution of a request or collection /monitor run.
  • Local variables don’t persist between sessions.

This type of variable can only be added, edited and removed using the script and here is the syntax

Session Variables

By this time you might have got familiar with Global & Environment variables in it while editing you might have noticed INITIAL VALUE & CURRENT VALUE

I was in an assumption that Initial Value was there for to retain the original value & current value is for showing current value but that was not the only case.

Whenever you share your collection to others via file or if you are in a workspace the values in the current value column wouldn’t be shared which is a great feature. What if you are storing some sensitive data like password then don’t save it in Initial Value column instead have it stored in Current Value.

Also, if you are manipulating values using scripts it modifies only the Current Value.

The above is the example how your Initial & Current value should look like.

Summary

Few tips to consider

  • Try to have minimal variables.
  • Try to not to make it collide/mix with other scope variables.
  • Remove unnecessary variables when no longer needed.
  • Don’t store sensitive information in Initial Value.

Let me know your valuable comments and if you want to know more about Postman variables go to the official website here.

--

--

Shiljo Paulson

Full stack Developer, good at OOPs, .Net, C#, TypeScript, JavaScript, SQL, HTML. Recent times interest is on Cloud, System Design and GoLang.