Using variables in Postman not only helps you manage APIs efficiently but also reduces errors and saves time. In this article, we’ll explore how to use the five types of variables in Postman: Global Variables, Collection Variables, Environment Variables, Local Variables, and Data Variables.

Types of Variables in Postman

Global Variables

  • Accessible from anywhere in Postman.
  • Ideal for storing shared values, such as a base URL or default timeout.

Collection Variables

  • Limited to a specific collection.
  • Commonly used for values associated with a group of requests within the same project.

Environment Variables

  • Scope is limited to a specific environment, such as Development, Testing, or Production.
  • Useful for managing values that vary across environments, like URLs or tokens.

Local Variables

  • Effective only within the current request.
  • Declared directly in the Pre-request Script or Test Script of a request.

Data Variables

  • Store values from datasets when running the Collection Runner.
  • Often used for automated testing with multiple datasets.

How to Create and Use Variables in Postman

Creating Global Variables

  1. Open the Environments tab in the left-hand menu.
  2. Select Globals from the list.
  3. Click Add and input the Key (variable name) and Value (value).
  4. Click Save to apply changes.
Creating Global Variables

Creating Environment Variables

  1. In the Environments tab, click Add to create a new environment.
  2. Name the environment (e.g., Development or Production).
  3. Add variables and their respective values to the Key-Value table.
  4. Click Save to finalize.
Creating Environment Variables

Creating Collection Variables

  1. Open the Collections tab and select the desired collection.
  2. Navigate to the Variables section.
  3. Add variables in the Key-Value table.
  4. Click Save to store them.
Creating Collection Variables

Creating Local Variables

  1. In the Pre-request Script or Test Script section of a request, use the following syntax:javascriptCopy codepm.variables.set("localVar", "local variable value");

Creating Data Variables

  1. Prepare a CSV or JSON file with the dataset.
  2. Use the Collection Runner to run the requests. Postman will automatically read the values from the file.

Using Variables in Postman Requests

You can use variables in the URL, Headers, Body, or Params of a request using the syntax {{variable_name}}.

Examples of Using Variables:

  1. In the URL:plaintextCopy codehttps://{{devURL}}/api/v1/users
    • {{devURL}} is an environment variable containing the domain of the development environment.
  2. In Authorization:plaintextCopy codeBearer {{emailToken}}
    • {{emailToken}} is a collection variable storing the login token.
  3. In the Body:jsonCopy code{ "username": "{{username}}", "password": "{{oldPass}}" }

Managing Variables in Scripts

Creating or Updating Variables

javascriptCopy codepm.environment.set("token", "eyJhbGciOiJIUzI...");
pm.collectionVariables.set("version", "v1.0");
pm.global.set("timeout", 5000);

Retrieving Variable Values

javascriptCopy codeconst token = pm.environment.get("token");
const version = pm.collectionVariables.get("version");

Deleting Variables

javascriptCopy codepm.environment.unset("token");
pm.collectionVariables.unset("version");

Example: Request Sign Up with Variable Usage

  1. Environment Variables:
    • {{devURL}}: Domain of the development environment.
    • {{email}}, {{oldPass}}: Authentication details.
  2. Collection Variables:
    • {{randomEmail}}, {{oldPass}}: User account details.
  3. Pre-request Script:javascriptCopy codepm.collectionVariables.set("randomEmail", "user+" + Math.random().toString(36).substring(2) + "@example.com");
  4. Test Script:javascriptCopy codeconst email = pm.collectionVariables.get("randomEmail"); pm.test("Verify email in response", () => { pm.expect(pm.response.json().email).to.eql(email); });

Conclusion

Using variables in Postman streamlines your API testing process, especially when working with multiple environments and complex datasets. By leveraging these features, you can standardize workflows and achieve greater efficiency in your tasks. Start applying these techniques today!

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment