Skip to content

How To Use Environment Variables In Postman Json Body

API Testing Blog

Using Environment Variables in Postman JSON Body for Dynamic Testing

Environment variables in Postman provide a powerful mechanism to manage and reuse data across different requests and environments. This is particularly useful for API testing, where you often need to interact with different endpoints, test with different credentials, or manipulate data dynamically.

Understanding Environment Variables

Environment variables are key-value pairs stored within Postman that can be accessed from different parts of the application, including your request bodies. They allow you to:

  • Dynamically change requests: Easily switch between different endpoints, credentials, or data values without manually modifying each request.
  • Organize test data: Keep your test data separate from your requests, making your tests easier to manage and maintain.
  • Collaborate effectively: Share environments with your team to ensure everyone uses the same test data and settings.

Creating and Managing Environment Variables

  1. Access the Environments Tab: Go to the Postman app and click on the “Environments” icon in the left sidebar.
  2. Create a New Environment: Click the “Add” button to create a new environment.
  3. Add Variables: In the newly created environment, add variables by providing a name and value for each:
    • Name: The name of the variable, e.g., baseUrl, apiKey, username.
    • Value: The actual value you want to store, e.g., https://api.example.com, your_api_key, demo_user.
  4. Select the Environment: Choose the desired environment from the dropdown menu at the top right of Postman.

How to Use Environment Variables in Postman JSON Body

1. Using Variable Directly in JSON Body:

The most straightforward way is to insert the environment variable directly within the JSON body.

  • Example:
{
"name": "{{user_name}}",
"email": "{{user_email}}",
"password": "{{user_password}}"
}

Explanation:

  • The variable names are enclosed in double curly braces ({{ and }} ) representing environment variable placeholders.
  • When you send the request, Postman will replace these placeholders with the corresponding values from the selected environment.

2. Adding Environment Variables in Pre-Request Scripts:

You can leverage pre-request scripts to dynamically generate JSON payloads using environment variables.

  • Example:
pm.environment.set("user_name", "John Doe");
pm.environment.set("user_email", "john.doe@example.com");
pm.environment.set("user_password", "P@sswOrd123");
pm.test("Validate JSON Body", () => {
const jsonData = {
"name": pm.environment.get("user_name"),
"email": pm.environment.get("user_email"),
"password": pm.environment.get("user_password")
};
pm.expect(pm.response.json()).to.deep.equal(jsonData);
});

Explanation:

  • The pm.environment.set() function is used to set new environment variables or update existing ones.
  • The pm.environment.get() function retrieves the values of environment variables.

3. Using Variables in a Collection:

If you have multiple requests that reuse the same JSON structure with different values, you can define the JSON in a collection and dynamically inject variables.

  • Create a Collection: Create a new collection in Postman and add your requests.
  • Define the JSON: Open the “Body” tab of the request and select “raw” as the format. Paste the JSON template with variables:
{
"name": "{{user_name}}",
"email": "{{user_email}}",
"password": "{{user_password}}"
}
  • Create a Collection Variable: Go to the collection settings and add a new variable named JSON_BODY. In the value field, paste the JSON template with the variables.
  • Update Requests: Navigate to each request in the collection. In the Body tab, select “raw” and choose “text” as the format. Then, paste the following code:
pm.collection.set("JSON_BODY", {
"name": "{{user_name}}",
"email": "{{user_email}}",
"password": "{{user_password}}"
});
pm.request.body = pm.collection.get("JSON_BODY");

Explanation:

  • pm.collection.set() function is used to store the JSON body template with variables in a collection variable.
  • pm.request.body function is used to update the request body with the value present in the collection variable.

4. Using Variables in Data Files:

You can use CSV or JSON files to store and manage your test data, then use environment variables to dynamically access and use this data.

  • Create a Data File: Create a CSV or JSON file containing your test data.

Example (CSV):

user_name,user_email,user_password
John Doe,john.doe@example.com,P@sswOrd123
Jane Smith,jane.smith@example.com,S3cr3t123

Example (JSON):

[
{
"user_name": "John Doe",
"user_email": "john.doe@example.com",
"user_password": "P@sswOrd123"
},
{
"user_name": "Jane Smith",
"user_email": "jane.smith@example.com",
"user_password": "S3cr3t123"
}
]
  • Import Data File: Import the data file into your Postman collection.
  • Use Variables in JSON Body: In the request body, use the environment variable names as placeholders.

Best Practices

  • Use Descriptive Variable Names: Choose clear and informative names for your environment variables to improve readability.
  • Employ Consistent Naming Conventions: Maintain a consistent naming convention across your environments and collections.
  • Employ Appropriate Data Types: Use the correct data types for your variables (strings, numbers, booleans) to avoid errors.
  • Prioritize Security: Securely store sensitive information in environment variables, and consider using environment variables for sensitive data rather than hardcoding.

By utilizing environment variables in your Postman JSON body, you can build dynamic and robust API tests that adapt to changing requirements and enhance your overall testing process.

API Testing Blog