What Are Variables in Postman: Definition and Usage
variables are placeholders that store values you can reuse across requests and environments. They help make your API tests flexible and easier to maintain by allowing dynamic data insertion.How It Works
Think of variables in Postman like labeled jars where you keep ingredients for cooking. Instead of writing the same ingredient name every time, you just refer to the jar's label. This way, if you want to change the ingredient, you only update the jar once, and all recipes using it get updated automatically.
In Postman, variables store values such as URLs, tokens, or user data. You can define them at different levels like global, environment, collection, or local. When you run a request, Postman replaces the variable placeholders with their actual values, making your tests adaptable and easier to manage.
Example
This example shows how to use a variable to store a base URL and reuse it in a request.
pm.environment.set("baseUrl", "https://api.example.com"); pm.sendRequest({ url: pm.environment.get("baseUrl") + "/users", method: 'GET' }, function (err, res) { pm.test("Status code is 200", function () { pm.expect(res).to.have.property('code', 200); }); });
When to Use
Use variables in Postman when you want to avoid repeating the same values in multiple requests. For example, store API base URLs, authentication tokens, or user IDs as variables. This makes it easy to switch between environments like development, testing, and production without changing each request manually.
Variables also help when you need to run tests with dynamic data, such as creating a user and then using that user's ID in later requests. They keep your tests clean, organized, and scalable.
Key Points
- Variables store reusable values to simplify API requests.
- They can be defined at global, environment, collection, or local levels.
- Using variables makes tests flexible and easier to maintain.
- Postman replaces variable placeholders with actual values during request execution.