Skip to content

How To Use Environment Variables Postman

API Testing Blog

Introduction to Environment Variables in Postman

Environment variables are powerful tools that allow you to manage and organize different configurations for your API tests within Postman. They enable you to:

  • Store sensitive information securely: Prevent hardcoding sensitive data like API keys and passwords directly into your requests.
  • Maintain separate settings for different environments: Switch effortlessly between development, testing, staging, and production environments without modifying your requests.
  • Simplify test maintenance: Easily adjust settings across multiple tests by modifying only the environment variable value.

How to Create and Manage Environment Variables in Postman

Here’s a step-by-step guide on creating and managing environment variables:

  1. Navigate to Environments:

    • Click on the “Environments” tab in the left-hand sidebar of Postman.
  2. Create a New Environment:

    • Click on the “Add” button to create a new environment.
    • Give your environment a descriptive name (e.g., “Development,” “Staging,” “Production”).
  3. Define Environment Variables:

    • In the environment editor, add variables by clicking “Add” and providing the following information for each:
      • Key: The name of the variable (e.g., BASE_URL, API_KEY).
      • Value: The current value for the environment (e.g., “https://api.example.com”, “your_api_key”).
      • Initial Value (optional): A default value that will be used if the variable is not defined for the selected environment.

How to Use Environment Variables in Postman Requests

  1. Select Your Environment:

    • Choose the appropriate environment from the dropdown menu at the top of the Postman interface.
  2. Utilize Variables in Request Components:

    • Replace hardcoded values in your request with the syntax {{variable_name}}.
    • For example, to use the BASE_URL variable in the request URL, use: {{BASE_URL}}/api/users.
    • You can also use variables in the request headers, body, and other areas.

Illustrative Example: Utilizing Environment Variables for Secure API Testing

Scenario: You are testing an API that requires API keys for authentication.

1. Define Environment Variables:

  • Environment: Production
  • Variables:
    • API_KEY: your_production_api_key
    • BASE_URL: https://api.prod.example.com

2. Create a Postman Request:

  • Request URL: {{BASE_URL}}/api/v1/users
  • Authorization:
    • Type: Bearer Token
    • Token: {{API_KEY}}

3. Send the Request:

  • Postman will automatically substitute the values from the selected Production environment.

Practical Tips for Using Environment Variables Effectively in Postman

  • Structure Your Environments: Create different environments for development, testing, staging, and production to prevent conflicts and ensure clear separation of settings.
  • Avoid Hardcoding Values: Never hardcode sensitive information directly into your requests; always use environment variables.
  • Stay Organized: Use descriptive variable names that clearly reflect their purpose.
  • Use the Postman Environment Manager for Bulk Editing: Modify multiple environment variables simultaneously.

Beyond Basic Environment Variables: Advanced Concepts in Postman

Using Environment Variables in Collections

Environment variables can be used to control the behavior and execution of entire Postman collections.

Example: You can define an isEnabled variable in your environment that controls whether certain requests in your collection are executed. You can use conditionals in the Pre-request script:

if (pm.environment.get("isEnabled") === "true") {
// Execute the request
} else {
// Skip the request
}

Data-Driven Testing with Environment Variables

Environment variables can be used to store data sets for data-driven testing. You can iterate through a collection of variables and send requests with different parameters.

Example: Define a variable users in your Test environment as an array of JSON objects:

[
{ "name": "John Doe", "email": "john.doe@example.com" },
{ "name": "Jane Doe", "email": "jane.doe@example.com" }
]

Then, in your request, use the pm.iterationData.get("users") function to access individual data points during each iteration. This allows you to send multiple requests with different user data.

These advanced techniques further highlight the power and flexibility of environment variables in Postman, enabling you to streamline your API testing workflow and tackle complex scenarios effectively.

API Testing Blog