My Postman
Tags: postman, javascript, rest, api
Published: 2019-12-28 17:05:06


---
title: My Postman
published: true
description: A journey into getting more out of your favourite API Client
tags: postman, JS, REST, API 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/mc4n2c7vi16qies67d8v.png
---
  
Almost everyone who works into web development, front-end or back-end, has in some part of their development career used "Postman" or some other API client to test the app we are building. But, do we all know that Postman provides so much more than what we usually use it for.  
  
Let's see how we can be more productive and use Postman to its full potential.  
*Since this is not an introduction, I am assuming that everyone here is familiar with basic Postman features such as sending requests, saving requests, creating collections, blah blah blah. *  
  
Time to deliver some letters. :stuck_out_tongue:  
  
## Environments and Variables:  
  
Setting up environments can boost up your productivity if your application is deployed in different systems. With environments, we can switch between different versions in no time.  
It's also better to create different environments for the different applications we are working on so that we don't mix up the variables from one application into another.  
  
Variables provide a dynamic nature to the values we use in our application. We can set variables in different scopes, thus we not have to carry the values from the requests manually from place to place.  
  
Postman supports the following variable scopes:  
- Global  
- Collection  
- Environment  
- Data  
- Local  
  
The heirarchy of variavbles scopes is like: Data < Local < Environment < Collection < Global  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/un118ase2zj7jj0qy8m1.png)  
  
We can set the environments by clicking on the "gear" (:gear:) icon in the top right corner.  
![Click on the gear button](https://thepracticaldev.s3.amazonaws.com/i/yom4ahs52uxzpo1tab0h.png)  
  
We can set the environment variables while adding the environments.  
![Enter the values and click "Add"](https://thepracticaldev.s3.amazonaws.com/i/sloovohe55fa0qh80r2u.png)  
  
We can easily switch between the environments created.  
![Once done, we can see the environments.](https://thepracticaldev.s3.amazonaws.com/i/dfp9c5v3ds1i3jkuagix.png)  
  
In order to use the variables declared, we simply have to enclose the variables names in double curly braces.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/887z6o6wjzovjwuloju9.png)  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ixckly8z7h8tta0g4dyd.png)  
## Scripts:  
  
Scripts can be of two types:  
- Pre-request Script  
- Test Script  
  
### Pre-request Scripts:  
Pre-request Scripts gives us the option to execute something right before sending a request. This can be anything such as setting a variable.  
Say, we want to set the date variable to the current date in every request, we can do it by adding the following in the Pre-Request Scripts section  
```javascript  
pm.environment.set("time", new Date());  
```  
### Other Scripts (Tests):  
  
The "Test" section serves a generic purpose of running test cases as well as using it to perform some other tasks such as setting up variables dynamically, chaining requests, etc., right after a request has finished running.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4uhp2fdl6dvndqplha0z.png)  
  
All this happens due to the Postman Sandbox.  
Postman Sandbox is a JavaScript execution environment that lets us play with our requests and getting more out of it.  
  
Postman also provides the console option where we can debug side-by-side the tests that we are writing.  
We can make an entry into the console using the simple JavaScript console utilities such as 
```
console.log(),
console.error(),
console.info()
```

Now, let's say we have to save a parameter from the response into a variable. We can simply write a script for that.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0ek0329euxqdhgiiufi1.png)  
  
The value for "token" will be set in the environment and we can use it later just like any other variable.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/n6t3eqw43nrn56s6tzuc.png)  
  
  
## Chaining Requests:  
  
Variables are not only useful in switching between the environments, but they also come really handy when we want to set the values from one request and use it into another. We can do this using the scripts we just learned about.  
  
Let's see how we can do this:  
Suppose we want to fetch the current weather of a particular place.  
We are using two APIs to fulfill our purpose:  
- [LocationIQ](https://locationiq.com "LocationIQ Homepage")  
- [Dark Sky](https://darksky.net/dev "Dark Sky Dev Homepage") 
 
We send our location to LocationIQ and get the latitude-longitude pair. We then use these values to get the weather using the Dark Sky API.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/9u3hssm6dgc9xla58tsk.png)  
  
The values have been set (we can see the preview on hovering)  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/hg6xgfqb0fveuh19az53.png)  
  
We simply have to use the variables to get the values from the previous request into the current one.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/yiuy8l5bds7p1ntmdn5i.png)  
  
## Adding Tests:  
  
We can run a sequence of tests on the completion of a request.  
  
Postman provides us a lot of sample test scripts that we can simply add by clicking on it.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4v8ddgo4uh09elosvsll.png)  
Here the first argument in the method test gives the name to that test, and the following argument is the function that executes the test.  
  
Postman sandbox provides us a variety of options to test the desirability of the response. We can write them as:  
```javascript  
pm.response.to.be.<condition>  
pm.response.to.not.be.<condition>  
pm.response.to.have.<condition>  
pm.response.to.not.have.<condition>  
```  
  
Say we have to check, in our weather example, that we get an OK response and the response always contains the current weather. We can do it as below:  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/c7m6kbyd3sj9i5sirt59.png)  
And then we can see if our test cases have passed in the "Test Results" section  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/237swiw5l28h4pxpyrlz.png)  
  
## Running a Collection:  
  
Postman provides us a feature called the Collection Runner. It helps us run all the APIs within a collection one after the other.  
The Collection Runner then gives us the details of all the APIs that ran successfully with all their details.  
  
Let's take a look.
I have a collection "Task Manager" which has a sub-folder "Users".  
The Users folder contains the whole lifecycle of the user form creation to login to update to finally deletion.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/6q89swb7992mzgq3vfoq.png)  
  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/jruvrw8xu3o38lpvt0hg.png)  
  
On running the collection we get something like this:  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/utk6m004j0uzel5qvw9w.png)  
All the green dots denote that the request was run successfully.  
  
We can also click on a particular request to see its info:  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/4wqqrfpk8m1ti7tdcrwq.png)  
  
## Authorization:  
  
Postman provides us a lot of options to authorize and authenticate our requests.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/1143acpsro2nmkxoiyj9.png)  
  
With respect to the option we choose, we are given further options corresponding to the option we have selected.  
Say, if we choose OAuth 1.0, we will be required to fill out the details such as "Consumer Key", "Consumer Secret", "Access Token" and "Token Secret".  
If we choose Bearer Token, then we simply have to input the JWT token which would be appended to the header of our requests, hence authenticating it.  
  
Remember the example in the scripts section? No? Scroll up.  
There we had set the token value from the response as an environment variable, we can simply use that value from the environment into our corresponding request, this sparing us of the hassle of copying the token everywhere to each one of our requests.  
  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/494b2peivdyeefigs8ww.png)  
  
Wasn't it amazing.  
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/184r7daz68k69n9prxnk.gif)  
  
  
---  
  
That was all for this article. I have some more features in mind that I may write in future posts.  
If you want to learn more about Postman features then refer to the following links:  
- [Postman Documantation](https://learning.getpostman.com/docs "Postman Docs")  
- [Postman Official YouTube Channel](https://www.youtube.com/channel/UCocudCGVb3MmhWQ1aoIgUQw "Postman YouTube")  
  
---  
Enjoy building APIs  
Cheers :beers: