Skip to content

How To Use Global Variable In Postman

API Testing Blog

Why Use Global Variables in Postman?

Global variables in Postman are a powerful tool for managing data that needs to be accessible across multiple requests in your test suite. They allow you to store values like API keys, authentication tokens, or common data elements that are reused in different scenarios. Using global variables helps:

  • Reduce redundancy: Avoid duplicating the same data in multiple requests.
  • Improve maintainability: Easily update values in one central place instead of modifying them throughout your tests.
  • Increase reusability: Share data across different test collections or environments.

Accessing Global Variables

Global variables are stored in the Postman environment. To access them within your requests, use the following syntax:

{{your_global_variable_name}}

Example:

Let’s say you have a global variable named baseUrl with the value https://api.example.com. You can use it in your request URL like this:

{{baseUrl}}/users

This will dynamically replace {{baseUrl}} with https://api.example.com when the request is executed.

Setting Global Variables

1. Using the Environment Variables Section

  • Navigate to your workspace’s Environments tab.
  • Create a new environment or select an existing one.
  • Click on the Add button to add a new variable.
  • Provide the variable name (e.g., baseUrl) and its value (e.g., https://api.example.com).
  • Save the environment.

2. Using the Pre-request Script

You can also set global variables using a pre-request script before sending your request:

pm.environment.set("myGlobalVariable", "someValue");

This script sets a global variable named myGlobalVariable with the value someValue. The updated global variable will be accessible in subsequent requests within the same collection or environment.

Example: Using Global Variables in Authentication

Let’s use global variables for implementing authentication in Postman:

1. Set Global Variables:

  • Environment: Create an environment named Production (or any name you prefer).
  • Add Variables:
    • apiKey: your_api_key_value
    • baseUrl: https://api.example.com

2. Request & Pre-request Script:

  • Request:

    • Define your request with the URL: {{baseUrl}}/api/data
    • Add an Authorization header: Bearer {{apiKey}}
  • Pre-request Script:

    const response = pm.sendRequest({
    url: "{{baseUrl}}/auth/token",
    method: "POST",
    header: {
    "Content-Type": "application/json",
    },
    body: {
    mode: "raw",
    raw: JSON.stringify({
    // your authentication details
    }),
    },
    });
    // Check if the authentication was successful
    if (response.response.statusCode === 200) {
    const token = response.json().token; // Extract the token from the response
    pm.environment.set("authToken", token); // Set the token as a global variable
    } else {
    console.error("Authentication failed");
    }

Explanation:

  • The pre-request script sends a POST request to the authentication endpoint (/auth/token) using the baseUrl global variable.
  • If authentication is successful, the received token is stored in the authToken global variable.
  • This authToken variable is then used in the subsequent request’s Authorization header.

Updating Global Variables

You can modify global variables dynamically during a test run using the pm.environment.set() function.

Example:

pm.environment.set("randomNumber", Math.floor(Math.random() * 100));

This script generates a random number between 0 and 100 and updates the global variable randomNumber with this value.

Best Practices for Global Variables

  • Organize your environment: Create multiple environments (e.g., Development, Testing, Production) for storing different sets of global variables.
  • Use meaningful names: Choose descriptive names for your variables to improve readability and maintainability.
  • Minimize the usage: Use global variables only when necessary. For data specific to a single request, consider using local variables instead.
  • Keep variables secure: Avoid storing sensitive information (like passwords or API keys) in global variables. Instead, use environment variables.
  • Document your variables: Add comments or descriptions to your variables to clearly understand their purpose and usage.

By following these best practices and exploring the various ways to manage global variables, you can harness their power to streamline your API testing workflow and make it more efficient and robust.

API Testing Blog