API Monitors
API monitors are used to monitor APIs. You can use API monitors to monitor the uptime of your Website or APIs and get notified when they are down.
Timeout
REQUIREDThe timeout is used to define the time in milliseconds after which the monitor should timeout. If the monitor does not respond within the timeout period, the monitor will be marked as down. For example, 5000
will set the timeout to 5 seconds. It is required and has to be a number greater than 0.
URL
REQUIREDThe URL is used to define the URL of the API that you want to monitor. It is required and has to be a valid URL.
Method
REQUIREDThe method is used to define the HTTP method that should be used to make the request. It is required and has to be one of the following: GET
, POST
, PUT
, PATCH
, DELETE
.
Headers
The headers are used to define the headers that should be sent with the request. It is optional and has to be a valid JSON object.
Eval
The eval is used to define the JavaScript code that should be used to evaluate the response. It is optional and has be a valid JavaScript code.
This is a anonymous JS function, by default it looks like this.
NOTE: The eval function should always return a json object. The json object can have only status(UP/DOWN/DEGRADED) and lantecy(number)
{status:"DEGRADED", latency: 200}
.
(function (statusCode, responseTime, responseDataBase64) {
let statusCodeShort = Math.floor(statusCode/100);
let status = 'DOWN'
if(statusCodeShort >=2 && statusCodeShort <= 3) {
status = 'UP',
}
return {
status: 'DOWN',
latency: responseTime,
}
})
statusCode
REQUIRED is a number. It is the HTTP status coderesponseTime
REQUIREDis a number. It is the latency in millisecondsresponseDataBase64
REQUIRED is a string. It is the base64 encoded response data. To use it you will have to decode it
let decodedResp = atob(responseDataBase64);
//if the response is a json object
//let jsonResp = JSON.parse(decodedResp)
Example
The following example shows how to use the eval function to evaluate the response. The function checks if the status code is 2XX then the status is UP, if the status code is 5XX then the status is DOWN. If the response contains the word Unknown Error
then the status is DOWN. If the response time is greater than 2000 then the status is DEGRADED.
(function (statusCode, responseTime, responseDataBase64) {
const resp = atob(responseDataBase64); //convert base64 to string
let status = "DOWN";
//if the status code is 2XX then the status is UP
if (/^[2]\d{2}$/.test(statusCode)) {
status = "UP";
if (responseTime > 2000) {
status = "DEGRADED";
}
}
//if the status code is 5XX then the status is DOWN
if (/^[5]\d{2}$/.test(statusCode)) status = "DOWN";
if (resp.includes("Unknown Error")) {
status = "DOWN";
}
return {
status: status,
latency: responseTime
};
});
Examples
Website Monitor
This is an example to monitor google every 5 minute.
Get Request
Example to monitor a GET Request with an Authorization header set to $SOME_TOKEN
.
$SOME_TOKEN
is set in environment variables as SOME_TOKEN
export SOME_TOKEN=some-token-example
POST Request
Example showing setting up a POST request every minute with a timeout of 2 seconds.
Custom Response Eval
Example showing how to use a custom response eval function. It also shows how to set secrets in body for POST Request. The secrets have to be present in environment variables.
export SERVICE_TOKEN=secret1_tone
export SERVICE_SECRET=secret2_secret