Skip to content

How To Use Collection Variable In Postman

API Testing Blog

How to Use Collection Variables in Postman for Efficient API Testing

Postman’s collection variables offer a powerful way to manage and reuse data across multiple API requests within a collection. This can significantly streamline your API testing workflow, reducing redundancy and improving test maintainability. Here’s a comprehensive guide on how to use collection variables effectively:

1. Defining Collection Variables

To define a collection variable, follow these steps:

  • Open your collection: Navigate to the collection you want to work with.
  • Click “Variables” tab: Within the collection, you’ll find a “Variables” tab.
  • Add new variable: Click the “Add variable” button and enter:
    • Key: The name of your variable.
    • Value: The initial value for the variable.
    • Type: Select the appropriate data type (text, number, boolean, etc.).
    • Current Value: The value you defined can be overridden with a value from an environment variable with the same name.

Example:

You can define a variable called “baseUrl” with the value “https://api.example.com”. This variable will hold the base URL for your API endpoints, making it easy to update the base URL for different environments or for use in multiple requests.

2. Using Collection Variables in Request

Once defined, you can use collection variables within your requests:

  • In the request URL: Replace hardcoded URL parts with your variable using double curly braces: {{baseUrl}}/users
  • In the request body: Similarly, replace hardcoded values within the JSON body with variables.
  • In headers: Use variables for dynamic authorization tokens or other header values.

Example: In the request for fetching user details, we can use the variable userId for dynamic user IDs:

{
"url": "{{baseUrl}}/users/{{userId}}",
"method": "GET"
}

3. Setting Collection Variables During Tests

Collection variables can be set dynamically during tests:

  • In the “Tests” tab: Use the pm.collectionVariables.set() function to set the value of a variable within a test script.

Example: You can capture the user ID from the response and store it in the userId variable:

pm.test("Extract User ID", function () {
var jsonData = pm.response.json();
pm.collectionVariables.set("userId", jsonData.id);
});

4. Accessing Collection Variables in Tests

You can access collection variables within your test scripts using the pm.collectionVariables.get() function.

Example: To get the user ID set in the previous step:

pm.test("Check User Name", function () {
var userId = pm.collectionVariables.get("userId");
// Use userId to verify the user details
});

5. Advantages of Using Collection Variables

  • Reusability: Define and reuse variables across multiple requests, reducing code redundancy.
  • Flexibility: Easily adjust variables for different environments or tests, promoting modularity.
  • Maintainability: Changes to variables affect all requests using them, simplifying updates.
  • Organization: Better organization and clarity in API testing workflows.

6. Using Environment Variables with Collection Variables

Postman allows you to set up “Environments” that hold variables applicable to a specific environment (e.g., development, production). You can either directly use environment variable names within your requests or use them as initial values for collection variables like this:

  • Direct use: Replace the collection variable name with the environment variable name. For example, {{baseUrl}} can be replaced with {{prodBaseUrl}} if prodBaseUrl is defined in your “Production” environment.
  • Initializing collection variable: Set the environment variable name as the “Current Value” of a collection variable. For example, a “baseUrl” collection variable can be initialized with “Current Value” as “{{prodBaseUrl}}”, which will automatically take the value from the “Production” environment.

Conclusion

Collection variables are a powerful feature in Postman for organizing and streamlining your API testing workflows. They promote reusability, flexibility, and maintainability, improving your test efficiency and accuracy. By mastering these concepts, you can build robust and maintainable API tests in Postman.

API Testing Blog