Skip to content

How To Use Environment Variable In Postman

API Testing Blog

Leveraging Environment Variables in Postman for API Testing

Environment variables allow you to store and manage dynamic values within Postman, making your API tests more flexible and maintainable. They are particularly useful for handling:

  • Credentials: Storing API keys, tokens, and usernames securely.
  • Base URLs: Seamlessly testing different environments (development, staging, production).
  • Configuration details: Managing timeouts, headers, and other test-specific settings.

1. Creating and Managing Environments

1.1 Creating an Environment

  • Navigate to Environment: Go to the “Environments” section on the Postman sidebar.
  • Create New Environment: Click the “Add” button to create a new environment.
  • Name Your Environment: Assign a descriptive name to your environment (e.g., “Development”, “Staging”).
  • Define Variables: Add your environment variables, providing a unique key and value for each.

Example:

KeyValue
BASE_URLhttps://api.example.com/v1
API_KEYyour_api_key_goes_here

1.2 Selecting and Switching Environments

  • Environment Selection: In the Postman interface, click on the “Environment” dropdown and choose the desired environment.
  • Switching Environments: Postman allows you to switch environments seamlessly during testing. Simply select a different environment from the dropdown.

2. Utilizing Environment Variables in Requests

2.1 Using Variables in Request URLs

Example:

{{BASE_URL}}/users

This will dynamically replace {{BASE_URL}} with the value defined in your selected environment.

2.2 Employing Variables in Request Headers

Example:

Authorization: Bearer {{API_KEY}}

This sets the Authorization header value to the dynamically retrieved value from API_KEY.

2.3 Accessing Variables in Request Body

Example:

{
"username": "test_user",
"password": "{{PASSWORD}}"
}

This will replace {{PASSWORD}} with the value from your selected environment during the request.

3. Managing Environment Variables with Collections

3.1 Linking Collections to Environments

When creating a new Collection, you can associate it with a specific environment. This ensures that your tests use the correct variables while executing them.

3.2 Setting Variables in Collection Runs

You can define variables specifically for a Collection run, overriding any values already set in the associated environment.

Example:

Environment Variable:

KeyValue
BASE_URLhttps://api.example.com/v1

Collection Run Variable:

KeyValue
BASE_URLhttps://api.test.example.com/v2

During the Collection run, BASE_URL will be replaced with https://api.test.example.com/v2, overriding the environment variable.

4. Best Practices for Environment Variables

  • Security: Never store sensitive information directly within your Postman interface. Utilize environment variables for managing credentials and keep them separate from your code.
  • Clarity: Use meaningful and descriptive names for your environment variables. This improves readability and maintainability of your tests.
  • Organization: Group related variables into dedicated environments to maintain order and manage different configurations effectively.
  • Version Control: Consider storing your environment variables in a version control system for better collaboration and tracking changes.

Conclusion: Environment variables are essential for effective API testing in Postman. They streamline dynamic requests, enhance security, and ensure consistent test execution across various environments. By leveraging this feature, you can create robust and efficient API testing workflows.

API Testing Blog