Skip to content

How To Use Postman Environment Variables

API Testing Blog

Understanding Postman Environment Variables

Postman environment variables are crucial for managing and organizing your API testing workflow. They allow you to define and store dynamic values, making your tests more flexible, reusable, and maintainable. Let’s explore how to effectively leverage environment variables in your Postman journey.

Creating and Managing Environments

Before diving into usage, you need to set up your environments.

  1. Navigate to Environments: In Postman’s sidebar, click on the Environments icon.
  2. Create a New Environment: Click the ”+” button to create a new environment. Give it a descriptive name (e.g., “Staging,” “Production”).
  3. Add Variables: Click “Add Variable” to define your environment variables.

Example:

Variable NameValueDescription
baseUrlhttps://api.example.comBase URL of your API
apiKeyyour_api_keyYour API Key
tokenPlaceholder for authentication tokens

Utilizing Environment Variables in Requests

Now that you have environments set up, let’s use them in your API requests.

  1. Select an Environment: In the top-right corner of Postman, choose the environment you want to use for your request.
  2. Insert Variables: Replace hardcoded values in your request URL, headers, or body with the {{variableName}} syntax.

Example:

// Request Body
{
"name": "{{userName}}",
"email": "{{userEmail}}"
}
// Request Headers
{
"Authorization": "Bearer {{token}}"
}

Setting Values Dynamically Using Pre-Request Scripts

You can dynamically update environment variables before sending a request using pre-request scripts.

Example:

**1. **Create a new Pre-request Script: In the request editor, go to the Pre-request Script tab.

**2. Set the value using JavaScript:

pm.environment.set('token', 'your_generated_token');

**3. Use the updated variable:

pm.sendRequest('https://api.example.com/users', function (err, response) {
if (err) { console.log(err); }
pm.test("Status code is 200", function () {
pm.expect(response.code).to.be.equal(200);
});
});

Managing Global Variables

Sometimes, you may need variables accessible across all environments. That’s where Global Variables come in.

  1. Create Global Variables: Go to the Globals section in Postman’s environment manager.
  2. Add Variables: Follow the same process as adding environment variables, but these will be accessible to all environments.

Example:

  • You might store common values like default timeout values or logging levels as global variables.

Working with Environment Collections

To streamline your testing, you can use environment variables in conjunction with collections.

Example:

  1. Create a Collection: Organize your APIs in a collection.
  2. Associate an Environment: In your Collection’s settings, choose the desired environment.
  3. Execute Requests: When you send requests from this collection, the assigned environment variables will be used.

Environment Variables in Test Scripts

You can also access and modify environment variables within your test scripts.

Example:

pm.test("Check response data", function () {
const responseData = pm.response.json();
pm.expect(responseData.name).to.be.equal(pm.environment.get('userName'));
});

By using environment variables in test scripts, you can verify that your requests correctly utilize the expected data from the chosen environment.

Best Practices for Postman Environments

  • Keep it Organized: Group your variables into logical environments (e.g., development, staging, production).
  • Clearly Name Variables: Use descriptive names that reflect the variable’s purpose.
  • Utilize Pre-request Scripts: Dynamically set environment variables for flexibility.
  • Document Variables: Add clear descriptions to understand each variable’s role.
  • Use Environments Effectively: Switch between environments seamlessly for different testing phases.

API Testing Blog