The healthchecks-io-client
library contains two simple and convenient HTTP clients for making requests to
the Healthchecks.io Ping API and the Healthchecks.io Management API.
Using NPM:
$ npm install healthchecks-io-client
Using Yarn:
$ yarn add healthchecks-io-client
The library exports two classes for interacting with the Healthchecks.io APIs and a ping
shorthand/wrapper function. The HealthChecksPingClient
class interacts with the Healthchecks.io Ping API. The HealthChecksApiClient
class interacts with the Healthchecks.io Management API.
For a HealthChecksPingClient
instance, you'll need the uuid
of a health check. For a HealthChecksApiClient
instance, you'll need an API key provided by Healthchecks.io. You can provide the API key as an apiKey
option or set the API key as an environment variable using HC_API_KEY
. By default, the client will use the HC_API_KEY
environment variable if the apiKey
option is not provided.
const {
HealthChecksPingClient,
HealthChecksApiClient
} = require('healthchecks-io-client');
// Creating a ping API client.
const pingClient = new HealthChecksPingClient({
uuid: 'HEALTH-CHECKS-UUID'
});
// Creating a management API client.
const apiClient = new HealthChecksApiClient({
apiKey: 'HEALTH-CHECKS-API-KEY'
});
import {
HealthChecksPingClient,
HealthChecksApiClient
} from 'healthchecks-io-client';
// Creating a ping API client.
const pingClient = new HealthChecksPingClient({
uuid: 'HEALTH-CHECKS-UUID'
});
// Creating a management API client.
const apiClient = new HealthChecksApiClient({
apiKey: 'HEALTH-CHECKS-API-KEY'
});
For a HealthChecksApiClient
instance, it is required to set the API key parameter as either an environment variable or as an option when instantiating the client. An API key is necessary to execute HealthChecksApiClient
calls. If an API key is not provided or cannot be determined, then an error is thrown.
const { HealthChecksApiClient } = require('healthchecks-io-client');
// Set the API key as an environment variable.
process.env.HC_API_KEY = 'HEALTH-CHECKS-API-KEY';
const apiClient = new HealthChecksApiClient();
Linux:
$ HC_API_KEY=HEALTH-CHECKS-API-KEY node app.js
Windows:
> cmd /C "set HC_API_KEY=HEALTH-CHECKS-API-KEY && node app.js"
These are the available options for creating a HealthChecksPingClient
instance.
Name | Default | Description |
---|---|---|
uuid |
undefined |
UUID of an existing health check |
timeout |
5000 |
Number of milliseconds before the request times out |
baseUrl |
https://hc-ping.com |
Base URL for the Healthchecks.io Ping API |
returnResponse |
false |
Return the response from the request |
These are the available options for creating a HealthChecksApiClient
instance.
Name | Default | Description |
---|---|---|
apiKey |
undefined |
Healthchecks.io API Key |
timeout |
5000 |
Number of milliseconds before the request times out |
baseUrl |
https://healthchecks.io |
Base URL for the Healthchecks.io Management API |
fullResponse |
false |
Get the full response instead of just the body |
maxContentLength |
10000 |
The max size of the HTTP response content in bytes |
maxBodyLength |
2000 |
The max allowed size of the HTTP request body in bytes |
Create an instance:
const { HealthChecksPingClient } = require('healthchecks-io-client');
// Creating a ping API client.
const pingClient = new HealthChecksPingClient({
uuid: 'HEALTH-CHECKS-UUID'
});
Send a "success" ping:
async function performTask() {
/*
* Add task logic here.
*/
// Signal a "success" after completion.
await pingClient.success();
}
Send a "success" ping on task completion and send a "fail" ping on task failure:
async function performTask() {
try {
/*
* Add task logic here.
*/
// Signal a "success" after completion.
await pingClient.success();
} catch(err) {
// Send a "fail" ping on failure.
await pingClient.fail();
}
}
Alternatively, instead of exporting the HealthChecksPingClient
you can export just the ping functionality.
// Export the 'ping' function.
const { ping } = require('healthchecks-io-client');
Send a "success" ping:
async function performTask(uuid) {
/*
* Add task logic here.
*/
// Signal a "success" after completion.
await ping(uuid, 'success');
}
Send a "success" ping on task completion and send a "fail" ping on task failure:
async function performTask(uuid) {
try {
/*
* Add task logic here.
*/
// Signal a "success" after completion.
await ping(uuid);
} catch(err) {
// Send a "fail" ping on failure.
await ping(uuid, 'fail');
}
}
Create an instance:
const { HealthChecksApiClient } = require('healthchecks-io-client');
// Creating a management API client.
const apiClient = new HealthChecksApiClient({
apiKey: 'HEALTH-CHECKS-API-KEY'
});
Get a list of health checks:
async function getHealthChecks() {
try {
return await apiClient.getChecks();
} catch(err) {
console.error(err);
}
}
Get a certain health check:
async function getHealthCheck(uuid) {
try {
return await apiClient.getCheck(uuid);
} catch(err) {
console.error(err);
}
}
Create a new health check:
async function createHealthCheck() {
try {
return await apiClient.createCheck({
name: 'App Check',
tags: 'prod app',
});
} catch(err) {
console.error(err);
}
}
pingClient.success(payload)
- Send a "success" ping on task completion (with optional payload) - Healthchecks.io DocumentationpingClient.fail(payload)
- Send a "fail" ping on task failure (with optional payload) - Healthchecks.io DocumentationpingClient.start()
- Sends a "job has started!" message - Healthchecks.io Documentation
ping(uuid, action, payload)
- Send a "success" or "fail" ping on task completion or task failure (with optional payload) - Healthchecks.io Documentation
apiClient.getChecks(tags)
- Gets a list of health checks - Healthchecks.io DocumentationapiClient.getCheck(uuid)
- Gets the information for a health check - Healthchecks.io DocumentationapiClient.createCheck(checkInfo)
- Creates a new health check - Healthchecks.io DocumentationapiClient.updateCheck(uuid, checkInfo)
- Updates an existing health check - Healthchecks.io DocumentationapiClient.pauseCheck(uuid)
- Disables monitoring for a check, without removing it - Healthchecks.io DocumentationapiClient.deleteCheck(uuid)
- Permanently deletes a health check - Healthchecks.io DocumentationapiClient.listPings(uuid)
- Gets a list of pings a health check has received - Healthchecks.io DocumentationapiClient.listFlips(uuid, params)
- Gets a list of flips (status changes) a health check has experienced - Healthchecks.io DocumentationapiClient.getIntegrations()
- Gets a list of integrations - Healthchecks.io Documentation
The CHANGELOG contains descriptions of notable changes.
This software is licensed under the Apache 2 license.