DEV Community

Cover image for Build a Postman Alert for HackerNews
Joyce Lin for Postman

Posted on • Originally published at blog.postman.com on

Build a Postman Alert for HackerNews

It’s every founder’s worst nightmare: your startup is going viral on HackerNews (HN), and you hear about it first from a customer, or worse yet, an investor. HackerNews is a message board known for its opinionated pundits stirring up discussions on everything related to technology. While it’s important to keep a pulse on the community, you may not have the resources or bandwidth to scour the interwebs for any mention of your company.

Let’s set up and customize your own Postman alert by cobbling together a few API calls and scheduling a cron job to run on Postman servers. In this example, we can watch HackerNews for any mention of our company, and then get notified on Discord.

A monitor is like a cron job running on Postman servers
A monitor is like a cron job running on Postman servers

Set up the alert

Fork the collection from this HackerNews public workspace by clicking the orange Run in Postman button below. If you also choose to “watch” the collection, you can get notified about updates when new API workflows are added to the collection.

Next, configure two parameters and schedule a monitor in your own workspace.

1. Set config : Under the Body tab of the “Set config” request, update the keyword to the name of your own startup. Then update the destination to your own discord webhook (or other type of webhook).

set the  raw `keyword` endraw  and  raw `destination` endraw  parameters to configure the monitor
Set the keyword and destination parameters to configure the monitor

2. Schedule a monitor : Once you have the alert working locally like you want it, schedule a monitor to run this collection from Postman servers as frequently as every five minutes.

Schedule a monitor to run as frequently as every five minutes
Schedule a monitor to run as frequently as every five minutes

And that’s it!

In the next section, let’s see what’s happening behind the scenes.

The API workflow

The API workflow to send an alert based on content in HackerNews stories
The API workflow to send an alert based on content in HackerNews stories

If you forked the collection, you can review the code under the Pre-request Script and Tests tabs for each API call. The API workflow utilizes the first four requests in the collection.

  • POST Set config : The first request “Set config” is a dummy request to httpbin. It requires setting two parameters that are saved as collection variables to be used in subsequent requests.
  • GET Get HN Top stories : This request queries the HN API to retrieve the top stories.
  • GET Get HN item by id : The third request queries the HN API to check each story retrieved in the previous step, looking for the keyword specified. Postman will continue to cycle on the current query until there are no more stories to check. If the keyword is detected, Postman moves on to the last request.
  • POST Send alert : This request is only triggered when the keyword is detected in the previous step, and issues an alert to Discord about the HN story containing the keyword. This request can also be updated to trigger alerts on other platforms.

Conditional logic under the Tests tab to send an alert only when keyword is detected
Conditional logic under the Tests tab to send an alert only when keyword is detected

If you caught all of that, then you understand these fundamental concepts in Postman:

  • Collection: A collection is a group of requests. This is helpful for organizing your work and collaborating with teammates. But it’s also the foundational building block for more advanced features in Postman, such as executing a more complex API workflow.
  • Scripting: You can add JavaScript code to run before and after an API call as pre-request and test scripts, respectively. This is the basis of test assertions once you receive a response from the server. But also allows you to control your API workflows, and incorporate branching and looping logic, like we did in this example.
  • Monitor: A monitor is like a cron job running on Postman servers. Lots of people use them to monitor the health of their websites and APIs. But you can also use them to run any kind of API workflow on a scheduled cadence. Monitors are closely related to webhooks, which are actually monitors triggered by an event.

In addition to this HackerNews alert, browse other samples and documentation for the Official HN API in this public workspace. You can swap out HackerNews with other social media sites. Or you can swap out Discord for a different platform to receive a notification. And once you learn how to build one alert, you can build an army of them to be your eyes and ears across the Internet.

The post Build a Postman Alert for HackerNews appeared first on Postman Blog.

Top comments (0)