Sign up free

API · Webhooks

Webhook alerts

A webhook alert channel makes 12happy send an HTTP POST with a JSON body to a URL you choose, whenever a monitor routed to it has something to report: an outage, a recovery, an expiring certificate and the other events below. This page is what that request looks like and how to check that it came from us. Webhook channels are on the Pro and Agency plans.

When it is sent

One request per event. A down alert follows the monitor’s alert routes, which can filter by severity and wait before sending (an escalation delay). An all-clear goes only to the channels that were told about that incident.

eventtitle, and what it means
incident.opened<monitor> — DOWN
A failure confirmed by enough regions. The title says DEGRADED instead when the incident is a degraded one (data.severity is "degraded").
incident.closed, monitor.recovered<monitor> — RECOVERED
The monitor is back up. The summary names the regions that confirmed it.
monitor.degraded<monitor> — DEGRADED
The monitor answers, but outside its limits.
monitor.flapping<monitor> — FLAPPING
Repeated outages in a short window. Further notifications are held back until it settles.
incident.acknowledged<monitor> — ACKNOWLEDGED
Someone took the incident. It is still down; the summary says who.
maintenance.started, maintenance.endedMaintenance started, Maintenance ended
A maintenance window began or ended. The title does not name a monitor.
ssl.expiring<monitor> — CERTIFICATE EXPIRING
A TLS certificate is close to its expiry date.
domain.expiring<monitor> — DOMAIN EXPIRING
A domain registration is close to its expiry date.
domain.unlocked<monitor> — DOMAIN UNLOCKED
The domain can now be transferred to another registrar.
dns.changed, dns.restored<monitor> — DNS CHANGED, <monitor> — DNS BACK TO NORMAL
The DNS records changed, or went back to the known ones.
visual.changed, visual.blank, visual.restored<monitor> — LOOKS DIFFERENT, <monitor> — BLANK PAGE, <monitor> — LOOKS NORMAL AGAIN
A visual monitor: the page differs from its reference, is blank, or looks like the reference again.

The request

A POST with these headers:

HeaderValue
Content-Typeapplication/json
User-Agent12happy-webhooks/1.0
X-TimestampUnix time in seconds (UTC), taken when this attempt is sent. A retry has a new one.
X-SignatureOnly when the channel has a signing secret. Hex of an HMAC-SHA256, below.

The body

A JSON object with these keys. The order of the keys is not guaranteed and new keys may appear, so parse the JSON rather than comparing it as text.

KeyValue
eventThe event name, from the table above.
event_idThe event’s number. It only ever increases.
cursor"evt_" followed by event_id, zero-padded to 19 digits. Pass it as since to GET /v1/events to catch up.
monitor_idThe monitor’s UUID. Empty when the event is not about one monitor, such as a maintenance window.
monitorThe monitor’s name. Empty in the same cases.
brandThe account’s brand name, or its name when it has no brand.
titleOne line, ready to show: the monitor and what happened.
summaryOne or two sentences: is it real, and where to start looking.
dataThe event itself, below.

What is in data depends on the event. Down, degraded, recovered and flapping events carry monitor_id, status,severity, confirming_regions, eligible_regions,cause, cause_detail, quorum (with required_down, required_up and eligible) and is_flapping. Certificate and domain expiry events carry expires_at and days_remaining. Visual events carry capture_id and change_percent. Maintenance, DNS and heartbeat events carry cause_detail, the sentence that says what happened.

An alert

incident.opened
{
  "event": "incident.opened",
  "event_id": 1042,
  "cursor": "evt_0000000000000001042",
  "monitor_id": "00000000-0000-0000-0000-000000000000",
  "monitor": "api.example.com",
  "brand": "Example Co",
  "title": "api.example.com — DOWN",
  "summary": "Confirmed from ams and iad (2 of 2 regions, 2 required). DNS resolution took 4,180ms then timed out; connection was never established.",
  "data": {
    "monitor_id": "00000000-0000-0000-0000-000000000000",
    "status": "down",
    "severity": "down",
    "confirming_regions": ["ams", "iad"],
    "eligible_regions": ["ams", "iad"],
    "cause": "dns",
    "cause_detail": "DNS resolution failed from ams and iad",
    "quorum": { "required_down": 2, "required_up": 2, "eligible": 2 },
    "is_flapping": false
  }
}

The all-clear

incident.closed
{
  "event": "incident.closed",
  "event_id": 1047,
  "cursor": "evt_0000000000000001047",
  "monitor_id": "00000000-0000-0000-0000-000000000000",
  "monitor": "api.example.com",
  "brand": "Example Co",
  "title": "api.example.com — RECOVERED",
  "summary": "Recovered, confirmed from ams and iad.",
  "data": {
    "monitor_id": "00000000-0000-0000-0000-000000000000",
    "status": "up",
    "severity": "",
    "confirming_regions": ["ams", "iad"],
    "eligible_regions": ["ams", "iad"],
    "cause": "",
    "cause_detail": "",
    "quorum": { "required_down": 2, "required_up": 2, "eligible": 2 },
    "is_flapping": false
  }
}

A certificate warning

ssl.expiring
{
  "event": "ssl.expiring",
  "event_id": 1051,
  "cursor": "evt_0000000000000001051",
  "monitor_id": "00000000-0000-0000-0000-000000000000",
  "monitor": "api.example.com",
  "brand": "Example Co",
  "title": "api.example.com — CERTIFICATE EXPIRING",
  "summary": "The TLS certificate for api.example.com expires in 14 days, on 15 January 2027.",
  "data": {
    "monitor_id": "00000000-0000-0000-0000-000000000000",
    "status": "ssl_expiring",
    "expires_at": "2027-01-15T10:00:00Z",
    "days_remaining": 14,
    "threshold_days": 14,
    "cause": "ssl_expiry",
    "cause_detail": "The TLS certificate for api.example.com expires in 14 days, on 15 January 2027."
  }
}

Check the signature

When the channel has a signing secret, every request carries X-Signature: the hex of an HMAC-SHA256, keyed with the secret, over the X-Timestamp value, a dot, and the raw body.

expected = hex(hmac_sha256(secret, X-Timestamp + "." + raw body))
  • Use the raw bytes of the body, as received. Parsing it and encoding it again changes the bytes, and the signature no longer matches.
  • Compare in constant time, not with ==.
  • Refuse a request whose X-Timestamp is more than five minutes from your clock. The timestamp is inside the signed text, so it can’t be changed without breaking the signature.
  • Keep the secret out of your code: read it from the environment, and refuse to run when it is missing or empty.
  • Process each event_id once (except test: true, whose event_id is always 0). A request replayed inside the five-minute window passes the signature check, and a retry from us sends the same event_id again with a new timestamp.
  • If you refuse a request, answer with an error status. Anything that isn’t 2xx is retried.

Node.js

verify.mjs
import { createHmac, timingSafeEqual } from 'node:crypto';

// rawBody is the request body exactly as received (a Buffer or a string),
// before any JSON parsing.
export function verify12happy(rawBody, headers, secret) {
  // No secret must never mean "accept everything".
  if (!secret) throw new Error('TWELVEHAPPY_WEBHOOK_SECRET is not set');

  const timestamp = headers['x-timestamp'];
  const signature = headers['x-signature'];
  if (!timestamp || !signature) return false;

  // Refuse anything more than five minutes away from now.
  const age = Math.abs(Date.now() / 1000 - Number(timestamp));
  if (!(age <= 300)) return false;

  const expected = createHmac('sha256', secret)
    .update(timestamp + '.')
    .update(rawBody)
    .digest('hex');

  const a = Buffer.from(expected);
  const b = Buffer.from(signature);
  return a.length === b.length && timingSafeEqual(a, b);
}

// Express: read the body raw, verify, then parse.
//
// app.post('/12happy', express.raw({ type: 'application/json' }), (req, res) => {
//   if (!verify12happy(req.body, req.headers, process.env.TWELVEHAPPY_WEBHOOK_SECRET)) {
//     return res.sendStatus(401);
//   }
//   const alert = JSON.parse(req.body);
//   console.log(alert.title, '-', alert.summary);
//   res.sendStatus(204);
// });

PHP

webhook.php
<?php
$secret    = getenv('TWELVEHAPPY_WEBHOOK_SECRET');

// No secret must never mean "accept everything" (getenv() gives false when it is unset).
if (! is_string($secret) || $secret === '') {
    http_response_code(500);
    exit;
}

$raw       = file_get_contents('php://input'); // the body exactly as received
$timestamp = $_SERVER['HTTP_X_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';

// Refuse anything more than five minutes away from now.
$fresh    = ctype_digit($timestamp) && abs(time() - (int) $timestamp) <= 300;
$expected = hash_hmac('sha256', $timestamp . '.' . $raw, $secret);

if (! $fresh || ! hash_equals($expected, $signature)) {
    http_response_code(401);
    exit;
}

$alert = json_decode($raw, true);
error_log($alert['title'] . ' - ' . $alert['summary']);
http_response_code(204);

The Test button

Send a test on a webhook channel in the console sends an incident.opened body with the same keys, plus "test": true, with event_id set to 0, and the same headers and signature. Its timeout is 8 seconds. Use test to tell it from a real alert.

Your response, retries and pausing

  • Answer with any 2xx status. Anything else counts as a failure, 4xx included.
  • Answer within 10 seconds. A slower answer counts as a failure.
  • Redirects are not followed: a 301 or 302 is a failure.
  • A failed delivery is retried, up to 10 attempts in all:
AttemptSent
1When the event is sent
21 minute after attempt 1 failed
35 minutes after attempt 2 failed
425 minutes after attempt 3 failed
52 hours after attempt 4 failed
68 hours after attempt 5 failed
7, 8, 9, 1024 hours after the one before failed

If an alert fails all ten attempts, which takes about 4.4 days from the first, it is marked failed and not sent again. You can still read it from the events list.

  • After 20 failed attempts in a row, 12happy pauses the channel, so a dead URL doesn’t keep using retries. Fix the receiver, then resume the channel with POST /v1/alert-channels/{id}/resume.
  • To catch up on what you missed, call GET /v1/events?since=<cursor> with the cursor of the last event you have. It returns the same events and needs an API key with the events:read scope.

Both endpoints are in the API reference for API keys.