Effective Variable Usage in Postman
Introduction
Utilizing variables efficiently in Postman can significantly enhance your API testing and development workflow. This guide focuses on effective strategies for incorporating variables into your requests, scripts, and environments. By mastering variable usage, you'll streamline your testing process and improve collaboration within your team.
What is Variable ?
In Postman, variables are placeholders that allow you to store and reuse values across different parts of your API requests and tests. They provide a way to make your requests dynamic and adaptable to different scenarios.
Variable scopes
In order from broadest to narrowest, 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.
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.
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.
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.
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 namedusername
, the local value will be used when the request runs.Postman stores variables as strings. If you store objects or arrays, remember to
JSON.stringify()
them before storing, andJSON.parse()
them when you retrieve them.
Initial and current values
When you edit variables, each one has an Initial value and Current value:
Initial value is a value that's set in the element (collection, environment, or globals) where the variable is defined. This value is synced to Postman's servers, and is shared with your team when you share that element. The initial value can be useful when collaborating with teammates.
Current value is used when sending a request. These are local values, and aren't synced to Postman's servers. If you change a current value, it won't be persisted in the original shared collection, environment, or globals.
You can persist or reset current values you have changed in variables. For more information, see sharing and persisting data.
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.
Defining Variables
1.Defining global variables
To view global variables, do the following:
Select Environments in the sidebar.
Select Globals.
To add a new global variable, do the following:
Select Add a new variable, and enter a name for the variable.
Select a Type for the new variable.
Add an Initial Value, and if you choose, a Current Value.
Select ๐พ Save to confirm your changes.
To edit an existing global variable, do the following:
Change the desired variable value.
Select ๐พ Save to confirm your changes.
2.Defining environment variables
To view environment variables, do the following:
Select Environments in the sidebar.
Select the environment you want to inspect variables for.
To add a new environment variable, do the following:
Select Add a new variable, and enter a name for the variable.
Select a Type for the new variable.
Add an Initial Value, and if you choose, a Current Value.
Select ๐พ Save to confirm your changes.
To edit an existing environment variable, do the following:
Change the desired variable value.
Select ๐พ Save to confirm your changes.
3.Defining collection variables
You can add collection variables when you create the collection or at any time after that.
To create or edit a variable for an existing collection, do the following:
Select Collections in the sidebar.
Select a collection, and then select the Variables tab.
Conclusion
By implementing the strategies outlined in this guide, you've learned how to leverage variables effectively in Postman for seamless API testing and development. Whether it's simplifying requests, enhancing scripts, or managing environments, strategic variable usage is key to optimizing your workflow. Keep exploring Postman's versatile features to further elevate your testing capabilities and drive success in your projects.