Skip to content

How To Use Global Variables In Postman

API Testing Blog

Understanding Global Variables in Postman

Global variables in Postman are powerful tools that allow you to store and reuse data across multiple requests, collections, and environments. They streamline your API testing workflow by eliminating the need to repeatedly enter the same information. This guide will walk you through the essentials of using global variables effectively.

Define Global Variables

Global variable definitions are housed within the “Globals” section of the “Environment Manager.” To access this, navigate to the “Postman” menu and select “Environment Manager.”

  1. Create a New Environment: If you don’t have one already, click “Add” to create a new environment.
  2. Define Variables: In the “Variables” section, click “Add” to define a new variable.
  3. Name and Value: Provide a descriptive name for your variable and assign its initial value. For instance, you could define a global variable named baseUrl with the value https://api.example.com.
  4. Environment Scope: Note that global variables exist within the scope of the specific environment you define them in.

Accessing Global Variables

You can access global variables in request URLs, headers, body parameters, and even in assertions using a simple syntax:

Example:

{{baseUrl}}/users

Here, {{baseUrl}} will be replaced with the actual value of your global variable, allowing you to dynamically construct your requests.

Using Global Variables in Test Scripts

Global variables also play a crucial role in Postman’s test scripts. They let you store and manipulate data dynamically throughout your test cases.

Example:

// Get the value of a global variable
const token = pm.environment.get('token');
// Use the token in a subsequent request
pm.sendRequest('{{baseUrl}}/users', function (err, res) {
if (err) {
console.log(err);
} else {
// Perform assertions using the retrieved token
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
}
});

In this example, the token variable is retrieved from the global scope and used in a subsequent request.

Updating Global Variables in Test Scripts

You can modify global variables dynamically within your test scripts, allowing you to adapt to changing environments or store information from your API responses.

Example:

// Update a global variable with the response ID
const userId = pm.response.json().id;
pm.environment.set('userId', userId);

Here, the userId is extracted from the response and stored in the global variable userId.

Managing Environments

Postman allows you to create multiple environments, each containing its own set of global variables. This enables you to switch between different configurations for testing in various environments, such as development, staging, and production.

  1. Create New Environments: Use the “Add” button within the Environment Manager to define different environments.
  2. Environment Switching: Switch between environments by selecting the desired environment from the dropdown in the top-right corner of the Postman interface.

Real-world Example: Authentication and Authorization

Let’s illustrate the use of global variables with a scenario involving authentication.

  1. Authentication Request:
    • Create a POST request to your API’s authentication endpoint.
    • Include your credentials in the request body.
    • Define a test script to extract the generated authentication token from the response and store it in a global variable named token:
      const token = pm.response.json().token;
      pm.environment.set('token', token);
  2. Subsequent Requests:
    • In subsequent requests that require authorization, include the token in the Authorization header:
      Authorization: Bearer {{token}}
    • The {{token}} placeholder will be replaced with the actual token stored in your global variable.

Advantages of Using Global Variables:

  • Code Reusability: Reduce redundancy by reusing the same data across multiple requests.
  • Environment Management: Easily switch between different environments without manual modifications.
  • Dynamic Testing: Adapt to changing environments or store response data for later use.
  • Improved Maintainability: Update global variables once in the Environment Manager, ensuring consistency across your tests.

By harnessing global variables effectively, you can streamline your API testing workflow, improve test maintainability, and ensure accurate and efficient automation. Remember to use descriptive variable names and maintain a consistent environment management strategy for optimal results.

API Testing Blog