Skip to content

A Node.js library for interacting with the Healthchecks.io API

License

Notifications You must be signed in to change notification settings

pauldenver/healthchecks-io-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Healthchecks.io API Client GitHub package.json version Build Status Coverage Status codecov

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.

Table of contents

Installation

Using NPM:

$ npm install healthchecks-io-client

Using Yarn:

$ yarn add healthchecks-io-client

Usage

Initializing a 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.

Example import methods:

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.

Using environment variables in an application:

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();

Setting environment variables before running an application:

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"

Options

HealthChecksPingClient Options:

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

HealthChecksApiClient Options:

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

Examples

HealthChecksPingClient Examples:

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');
  }
}

HealthChecksApiClient Examples:

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);
  }
}

Documentation

HealthChecksPingClient Documentation:


ping Wrapper Documentation:

  • ping(uuid, action, payload) - Send a "success" or "fail" ping on task completion or task failure (with optional payload) - Healthchecks.io Documentation

HealthChecksApiClient Documentation:


Change Log

The CHANGELOG contains descriptions of notable changes.

License

This software is licensed under the Apache 2 license.

About

A Node.js library for interacting with the Healthchecks.io API

Resources

License

Stars

Watchers

Forks

Packages

No packages published