Variables in Postman are used to store reusable values such as URLs, tokens, IDs, and test data. They allow the same value to be used across multiple API requests without manually entering it each time.
- Variables make API requests easier to manage and maintain.
- They can store different values for environments such as Development, Testing, and Production.
- Postman supports Global, Collection, Environment, Local, and Data variables.
Creating and Using Variables in Postman
Step 1: Environment in Postman
It allows you to Create Environments in Postman facilitating the organization of variables based on different contexts (e.g., development, production).
Step 2: Creating an Environment
- Open Postman and go to the "Environment" tab.
- Click "Add Environment" and name it (for example, "JSONPlaceholder").
- Define variables like "BaseURL" and set their corresponding values to "https://jsonplaceholder.typicode.com/"
Step 3: Environment Variable in Postman
In each request, use environment variables by enclosing them in double curly braces, like {{baseURL}}. This allows seamless switching between environments.
- Now, create a new request to the "https://jsonplaceholder.typicode.com//albums" endpoint.
- In the request, use the environment variable by enclosing it in double curly braces, like {{BaseURL}}/albums.
Step 4: Creating an Environment Variable in Postman
Navigate to the created environment and click "Add new variable" Define a new variable (e.g., "apiKey") and set its value.
Step 5: Global Variables in Postman
Global variables are accessible across all environments. Create them by going to the "Globals" tab and adding variables like "authToken."
Step 6: Creating a Global Variable in Postman
Similar to environment variables, define global variables by clicking "Add" in the "Globals" tab.
Step 7: Using a Global Variable in Postman
Utilize global variables in requests, enhancing reusability and consistency across different environments.
Variable Scopes
Postman provides different variable scopes based on where and how the values are used.
.png)
- Local Variables: Local variables are temporary variables available only within the scope in which they are created, such as a request or script.
- Data Variables: Data variables are supplied through CSV or JSON data files during collection runs and are mainly used for data-driven testing.
- Environment Variables: Environment variables belong to a specific environment and are useful for values that change between environments, such as development, testing, and production URLs.
- Collection Variables: Collection variables belong to a specific collection and can be shared by requests within that collection.
- Global Variables: Global variables are available across the workspace and can be accessed by requests and scripts across different collections and environments.
Use Cases of Variables in Postman
Variables in Postman are used to store and reuse values across API requests, environments, collections, and test workflows.
- Dynamic Request URLs: Use variables for base URLs, API versions, resource IDs, and other URL components.
- Authorization: Store API keys, access tokens, and other authentication values for use in request headers.
- Test Data Management: Store and reuse test data such as usernames, IDs, email addresses, and input values.
- Environment Management: Use different variable values for Development, Testing, Staging, and Production environments.
- Automated Testing Workflows: Pass values between requests and scripts to create reusable and maintainable API test workflows.
Advantages of Variables in Postman
Variables in Postman make API testing more flexible, reusable, and easier to maintain by allowing values to be stored once and reused across multiple requests.
- Reduce repeated data entry by reusing values such as URLs, tokens, IDs, and test data.
- Support easy switching between environments such as Development, Testing, Staging, and Production.
- Improve request maintainability by allowing values to be updated in one place.
- Enable data-driven testing by using different input values during collection runs.
- Simplify API automation by using variables in requests, headers, bodies, and scripts.