Environment & Variables
Variables enable you to store and reuse values in Postman. By storing a value as a variable, you can reference it throughout your collections, environments, requests, and test scripts. Variables help you work efficiently, collaborate with teammates, and set up dynamic workflows.
Ex: base_url = https://reqres.in
API : https://reqres.in/api/users/2
Means: base_url/api/users/2
An environment is a set of variables you can use in your Postman requests.
Variable scopes: Postman supports variables at different scopes, these scopes are: global, collection, environment, data, and local.
- Global variables enable you to access data between collections, requests, test scripts, and environments. Global variables are available throughout a workspace. Since global variables have the broadest scope available in Postman, they’re well-suited for testing and prototyping. In later development phases, use more specific scopes.
- Collection variables are available throughout the requests in a collection and are independent of environments. Collection variables don’t change based on the selected environment.
- Collection variables are suitable if you’re using a single environment, for example for auth or URL details.
- Environment variables enable you to scope your work to different environments, for example local development versus testing or production. One environment can be active at a time. If you have a single environment, using collection variables can be more efficient, but environments enable you to specify role-based access levels.
- Data variables come from external CSV and JSON files to define data sets you can use when running collections with Newman or the Collection Runner. Data variables have current values, which don’t persist beyond request or collection runs.
- Local variables are temporary variables that are accessed in your request scripts. Local variable values are scoped to a single request or collection run, and are no longer available when the run is complete. Local variables are suitable if you need a value to override all other variable scopes but don’t want the value to persist once execution has ended.
If a variable with the same name is declared in two different scopes, the value stored in the variable with narrowest scope will be used. For example, if there is a global variable named username and a local variable named username, the local value will be used when the request runs.
Variable types : Beyond scope, global and environment variables can also be defined by type. The two variable types that you can configure for global and environment variables are
- Default type is automatically assigned to variables. This type is shown as plain text and doesn’t have extra properties.
- Secret type masks the initial and current values for all workspace members and can be used to prevent unintentional disclosure of sensitive data, including API secrets, passwords, tokens, and keys.