How to Use Environment Variables in Postman?

PostmanREST APISOAP API

We can use Environment Variables in Postman. We can configure, obtain and delete an Environment variable at runtime with the help of scripts. This is achieved by the pm.* function. The scripts to use an Environment variable can be included either in the Tests or Pre-Request Script tab

.To set an Environment variable, the script should be −

pm.environment.set('<name of Environment variable>', '<value of variable>')

To get an Environment variable value, the script should be −

pm.environment.get('<name of Environment variable>')

To get the value of the Environment variable in the Postman console, the script should be −

console.log(pm.environment.get('<name of Environment variable>'))

To delete an Environment variable, the script should be −

pm.environment.unset('<name of Environment variable>')

Let us try to use an Environment variable env_variable1.

Step1 − Add the below script to set the value of an Environment variable.

pm.environment.set('env_variable1', 'val1')

Step2 − Add the below script to get the value of Environment variable env_variable1 and print it in console.

console.log(pm.environment.get('env_variable1'))

Step3 − Add the below script to delete the value of Environment variable env_variable1 and verify it.

pm.environment.unset('env_variable1'))
console.log(pm.environment.get('env_variable1'))

Step4 − Click on Send to execute a request.

Step5 − After the Response is received, open the Postman Console. It first displays val1 which is the value set for the Environment variable env_variable1. Next, it displays undefined since the value of the Environment variable env_variable1 has been deleted in Step3.

raja
Published on 25-Jun-2021 15:04:17
Advertisements