Skip to content

How To Use Environment Variables In Postman

API Testing Blog

How to Use Environment Variables in Postman for API Testing

Environment variables are a powerful tool in Postman that allow you to manage and reuse data across your API tests. This guide will walk you through the process of using environment variables in Postman for API testing, providing practical examples and step-by-step instructions.

Understanding Environment Variables

Environment variables are key-value pairs that store data you want to access within your Postman environment. Think of them as containers for information like API endpoints, authentication tokens, or sensitive data. This allows you to easily switch between different environments (e.g., development, testing, production) or keep sensitive information secure.

Setting Up Environments

  1. Create an Environment:

    • Go to the “Environments” section in Postman (the “Environments” icon in the left sidebar).
    • Click the “Add Environment” button.
    • Give your environment a descriptive name (e.g., “Development,” “Staging”).
  2. Define Variables:

    • Within your new environment, click “Add Variable” to add key-value pairs.
    • Each variable has a “Key” (a name identifying the data) and a “Value” (the actual data).
    • Example:
      • Key: baseUrl
      • Value: https://api.example.com

Using Environment Variables in Your Requests

  1. Accessing Variables: Use the {{ and }} syntax to access the value of an environment variable within your requests.

    • Example:
      • Request URL: {{baseUrl}}/users
    • When this request is sent, Postman will replace {{baseUrl}} with the value of the baseUrl variable in your current environment.
  2. Dynamically Setting Variables: You can dynamically set the value of variables within your tests using the Postman Runner.

    • Example:
      • In your test script:
        pm.environment.set("userId", response.json().id);
    • This code snippet sets the environment variable userId to the id value retrieved from the response body.

Working with Multiple Environments

  1. Switching Environments:

    • The environment is selected at the top-right corner of the Postman interface.
    • You can easily switch between environments to test against different endpoints or data sources.
  2. Environment Specific Values:

    • For sensitive data, you can define separate environment variables for each environment.
    • Example:
      • In your “Development” environment:
        • Key: apiKey
        • Value: dev-secret-key
      • In your “Production” environment:
        • Key: apiKey
        • Value: prod-secret-key

Practical Examples

Example 1: Testing an API with Different Base URLs

  1. Create Environments:

    • “Development” with baseUrl: https://api.example.com/dev
    • “Production” with baseUrl: https://api.example.com/prod
  2. Request with Variable:

    • Request URL: {{baseUrl}}/users
  3. Running Tests:

    • Switch between “Development” and “Production” environments to test against different endpoints.

Example 2: Storing Authentication Tokens

  1. Get Token from Authentication Endpoint:

    • Send a request to your authentication endpoint to get a token.
    • Store the token in an environment variable using the Postman Runner:
      pm.environment.set("authToken", response.json().token);
  2. Use Token in Subsequent Requests:

    • Add an Authorization header to all subsequent requests with the authToken variable:
      • Authorization: Bearer {{authToken}}

Best Practices for Using Environment Variables

  • Use descriptive names for your environment variables.
  • Keep sensitive information securely stored in environment variables.
  • Use different environments for different phases of your development lifecycle.
  • Leverage the Postman Runner to dynamically set and update environment variables during your test execution.

By effectively using environment variables in Postman, you can streamline your API testing process, improve maintainability, and manage different testing environments with ease.

API Testing Blog