Getting started with postman - Creating Environment variables in your collections.

ยท

2 min read

Getting started with postman - Creating Environment variables in your collections.

Table of contents

No heading

No headings in the article.

Testing APIs manually can be very repetitive. This article will show you how to create environment variables in postman and use them throughout your test.

Environment variables are the most commonly used kind of variables in postman. Variables on the other hand are basically placeholders or containers used to store/hold values, data, or results of some expressions. The cool thing is that this data stored in a variable can be used throughout your program.

for example

var firstname = "Esther"

from above, we have declared a variable called "firstname" and we gave it the value of "Esther".

How to Create Environment variable in postman

A typical scenario is, Imagine you are working with different servers on a project, such that we can have different environments for testing like dev, test, stage, and prod which differ mostly just by the URL. In this situation, you can create an environment file matching the different environments like dev, stage, and prod for your application. Environment variables are also used to pass data from one endpoint request to another. They can be created through the postman console as well as through postman test scripts.

For creating environment variables through the console please follow the steps below;

  1. Open your postman workspace page
  2. click on the eye icon to view environment variables. image.png
  3. Add an environment name image.png
  4. Create and save a new environment variable image.png Congrats, you have created your first environment!๐Ÿค—

Next steps

In order to use the created environment in your postman requests, you will need to select from the environment list dropdown. image.png

Now, let's work with our newly created environment. You can simply use the double curly brace syntax to access an environment variable. For example, we would be accessing the one we created above as in the screenshot below. Don't forget that you can add as many variables to your environment. image.png

In order to use Environment variables through the postman script, you can use pm.environment.set and pm.environment get to add and modify environment variables respectively.

pm.environment.set("token",5)

Postman will set this variable to the environment you have selected once you send this request. Screen Shot 2022-09-13 at 12.55.09.png

Always make sure you are using pm.environment on your script when working with environment variables.

Meanwhile, you can visit the official postman documentation to learn more about variables and how to use them.

Don't forget to like and follow my blog if you find this useful and also drop your comments ๐Ÿ™