Skip to content

How To Use Collection Variable In Postman Body

API Testing Blog

How to Utilize Collection Variables within Postman Request Bodies

When testing APIs, you often need to reuse the same data across multiple requests. This is where Postman’s collection variables shine. They allow you to store and dynamically insert values into your request bodies, simplifying your testing process and reducing redundancy.

1. Defining Collection Variables

Before using variables within your request bodies, you need to define them in your Postman collection. Here’s how:

  1. Open your Postman collection: Navigate to the collection containing the requests where you want to use variables.
  2. Access collection variables: Click on the three dots (ellipsis) next to the collection name and select “Manage Collection Variables.” This will open the collection’s variable editor.
  3. Create a new variable: Click on the “Add variable” button (plus icon).
  4. Define variable details:
    • Provide a key (e.g., username) for your variable.
    • Assign a value (e.g., testuser).
    • Check the initial value checkbox if you want the variable to be initialized at the start of every collection run.

2. Dynamically Inserting Collection Variables in Request Bodies

Once defined, you can effortlessly insert collection variables within your request bodies using the {{ variable_key }} syntax.

Example:

Let’s assume you have a variable named userID with the value 12345. To include this variable in a JSON request body:

{
"userId": "{{userID}}",
"name": "John Doe"
}

When you send this request, Postman will dynamically replace {{userID}} with the actual value 12345, resulting in:

{
"userId": 12345,
"name": "John Doe"
}

3. Using Collection Variables in Multiple Requests

The real power of collection variables lies in their reusability across different requests within a collection. By defining variables once, you can easily manage and update data used in multiple API calls.

Example:

Let’s say you have two requests: one for creating a new user and another for retrieving that user’s details. You can use the same userID variable in both:

Create User:

{
"name": "Jane Smith",
"email": "jane.smith@example.com",
"userId": "{{userID}}"
}

Retrieve User:

{
"userId": "{{userID}}"
}

Here, the userID variable will be automatically populated in both requests, ensuring consistency and simplifying your testing workflow.

4. Updating Collection Variables During Testing

During your testing, you might need to dynamically change the values of collection variables. Postman offers several ways to achieve this:

a) Using Pre-request Scripts:

Pre-request scripts can be used to modify the values of collection variables before sending each request.

Example:

// In a pre-request script
pm.collectionVariables.set("userID", "67890");

This script will set the userID variable to 67890 before every request within your collection.

b) Using Test Scripts:

You can also modify variable values within test scripts.

Example:

// In a test script
pm.collectionVariables.set("userID", "12345");

This script will update the userID variable after a request is sent, allowing you to further tailor your testing workflow.

5. Accessing Collection Variables in Tests

Besides request bodies, you can also access collection variables within your test scripts.

Example:

// In a test script
var userId = pm.collectionVariables.get("userID");
console.log("User ID: ", userId);

This script retrieves the value of userID and outputs it to the console.

6. Using Environment Variables within Request Bodies

Postman allows you to use environment variables alongside collection variables. Environment variables offer an overarching scope for managing data across multiple environments, such as development, testing, and production.

To utilize environment variables within request bodies, use the same syntax as collection variables: {{variable_name}}.

Example:

{
"baseUrl": "{{baseUrl}}",
"userId": "{{userId}}"
}

Here, baseUrl is likely an environment variable, while userId could be a collection variable. This allows you to adjust the base URL depending on the environment while retaining specific user IDs for testing purposes.

7. Collection Variables vs. Environment Variables

While both collection and environment variables provide value in managing dynamic data, their use cases differ:

Collection Variables:

  • Scope: Limited to the specific collection they are defined within.
  • Purpose: Managing data specific to a particular set of API calls or tests.
  • Flexibility: Easier to change and update within a collection.

Environment Variables:

  • Scope: Global, applying to all collections within an environment.
  • Purpose: Storing information that might differ across test environments (e.g., Base URLs, API keys).
  • Flexibility: Requires updating the entire environment for changes to take effect.

Understanding the distinction between these variables and their respective scopes will help you optimize your testing strategy.

By mastering the usage of collection variables in Postman, you streamline API testing workflows, improve data reusability, and achieve more effective results.

API Testing Blog