Skip to content

How To Set Environment Variable In Postman Using Script

API Testing Blog

Setting Environment Variables in Postman Using Scripts

Environment variables are crucial for managing different configurations for your API tests. This guide will show you how to efficiently set and manage these variables using Postman scripts.

Understanding Environment Variables in Postman

Environment variables are key-value pairs that store data specific to a particular environment. This data can be anything from API endpoints, authentication tokens, database credentials, or even test data. Using environment variables allows you to:

  • Organize your test configurations: Keep different sets of configurations for development, testing, staging, and production environments.
  • Reduce redundancy: Avoid hardcoding values in your tests, making them more adaptable and easier to maintain.
  • Share data between requests: Easily access and use environment variables across multiple API requests within a collection.

How to Set Environment Variables in Postman Using Scripts

Postman provides a scripting interface, powered by JavaScript, that enables you to dynamically manage environment variables during your tests. Let’s explore the different ways you can set these variables using scripts.

1. Setting Environment Variables in the Pre-request Script

The pre-request script runs before each API request in a collection. This is where you can define or modify environment variables before sending the request.

Example:

// Set a base URL environment variable
pm.environment.set("baseUrl", "https://api.example.com");
// Set a token environment variable
pm.environment.set("token", "your_token");

Step-by-Step Guide:

  1. Open a request in your Postman collection.
  2. Click on the Pre-request Script tab.
  3. Paste the above script into the editor.
  4. Run the request, and the environment variables will be set for the current environment.

2. Setting Environment Variables in the Tests Script

The tests script runs after each request is sent, allowing you to analyze the response and potentially update environment variables based on the results.

Example:

// Retrieve a user ID from the response
const userId = pm.response.json().id;
// Set the user ID as an environment variable
pm.environment.set("userId", userId);

Step-by-Step Guide:

  1. Open a request in your Postman collection.
  2. Click on the Tests tab.
  3. Paste the above script into the editor.
  4. Run the request. The script will extract the user ID from the response and set it as the “userId” environment variable.

Accessing Environment Variables in Your Scripts

Once environment variables are set, you can access them in Postman scripts using the following syntax:

pm.environment.get("variableName");

Example:

// Access the base URL environment variable
const url = pm.environment.get("baseUrl");
// Send a request to the defined base URL with the token
pm.sendRequest(url + "/users", function (error, response) {
// Handle response
});

Managing Multiple Environments

Postman supports managing multiple environments. You can create separate environments for development, testing, staging, and production. This feature lets you switch between different configurations easily by selecting the desired environment.

Step-by-Step Guide:

  1. Click on the Manage Environments button in the top right corner of Postman.
  2. Click on Add Environment.
  3. Give your environment a name (e.g., “Development”).
  4. Add your environment variables with their corresponding values.
  5. Click on Save.

Now you can switch between your environments by selecting them from the dropdown menu in the top-right corner of Postman. Each environment will have its specific set of environment variables.

Best Practices for Using Environment Variables

  • Use clear and descriptive variable names: Make your scripts easier to understand and maintain.
  • Avoid storing sensitive information in environment variables: Instead, use secure methods to manage credentials.
  • Keep your environment variables organized: Categorize them logically to maintain order and clarity.

By mastering the use of environment variables with scripts in Postman, you can significantly improve the efficiency, adaptability, and maintainability of your API tests.

API Testing Blog