Variables

Variables are symbolic names that represent the information you store in them. The information the variable represents can change, but the operations on the variable remain the same. Variables in Postman work the same way.

This topic covers:

Variable scopes

You can assign five types of variable scopes:

  1. Global
  2. Collection
  3. Environment
  4. Local
  5. Data

You can view different kinds of buckets in which values reside. If a variable is in two different scopes, the scope with a higher priority wins and the variable gets its value from there. Postman resolves scopes using this hierarchy progressing from broad to narrow scope. 

nested variable scopes

If a variable from the currently active environment shares its name with a global variable, the environment variable takes priority.

Environment variables override global variables, and data variables override enviornmental variables. Data variables are only available in the collection runner.

Accessing variables in the request builder

You can use variables in the following form in the Postman user interface - `{{variableName}}`.

The string `{{variableName}}` will be replaced with its corresponding value when Postman resolves the variable.

For example, for an environment variable url with the value http://localhost, you will have to use `{{url}}`  in the request URL field. {{url}} will be replaced by http://localhost when the request is sent.

Since variables in the request builder are accessed using string substitution, they can be used everywhere in the request builder where you can add text—including the URL, URL parameters, headers, authorization, request body, and header presets.

Postman evaluates the variables according to scoping rules, as discussed in the previous “Variable scopes” section, and sends them to the server.

variables used in request builder

Accessing variables through scripts

You can also use variables in pre-request and test scripts.

Since these sections for scripts are written in JavaScript, you will initialize and retrieve these variables in a different manner. You can initialize variables in scripts and put them in a particular scope. 

  1. Defining an environment or global variable in a script: 

To set a variable in a script, use the pm.environment.set() method or pm.globals.set() method depending on the desired scope. The method requires the variable key and value as parameters to set the variable.  When you send the request, the script will be evaluated and the value will be stored as the variable. Note that defining a collection variable is a little different and can be done by editing the collection details.

  1. Fetching a pre-defined variable: 

Once a variable has been set, use the pm.variables.get() method or, alternatively, use the pm.environment.get() or pm.globals.get() method, depending on the appropriate scope to fetch the variable. The method requires the variable name as a parameter to retrieve the stored value in a script.

  1. Setting a variable in a scope: 

You can access environment variables with the corresponding environment template. You can access collection variables from a request within the collection. You can broadly access global variables regardless of the selected environment.

variables used in script

Defining collection variables

You can define collection variables by editing the collection details.

Click on the ellipses (…) next to the collection name, and select “Edit” to open the EDIT COLLECTION modal. Select the Variables tab to add and edit collection variables.

You can also define collection variables when creating the collection.

Logging variables

Often while using variables in scripts, you will need to see the values they obtain.

You can use the Postman Console to do this easily.

From the application menu, select “View” and then “Show Postman Console”.  To log the value of a variable, you can use console.log(foo); in your script.

When you send a request, the script will be evaluated and the value of the variable will be logged in the Postman Console.

variables logged

Data variables

The Collection Runner lets you import a CSV or a JSON file, and then use the values from the data file inside HTTP requests and scripts. We call these ’data variables’.

To use them inside Postman, follow the same syntax as environment or global variables. 

Data variables in requests

Variables inside the Postman UI are enclosed inside curly braces.

For example, in the screenshot below, {{username}} and {{password}} inside URL parameters would be replaced by corresponding values from the data file:

data variables in requests

Data variables in pre-request and test scripts

Here’s an example of Inside pre-request and test scripts. Let’s say you have the pm.iterationData.get("username") method inside pre-request and test scripts. The method would let you access the value of the username variable from a data file.

data variables in scripts

To learn more about data files, see Working with data files

Dynamic variables

Postman has a few dynamic variables you can use in your requests. 

Dynamic variables cannot be used in the Sandbox. You can only use them in the {{..}} format in the request URL / headers / body.

  • {{$guid}} : Adds a v4 style guid
  • {{$timestamp}}: Adds the current timestamp
  • {{$randomInt}}: Adds a random integer between 0 and 1000

dynamic variables

Quick Look for variables

Quick Look is a quick preview feature that displays all your environment and global variables in one place.

Click the “eye” icon in the top right to toggle the display, or type the keyboard shortcut (CMD/CTRL + ALT + E).

quick look

Autocomplete and tooltips for variables

Postman variables are very powerful, and two features - autocomplete and tool tips - make them even more convenient.

Autocomplete for variables

autocomplete for variables

Type an open curly bracket to summon the autocomplete menu.

For the pre-request and test scripts section, which uses the data editor, enter the first letter of a variable to trigger the autocomplete.

The menu contains a list of all the variables in the current environment, followed by globals. Navigating through the list also shows the current value and scope for each variable, along with a feedback for overridden variables. 

Variable highlighting and tooltip on hover

variable highlighting and tooltips

Variables are highlighted in orange, with unresolved variables shown in red.

Hovering over a variable shows its current value and the scope. If a variable is unresolved - such as, no value in the current environment - the tooltip shows the appropriate feedback.