@heimdall-monitor/sdk
v0.1.0
Published
Heimdall Monitor SDK — cron job heartbeat helper for Node.js
Maintainers
Readme
@heimdall-monitor/sdk
Node.js SDK for Heimdall Monitor — a typed, zero-dependency helper for sending cron job heartbeat pings.
The heartbeat endpoint is language-agnostic: any tool that can make an HTTP POST request works out of the box. The SDK is a convenience wrapper for Node.js projects.
Quick Start
npm install @heimdall-monitor/sdkRequires Node.js 18 or later (uses native fetch).
Integration Examples
Both examples below call the same endpoint — pick whichever fits your stack.
Option 1 — Plain HTTP (works from any language or shell)
# Bash / curl — runs after your job succeeds
your-cron-job && curl -s -X POST https://heimdall.observer/api/v1/heartbeat/YOUR_TOKEN# Python
import urllib.request
urllib.request.urlopen(
urllib.request.Request(
"https://heimdall.observer/api/v1/heartbeat/YOUR_TOKEN",
method="POST"
)
)// Go
http.Post("https://heimdall.observer/api/v1/heartbeat/YOUR_TOKEN", "", nil)Option 2 — Node.js SDK
import { ping, wrap, configure } from "@heimdall-monitor/sdk";
// ---- Simple: call ping() after your job completes ----
async function myBackupJob() {
// ... your job logic ...
await ping("YOUR_TOKEN");
}
// ---- Auto-wrap: ping is sent only on success ----
// If myBackupJob throws, no ping is sent → Heimdall alerts you
const protectedJob = wrap(myBackupJob, "YOUR_TOKEN");
await protectedJob();
// ---- CJS (require) also works ----
const { ping } = require("@heimdall-monitor/sdk");
await ping("YOUR_TOKEN");Always
awaitthe ping (or the wrapped job) before your process exits. An unawaitedping()followed byprocess.exit()may drop the HTTP request before it is sent.
API Reference
ping(token, options?): Promise<void>
Sends POST https://heimdall.observer/api/v1/heartbeat with the token in the
Authorization: Bearer <token> header. The token never appears in the URL by
default — this keeps it out of access logs, proxies and APM traces.
| Parameter | Type | Default | Description |
| ---------------------- | -------------------- | ---------- | ---------------------------------------------------------------------------------------------------------- |
| token | string | — | Heartbeat token from the Heimdall dashboard (required — empty tokens throw immediately) |
| options.throwOnError | boolean | false | Throw if the request fails or returns a non-2xx status |
| options.verbose | boolean | false | Log a warning to stderr on error (no throw) |
| options.timeoutMs | number | 10000 | Request timeout — protects your job from a hung server |
| options.authMode | 'header' \| 'path' | 'header' | Where to send the token. 'path' uses the legacy capability URL (/heartbeat/:token) for older backends. |
Silent by default — errors never break your cron job.
// Default: silent
await ping("YOUR_TOKEN");
// Throw on failure (e.g. for CI jobs where you want hard failures)
await ping("YOUR_TOKEN", { throwOnError: true });
// Log warning without throwing
await ping("YOUR_TOKEN", { verbose: true });
// Custom timeout (fast-fail in short-lived serverless functions)
await ping("YOUR_TOKEN", { timeoutMs: 3000 });wrap(fn, token, options?): T
Higher-order function that runs fn(...args) and calls ping(token) only if fn resolves
successfully. If fn throws or rejects, no ping is sent — Heimdall detects the miss.
const protectedJob = wrap(async () => {
// heavy job logic
}, "YOUR_TOKEN");
await protectedJob(); // ping sent on success, skipped on errorNote: the wrapped function does not preserve
this. Use an arrow function or bind the method before wrapping:wrap(myJob.bind(ctx), 'YOUR_TOKEN').
configure(options): void
Override the default base URL globally. Call once at startup before any ping() calls.
import { configure, ping } from "@heimdall-monitor/sdk";
// For self-hosted Heimdall deployments
configure({ baseUrl: "https://my-self-hosted-heimdall.example.com" });
await ping("YOUR_TOKEN");
// → POST https://my-self-hosted-heimdall.example.com/api/v1/heartbeat
// Authorization: Bearer YOUR_TOKEN| Option | Type | Default | Description |
| --------- | -------- | --------------------------- | ----------------------------------- |
| baseUrl | string | https://heimdall.observer | Base URL for all heartbeat requests |
Design Principles
- Zero runtime dependencies — uses Node.js native
fetch(18+), no extra packages in yournode_modules - Silent by default — ping failures never crash your cron job; opt into
throwOnErrororverboseas needed - No retry logic — intentional: a retry after failure could mask a real miss in a dead-man's switch scenario
- Timeout protected — requests are aborted after
timeoutMs(default 10s) so a hung server never stalls your job - Token safety — the token is sent in the
Authorizationheader by default (never in the URL) and is never logged; it is redacted (<redacted>) from verbose warnings and from errors thrown withthrowOnError: true - Fail fast on bad input — an empty token throws immediately (programming error, not a network failure)
Security
The heartbeat token is a credential
The token identifies one monitor and grants exactly one capability: sending a heartbeat to it. It cannot read data, change configuration or reach other monitors. Treat it as a secret anyway — anyone with the token can keep a monitor green.
The heartbeat endpoint accepts two forms:
| Form | Request | When to use |
| ------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
| Header (recommended) | POST /api/v1/heartbeat with Authorization: Bearer <token> | SDKs, CI jobs, anything that can set headers. Keeps the token out of URLs and access logs. |
| Path (capability URL) | POST /api/v1/heartbeat/<token> | cron, curl or any language with no header support. |
The capability-URL form is the established pattern for heartbeat pings
(Healthchecks.io, Cronitor and UptimeRobot push monitors all use it) and is what
makes the integration work from a bare curl one-liner. The SDK uses the header
form by default.
If you must use the path form, remember that URLs show up in access logs, proxies and APM traces. To limit exposure:
- Rotate or revoke the token from the dashboard if it leaks (deleting the monitor invalidates its token).
- The SDK never logs the token —
verbosemode and errors thrown withthrowOnError: trueare redacted (<redacted>) from the message,stack,causeandAggregateError.errors. - The backend never logs the token — not even a prefix — and rate-limits the endpoint (120 requests/minute per IP).
- If your policy forbids secrets in URLs, use the header form (or route the heartbeat through your own proxy and inject the token there).
Reporting a vulnerability
Please report security issues privately to the maintainers instead of opening a public issue. Include reproduction steps and the affected version.
License
MIT
