How to check API health and readiness
In this guide we will use Skybear.NET to continuously check if our API is not only up and running, but also correctly processing requests.
The vast majority of uptime checker tools only allow you to send a single request and assert its response, some even only allow asserting the response HTTP status code. These shallow checks are often called uptime health checks or liveness checks.
Skybear.NET puts all that into shame by supporting complex response assertions and chaining as many requests/responses as you want💪🏼
These comprehensive checks are also called deep health checks or readiness checks.
If you still only want a simple liveness uptime check, we support those too.
Starter for multi-step script
The following snippet could be used as a starting point for your own multi-step health check. It’s actually a runnable and working script.
Open this script in the Open Editor (no signup required) and tweak it to your liking based on your own API/website.
The above script will:
- Send a
POST
request to the.../login
endpoint, simulating a fake login flow, and then extract and capture thetoken
from the response into a Hurl variableauth_token
. It will also ensure it’s a non-empty token. - Send a
GET
request to the.../user-info
endpoint to fetch the user information, and assert it’s the rightuserId
.
If you don’t want to exercise your login flow in the readiness health check, you can store the authentication token for your API in Skybear.NET account’s secrets page, and then use that as a Hurl variable in your scripts.
Using an account secret Hurl variable means you can skip step 1 in the above example script, and go straight to the second step where the Hurl variable auth_token
will need to be replaced by whatever name you entered in your account’s secrets page when you saved the authentication token.
Example - Skybear.NET Open Editor readiness check
We are going to sanity check that the Skybear.NET Script Open Editor is not only online, but also able to execute scripts successfully. This is not meant to be an exchaustive test for all its functionality, but it should make sure that:
- The backend serving the website is online.
- Requests are being processed correctly.
- Scripts are executed correctly, proving our service dependencies are fine, since execution is handled by a separate microservice.
The Open Editor allows you to write and run a script without creating an account, entirely anonymously and effortlessly.
The following screenshot showcases the full readiness health check script along with one of its execution reports:
The first request at line 1, GET
s the Open Editor webpage asserting the HTTP status code to be 200
, and then extracts from the HTML response the CSRF token into the variable csrf_token
, and asserting that it’s not empty.
In line 12, we send a followup request to an internal endpoint used by our UI to run a sample script (which itself fetches the Open Editor website, although that could be doing anything else), and ensures that the response code is 200
meaning that the test actually ran and completed successfully.
We could do further checks for the actual report results, or even try to fetch one of the links for Output resources, but even with just the above two requests we know that our service is up and running and can process script executions.
Compare the assurance you get from the above to a simple uptime checker.
Scheduled runs
Now that we have a script to check liveness, readiness, and correctness, we can create a scheduled cron trigger to make sure it runs continuously every day and sends us an email when it fails.
After you create the Skybear.NET script with the appropriate content, navigate to its Settings tab, and configure a Scheduled Cron trigger with the cron expression */15 * * * *
so that it runs every 15 minutes, every day, forever.
You can configure the trigger to notify you by email when any of the script assertions fail.