Postman Monitors - The hidden gem
Tags: postman, monitoring, automation
Published: 2020-12-22 20:52:53

Postman is a great tool. I have been using it since it was just a browser extension and really loved the idea of testing my APIs before giving it to the frontend folks.

It has been a great help while I was learning about APIs and how to create them.

It has come a long way since then. Now, there are much more things like `Mocks`, `Monitors` etc (_Actually they have been around a while but not the most popular_).

In this post, I would specifically like to highlight how we can use Postman Monitors for some super cool things.

> Before I begin, I feel obligated to point out that I am a Postman employee, but its not in my job description, nor have I been asked by anyone to write such a post. I do so, with my own free will(_like Lucifer wanted us humans to have_ :P )

## What are Postman Monitors?
So, monitors in the postman arsenal are in my opinion one of the most powerful weapons, one which we can choose to automate any kind of work(_well most of them anyway_).

* Want to make sure your website isn't down every few minutes?
* Want to run a series of tests on your APIs on regular intervals?
* Want to be notified when a product is available on Amazon?
* Want to set up a reminder when your favorite crypto reaches a benchmark(Go Litecoin).

It has got you covered!

Think of monitors as your serverless cron job alternatives, where you can customize _what to do_, _when to do it_, and _what to do after it has been done_!

I hope you're with me on this weird chain of thought!

## Enough talk, get to action already!
In this post we'll check how to set up a monitor and do a very basic task of checking if your website is up or not at regular intervals and reporting via email if it is down.

> Don't worry, we'll cover more awesome things in a future post.

First things first, you need to sign in by visiting [https://www.postman.com](https://www.postman.com/)

### Step 1: Go to a workspace

![Select a workspace](https://dev-to-uploads.s3.amazonaws.com/i/7ah03gqg75twkn2yn8m6.png)


Well, all things in Postman reside in a workspace. So, the monitor we will be creating will reside in one of the workspaces you are a part of. Everyone, is a part of _their own workspace_(termed **My Workspace**) or you can create a new Workspace if you want to group your work separately.

> Side note: We recently launched Public Workspaces and it is pretty cool, check it out [in this blog post](https://blog.postman.com/public-workspaces-why-we-created-them-what-you-can-do/) if you wanna know more.

### Step 2: From the left sidebar, select collections and create one and add a request to it.


![Create a collection](https://dev-to-uploads.s3.amazonaws.com/i/jcicdx4u219l4cmo6ke0.png)


![Add a request to your collection](https://dev-to-uploads.s3.amazonaws.com/i/szlycrcr0l6hnra4jczs.png)

### Step 3: Add details about your webpage.

![Add request details to your request](https://dev-to-uploads.s3.amazonaws.com/i/ousqrbexrkchg4lln8du.png)



Here, I will be sending a `GET` request to [https://saranshdhingra.dev](https://saranshdhingra.dev)

> Yes, this is a shameless attempt to advertise my portfolio website. I feel terrible, so if you could just visit the website, leave awesome feedback and read the rest of the article, I'll be on my way :D

In order to test it you can quickly send the request to see if everything works.

You should get a `200` status code response if your webpage is up and configured properly.

> Note that in order to run the request from the web and not the app, you may need to select the `Desktop Agent` to avoid `CORS` issues. If your website is configured to allow all `Cross Origin Requests` then you may not need this. You can select the agent in the bottom right section of the page.



![Select the proper Postman Agent](https://dev-to-uploads.s3.amazonaws.com/i/on37cskpva9vx55sfvi1.png)



### Step 4: Add a testing mechanism
We have set up how to send a request to our webpage, but how do we know if everything works perfectly. We want to automate the whole process after all!

> Enter the **Tests** tab.

Just below the bar where you entered your webpage address you may see a lot of tabs, one of them will be the `Tests` tab. When selected it looks like this:


![Postman Tests tab](https://dev-to-uploads.s3.amazonaws.com/i/4b5nj2mrrxv32hq87z3f.png)

This is another tool in the _Postman arsenal_ which I think is very powerful.

Think of this tab as a hook that runs after the request runs. So you can do all sorts of things with this, things like:
* Getting the response body and parsing content.
* Take some dynamic content and initiate another request based on that(_we'll do this sometime later_)
* Check the status code of the response received(_Bingo!_)

So, in order to test if our website is working fine, every time we run our request, we'll be checking if the `response code is equal to 200` or not.

This is how we do that:
```
pm.test("Check Status code of my website", function () {
    pm.response.to.have.status(200);
});
```

So, put this in the `Tests` tab and `Run` the request. You should see that your test passed(check it in the `Test Results` tab in the response section).


![Test Results in postman](https://dev-to-uploads.s3.amazonaws.com/i/zpososgsnslo4xgcij1y.png)


> Try changing the status code in the test temporarily and running the request again to see how it behaves when the test fails.

**Save the request, and now we can move to the monitors part of the tutorial**

### Step 5: Create a monitor for your collection
![Create a monitor](https://dev-to-uploads.s3.amazonaws.com/i/46jz8xhpo4ls7c1fhbcb.png)



From the left sidebar, select `Monitors` and `Create a Monitor`.

* Enter a meaningful name for your monitor.
* Select the collection that we created earlier.
* For now, you can keep the Version as `CURRENT`, and Environment as `No Environment`, we'll use them in future tutorials.
* Select the desired frequency. For a normal, portfolio-like website an hourly frequency should suffice.

> Please note that if your monitoring calls have a quota, you get 1000 monitoring calls free every month. You can check your current usage by going into the `Resource Usage` page.

![Monitoring usage](https://dev-to-uploads.s3.amazonaws.com/i/uzq1j55tbo8yd5gdjznb.png)



The options should look something like this:



![Set monitoring options](https://dev-to-uploads.s3.amazonaws.com/i/s2ht2men8grehvzlfyzz.png)



**Additional Options:**
You can set some additional options for more granular control over your requests.

For example, you can send the requests from **multiple regions** that ensures your website behaves similarly in different regions.

A very important option is to have an email sent to you whenever a Test fails on your monitor run.

So, if your website returns with a `non 200 status code`, you can get an email for the same.

We'll talk about cooler delivery methods in a future tutorial, but for now, let's select that.


![Additional options while creating a monitor](https://dev-to-uploads.s3.amazonaws.com/i/rp61k1i474a1vizl4a7t.png)



### Step 6: Run your monitor
You can either wait for your monitors to run at their designated time or `Run` them manually for testing. After running them, you see the records of individual instances when you had _requested/scheduled_ the monitor to run.

By clicking on a specific bar, you can see more details about the requests and the test results.

![Postman monitor Run results](https://dev-to-uploads.s3.amazonaws.com/i/twk1aicz6isglstimgrd.png)



## Conclusion
So, we created a collection, set up a test that checks for a deciding criteria to know whether the website is up or not, and then automated the whole process to run at scheduled intervals.

I don't know about you, but I was very thrilled the first time I did this. I could imagine so many possibilities to achieve things that I needed to automate. Things that I knew how to achieve in code, but this was a whole other domain, no need to manage `crons`, no need to manage servers, and all from a tool that I use to test my APIs already.

Right now this is just a basic run through of a part of the product, but we'll get into more details, and we'll try to achieve something cool every time.

**Stick with me, and you'll find Zen!**
_Actually no, I'm finding that myself, send me a link when you find it :P_