Making sure your website or API is up and running well can be a round-the-clock job. To help in this tireless effort, we can make uptime reports and status alerts visible within the tools we frequently use, like Slack. With a platform like Standard Library, an integration like that is nearly effortless. This guide will show you how to set up an API on Standard Library that will ping a URL and send a message to Slack if the URL doesn’t return a 2xx status code. It will leverage Standard Library’s scheduled tasks, which allow you to execute APIs based on nearly any schedule.
If you’re not familiar with Standard Library, we’re a serverless platform that enables everyone to build, ship, and integrate scalable APIs in a matter of seconds. With the help of our online text editor, Code.xyz, you can do it right from your browser.
Head over to the Slack app dashboard, and click “Create New App” in the top-right corner of the screen. After a moment, you’ll be redirected to a page labeled “Basic Information”. On the left side of the screen, you’ll see “OAuth and Permissions” under the “Features” category.
Once there, scroll down to “Scopes”. We need the bot to be able to send messages to Slack on behalf of itself. That means we need to give it “chat:write:bot” permissions, so search for that in the “Select Permission Scopes” drop-down.
With the desired permissions set, scroll back up to the top of the “OAuth and Permissions” page and click the green “Install App to Workspace” button. Once you authorize it, the page will refresh and where the install button once was will be an OAuth token. Click copy; you’ll need that in just a moment.
With Slack all squared away, open up Code.xyz. If you follow that link, a template for the ping API will automatically open. Otherwise, you can click on the “Community API Sources” tab and search for “steve/ping”.
Once it’s loaded, you’ll land on the __main__.js
endpoint. It takes a single parameter, url
. The function makes an HTTP request to url
and checks if the status code is in the 2xx range. If it’s not, it uses the Slack SDK to send a message to a channel specified by the environment variable SLACK_CHANNEL_ID
.
The heavy lifting is done in the /src/request.js
file. Feel free to peruse that file, but in essence, it makes a GET request to a URL and records some information about the request. It will return the status code and timing information like the time to first byte and total request duration.
After the ping, we check if the status code is less than 200 or greater than 299. If it is, message slack with the details of the request, formatted with the function formatPing
. formatPing
takes the object returned by the request
function, stringifies it, and wraps it up in triple backticks. Slack will display anything surrounded by three backticks as a code block, which can make JSON look a little cleaner. For more on formatting see the Slack documentation.
If you open up env.json
, you’ll see two environment variables, SLACK_OAUTH_TOKEN
and SLACK_CHANNEL_ID
. The former is what we just got from the Slack app dashboard, and the latter is the name of the channel you want the bot to post to, like #general
or #alerts
. Make sure you fill out the “dev” set, as shown below.
Now seems like an appropriate time to test this API out. Go back to __main__.js
and press cmd/ctrl+b to open the parameters pane in the bottom-left of the screen. Type in a URL and hit enter. If all is well for whatever website/API is at that URL, you should see nothing!
That’s because if the ping API can reach the URL with a 2xx status code, it’s a noop. To try a failing case use [https://bad.stdlib.com/](http://bad.stdlib.com/)
as the URL. In the Code.xyz results panel you’ll see the response from Slack’s API saying a message was sent. If you navigate to the channel you put in env.json
—
You’ll see a message that https://bad.stdlib.com/
was unreachable, and some information about the request.
With the ping API up, we can now schedule the API to run every few minutes. Click on the “tasks” icon at the top of Code.xyz. On the right side of the scheduled tasks page, under “Scheduled a New Task”, search for the API you just deployed.
Once you open it, you’ll see the task creation widget. To set the schedule, you have two options. By default you pick from a few drop-downs, allowing you to create intervals like “run once a minute” or “run twelve times an hour”. You can also click on “Advanced (cron)” if you like cron syntax. Whichever way you choose, it’s probably best to have the API run at least once every five minutes.
Click the blue “Schedule Task” and the task should appear on the left side of the screen.
There are many ways to expand upon this simple ping API. For instance, maybe you want to include status codes in the 3xx range (redirects) as acceptable. Well, all you have to do is change one line of code! Changing
if (ping.statusCode < 200 || ping.statusCode > 299) {...}
to
if (ping.statusCode < 200 || ping.statusCode > 399) {...}
Will do the trick! Now perhaps you guilty of not checking Slack as often as you should, and prefer good old SMS. With Standard Library, SMS integration is just a line of code away! In fact; there is another endpoint in the ping API already set up with SMS notifications. Open up extra.js
, and you’ll see —
It’s the same as __main__.js
but with the extra conditional to check if a phone number was provided. If so, it’ll send a message to prompting the recipient to get on Slack to see whats happening.
The point of these small changes is to show that at the end of the day scheduled tasks, and any API on Standard Library for that matter, are just JavaScript and fully customization to your exact use case.
I hope this tutorial has been helpful in showing you how easy it is to get started with Standard Library. For more inspiration as to how you can better use Standard Library, you can check out more guides written by the team here. If you have a neat idea you’d like to share, reach out to me directly by e-mail: [email protected], or follow the Standard Library team and me on Twitter.
Steve Meyer is a recent graduate of Oberlin College and Software Engineer at Standard Library. When he’s not programming, you can find him baking bread, or playing Spider-Man.