1. Monitoring as Code
  2. Use the TypeScript SDK

Monitoring as Code

Use the TypeScript SDK

@onlineornot/api is a typed TypeScript client for the OnlineOrNot REST API. Use it in deploy scripts, CI jobs and internal tools to create checks, read results and manage status pages from code.

The SDK is generated from the API's OpenAPI description. It uses the Fetch API, ships as ESM with type declarations, and has no runtime dependencies. It is on 0.x, so operation names can change between minor releases.

Install

npm install @onlineornot/api

Authenticate

Create a token in API Tokens with access to the resources your code needs, and keep it in a server-side environment variable:

import { client } from '@onlineornot/api';

client.setConfig({
  auth: process.env.ONLINEORNOT_API_TOKEN,
});

Don't use an API token in browser code.

List checks

Every operation returns { data, error, request, response }. The API's response envelope is kept as is, so the records are in data.result.

import { listChecks } from '@onlineornot/api';

const { data, error } = await listChecks();
if (error) throw error;
console.log(data.result);

Create an uptime check

import { createUptimeCheck } from '@onlineornot/api';

const { data, error } = await createUptimeCheck({
  body: {
    name: 'API health check',
    url: 'https://api.example.com/health',
    test_interval: 60,
    test_regions: ['aws:us-east-1', 'aws:eu-central-1'],
  },
});
if (error) throw error;
console.log(data.result.id);

Create a browser check from a Playwright file

createBrowserCheck accepts a Playwright Test script. Read it from a self-contained .spec.js file:

import { readFile } from 'node:fs/promises';
import { createBrowserCheck } from '@onlineornot/api';

const script = await readFile('checks/homepage.spec.js', 'utf8');

const { data, error } = await createBrowserCheck({
  body: {
    name: 'Homepage Playwright check',
    script,
    test_interval: 300,
    test_regions: ['aws:us-east-1'],
  },
});
if (error) throw error;

The script must be one standalone file that uses @playwright/test. Local imports and extra dependencies aren't uploaded. See Create a scripted browser check.

Create a heartbeat

import { createHeartbeat } from '@onlineornot/api';

const { data, error } = await createHeartbeat({
  body: {
    name: 'Nightly backup',
    report_period_cron: '0 3 * * *',
    timezone: 'UTC',
    grace_period: 900,
  },
});
if (error) throw error;

Validate input with Zod or Valibot

Schemas generated from the same OpenAPI description are available from optional entrypoints. Install the validator you use:

import { zCreateCheckBody } from '@onlineornot/api/zod';

const input = zCreateCheckBody.parse({
  name: 'Website',
  url: 'https://example.com',
  test_interval: 60,
});

Use @onlineornot/api/valibot for Valibot schemas, which have a v prefix.

Use an isolated client

createClient gives you a client with its own token and fetch, without changing the shared one:

import { createClient, listChecks } from '@onlineornot/api';

const onlineornot = createClient({
  auth: process.env.ONLINEORNOT_API_TOKEN,
});

const result = await listChecks({ client: onlineornot });

What the SDK covers

The SDK has an operation for each public API endpoint: uptime, browser, DNS and TCP checks and their results and incidents, heartbeats, status pages and their components, incidents, maintenance and subscribers, maintenance windows, webhooks and their deliveries, environment variables, API tokens, users and audit logs. The API documentation describes every request and response.

If you'd rather declare monitoring than script it, use the Terraform provider. For shell scripts and CI, the CLI wraps the same API.

The source is in the OnlineOrNot/onlineornot repository.