Variables in Postman

Variables in Postman

What is variable?

·

3 min read

A variable is a memory space in which we can store data and used it at any point in time when needed.

In Postman, a variable can be declared by the following scopes:

  1. Global variables enable you to access data between collections, requests, test scripts, and environments

  2. Collection variables are available throughout the requests in a collection and are independent of environments. Collection variables don't change based on the selected environment.

  3. Environment variables enable you to scope your work to different environments, for example, local development versus testing or production. One environment can be active at a time.

  4. Data variables come from external CSV and JSON files to define data sets you can use when running collections with Newman or the Collection Runner. Data variables have current values, which don't persist beyond request or collection runs.

  5. Local variables are temporary variables that are accessed 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.

Defining Variables

Variables can be defined according to your need such as global, local, and environmental.

To define variables at any scope in the request builder, do the following:

  1. Select the data you need, for example in the address, parameters, headers, or body. Select Set as variable.

  1. Select Set as a new variable.

  2. Enter a Name, confirm the Value is correct, and select a scope. Select Set variable.

Defining global variables

To add a new global variable, do the following:

  1. Select Add a new variable, and enter a name for the variable.

  2. Select a Type for the new variable.

  3. Add an Initial Value, and if you choose, a Current Value.

  4. Select Save to confirm your changes.

Defining environment variables

To add a new environment variable, do the following:

  1. Select Add a new variable, and enter a name for the variable.

  2. Select a Type for the new variable.

  3. Add an Initial Value, and if you choose, a Current Value.

Select Save to confirm your changes.

Defining collection variables

To create or edit a variable for an existing collection, do the following:

  1. Select Collections in the sidebar.

  2. Select a collection, and then select the Variables tab.

Defining variables in scripts

You can set variables programmatically in your request scripts.

//defining a global variable
pm.globals.set('variable key','variable value');
//example
pm.globals.set('token','abcd');

//defining a collection variable
pm.collectionVariables.set('key', 'value');

//environment
pm.environment.set('key', 'value');

//local variable
pm.variables.set('key', 'value);

Using Variables

You can use double curly braces to reference variables throughout Postman. For example, to reference a variable named "cust_id" in your request authorization settings, you would use the following syntax with double curly braces around the name:

https://postman-echo.com/get?customer_id={{cust_id}}

Using variables in scripts

You can retrieve the current value of a variable in your scripts using the object representing the scope level and the .get method:

//access a variable at any scope including local
pm.variables.get("variable_key");
//access a global variable
pm.globals.get("variable_key");
//access a collection variable
pm.collectionVariables.get("variable_key");
//access an environment variable
pm.environment.get("variable_key");

Thank you for reading