Documentation menu
Monitoring

Heartbeats

Monitor background jobs, cron tasks, and scheduled work with heartbeat monitors.

Heartbeat monitors let you ping a URL when your task completes. If Incidion doesn’t receive a ping within the expected interval, the monitor goes down.

When to use heartbeats

Heartbeat monitors are perfect for:

  • Cron jobs: Nightly backup, hourly report, weekly cleanup
  • Scheduled tasks: Batch processing, data sync, cache refresh
  • Async workers: Long-running background jobs
  • One-off commands: Manual operations that should complete by a certain time

Any task that doesn’t expose an HTTP endpoint (or shouldn’t expose one) is a good candidate.

Create a heartbeat monitor

  1. Open Monitors and tap + (web dashboard: Observe > Monitors, New monitor)
  2. Select Heartbeat as the type
  3. Enter a name (e.g., “Nightly backup”)
  4. Set Expected ping every (e.g., 1 day; at least 30 seconds)
  5. Tap Create monitor

Incidion generates a unique ping URL with a secret token, for example https://api.incidion.dev/api/v1/heartbeat/hb_9f3k.../. Copy it from the monitor and add it to your task. The token is the secret: anyone with the URL can ping the monitor. If it leaks, get a new one with POST /api/v1/projects/{project_id}/monitors/{id}/regenerate-heartbeat-token/; the old URL stops working.

Besides the interval you can set a grace period: how late a ping may be before the monitor counts as down (default: the interval, at most 1 hour).

Ping the endpoint

When your task completes, make a GET, HEAD or POST request to the ping URL. No API key is needed.

Curl example

# The job ran
curl https://api.incidion.dev/api/v1/heartbeat/<token>/

# The job started (the next ping records how long it ran)
curl https://api.incidion.dev/api/v1/heartbeat/<token>/start/

# The job failed: the monitor goes down at once and an incident opens
curl -X POST https://api.incidion.dev/api/v1/heartbeat/<token>/fail/ \
  -H "Content-Type: application/json" \
  -d '{"message": "backup: disk full"}'

A POST body may carry {"status": "up|down|degraded", "message": "..."} (the message is kept up to 1000 characters); any other body, such as job output, is ignored. Pings are limited to 60 per minute per token.

A ping answers 200:

{"status": "ok", "event": "success", "monitor_status": "up", "status_changed": false}

event is success, start or fail (start answers only {"status": "ok", "event": "start"}). An unknown token answers 404. A paused monitor, or one in an archived project, answers 200 with {"status": "ignored", "reason": "..."} and records nothing, so curl -f in the job does not fail.

start/ also restarts the clock: the next ping is expected within the interval plus grace from the start. If a job sends start/ and then neither a ping nor fail/, the monitor goes down when that time is up.

Python example

import os
import requests

# The ping URL of the monitor
HEARTBEAT_URL = os.environ["HEARTBEAT_URL"]

# Do your work
result = do_important_work()

# Ping when done
try:
    response = requests.get(HEARTBEAT_URL, timeout=10)
    if response.status_code == 200:
        print("Heartbeat sent")
except Exception as e:
    print(f"Heartbeat failed: {e}")

Bash (cron) example

#!/bin/bash
set -e

# Do your work
/opt/backup/nightly.sh

# Ping heartbeat on success
curl --silent https://api.incidion.dev/api/v1/heartbeat/<token>/ || echo "Heartbeat failed"

Save it as /opt/bin/nightly-backup.sh and add it to the crontab:

0 2 * * * /opt/bin/nightly-backup.sh

The backup runs at 2 AM. If it completes, it pings the heartbeat. If the cron job fails or doesn’t complete, no ping is sent, and the monitor goes down.

Node.js example

// Node 18+ has fetch built in
async function sendHeartbeat() {
  try {
    const response = await fetch(
      'https://api.incidion.dev/api/v1/heartbeat/<token>/',
      { signal: AbortSignal.timeout(10_000) }
    );
    if (response.ok) {
      console.log('Heartbeat sent');
    }
  } catch (error) {
    console.error('Heartbeat failed:', error);
  }
}

async function doWork() {
  // Your work here
  await importantTask();
  
  // Send heartbeat when done
  await sendHeartbeat();
}

doWork();

Or use an SDK: see SDKs.

Heartbeat status

A heartbeat monitor’s status is determined by the time since the last ping (or start/):

Time since last ping Status
Within the interval plus grace Up
More than the interval plus grace Down

A fail/ ping, or a POST with "status": "down", makes it down at once; "status": "degraded" makes it degraded. The next normal ping brings it back up.

Grace period

The grace period is the extra time after the interval before a missed ping counts. Without one, it is the interval itself, at most 1 hour: with a 1-day interval the monitor goes down 25 hours after the last ping, with a 10-minute interval after 20 minutes. Set it from 0 (no grace) up to 7 days.

Heartbeats are not re-checked: a missed ping counts at once (no confirmation checks).

Failed pings

If a ping request fails (network error, timeout), Incidion doesn’t count it. Your task should handle the failure:

# Robust cron example
#!/bin/bash

# Attempt to ping up to 3 times
for i in 1 2 3; do
  curl --silent --max-time 5 https://api.incidion.dev/api/v1/heartbeat/<token>/ && break
  sleep 2
done

Alerting

A heartbeat monitor that goes down opens an incident and pages people through the project’s escalation policy, like any monitor; you need no alert rule. Add an alert rule on the monitor (for example Monitor Status Change to down) to also notify Slack, email or other channels.

Older ping URLs

Monitors created before token URLs can still be pinged at /api/v1/heartbeat/<monitor id>/ with an active API key of the monitor’s project (pk_ or sk_) in the X-API-Key header. The key needs the heartbeats:write scope (403 insufficient_scope otherwise; see API keys). Switch them to the token URL shown on the monitor; token URLs need no key.

Running tasks with uncertain completion time

If your task can take variable time to complete, set the interval to the maximum expected time plus a buffer:

# Task usually takes 30 minutes, sometimes 45
# Set interval to 60 minutes (3600 seconds)

This prevents false alerts while still catching hangs. If the task regularly exceeds the interval, increase the interval.

Tips

  • Measure current duration: Run your task a few times and note the typical duration
  • Add buffer: Set interval to 1.5x the typical duration
  • Test before deploying: Create a test heartbeat monitor and ping it manually
  • Log failures: Log when the ping fails so you can debug network issues
  • Handle DNS: Use full URL (including https://) to ensure DNS is checked
  • Timeouts: Use curl --max-time to prevent hanging requests
  • Fail-fast jobs: If your task can fail, make sure the ping only happens on success