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
- Open the Environments tab in the left-hand menu.
- Select Globals from the list.
- Click Add and input the Key (variable name) and Value (value).
- Click Save to apply changes.
Creating Environment Variables
- In the Environments tab, click Add to create a new environment.
- Name the environment (e.g., Development or Production).
- Add variables and their respective values to the Key-Value table.
- Click Save to finalize.
Creating Collection Variables
- Open the Collections tab and select the desired collection.
- Navigate to the Variables section.
- Add variables in the Key-Value table.
- Click Save to store them.
Creating Local Variables
- In the Pre-request Script or Test Script section of a request, use the following syntax:javascriptCopy code
pm.variables.set("localVar", "local variable value");
Creating Data Variables
- Prepare a CSV or JSON file with the dataset.
- 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:
- In the URL:plaintextCopy code
https://{{devURL}}/api/v1/users
{{devURL}}
is an environment variable containing the domain of the development environment.
- In Authorization:plaintextCopy code
Bearer {{emailToken}}
{{emailToken}}
is a collection variable storing the login token.
- 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
- Environment Variables:
{{devURL}}
: Domain of the development environment.{{email}}
,{{oldPass}}
: Authentication details.
- Collection Variables:
{{randomEmail}}
,{{oldPass}}
: User account details.
- Pre-request Script:javascriptCopy code
pm.collectionVariables.set("randomEmail", "user+" + Math.random().toString(36).substring(2) + "@example.com");
- Test Script:javascriptCopy code
const 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!