## Webhooks

*a signed HTTPS POST per finalized transaction*

Each finalized transaction touching a listed address arrives at your HTTPS url as a signed POST. Dev, Business and Pro plans only. A delivered event or test costs 1 credit; failed tries and management calls are free. Caps: [rate limits](https://carbium.sh/docs/limits.md). Manage webhooks on the dashboard's Webhooks tab (DELIVERIES: log and test) or the API at `carbium.sh` with the dashboard session, `Authorization: Bearer <session token>` as in [account and billing](https://carbium.sh/docs/account.md), or an [account API key](https://carbium.sh/docs/account-api-keys.md); a service key is refused.

### quick start

```
# 1 create: the dashboard's API, with the session token or an account API key (never a service key)
curl -s https://carbium.sh/v1/webhooks -H 'Authorization: Bearer <session token>' \
  -H 'Content-Type: application/json' \
  -d '{"key_id":<your key_id>,"url":"https://example.com/hook","addresses":["<base58 address>"]}'
# -> {"webhook":{"webhook_id":7,"status":"active",...},"secret":"whsec_..."}
# the secret is shown once: store it. No 2xx within 15 s: 8 tries over about 27 hours.

# 2 test, once parts 3 and 4 answer on your url: one signed event with slot 0 and "test": true, tried once
curl -s -X POST https://carbium.sh/v1/webhooks/7/test -H 'Authorization: Bearer <session token>'
# -> {"event_id":"evt_test_...","outcome":"delivered","http_status":200,"reason":"","latency_ms":102,...}

// 3 receive (node, no package; save parts 3 and 4 as receive.mjs): keep the RAW body, answer 200 at once, work after
import { createServer } from "node:http";
createServer((req, res) => {
  const parts = [];
  req.on("data", (c) => parts.push(c)).on("end", () => {
    const raw = Buffer.concat(parts);
    if (!verify(process.env.WHSEC, req.headers, raw)) return res.writeHead(401).end();
    res.writeHead(200).end();
    for (const tx of JSON.parse(raw)) console.log(tx.test ? "test" : tx.slot, tx.transaction?.signatures[0] ?? tx.signature);
  });
}).listen(8080); // behind your https name on 443

// 4 verify: HMAC-SHA256 of id.timestamp.body under the secret's bytes
import { createHmac, timingSafeEqual } from "node:crypto";
function verify(secret, h, raw) {
  if (!secret) return false;
  const id = h["webhook-id"], ts = h["webhook-timestamp"];
  if (!id || !(Math.abs(Date.now() / 1000 - Number(ts)) <= 300)) return false; // 5 minutes
  const key = Buffer.from(secret.slice("whsec_".length), "base64");
  const want = Buffer.from("v1," + createHmac("sha256", key).update(id + "." + ts + ".").update(raw).digest("base64"));
  return String(h["webhook-signature"] || "").split(" ").some((s) => { const got = Buffer.from(s);
    return got.length === want.length && timingSafeEqual(got, want); });
}
```

### routes

| route | body | answer |
|---|---|---|
| GET /v1/webhooks | none | your webhooks, oldest first, no address lists |
| POST /v1/webhooks | the create fields | {webhook, secret}; secret shown once |
| GET /v1/webhooks/{id} | none | the webhook with its addresses |
| PATCH /v1/webhooks/{id} | any create field, status | the webhook |
| DELETE /v1/webhooks/{id} | none | {"deleted": true} |
| POST /v1/webhooks/{id}/rotate | none | {secret, secret_hint, previous_valid_until}; new secret shown once; old one verifies one more day |
| GET /v1/webhooks/{id}/deliveries | none; ?limit= (50, at most 200), ?before= (a line's cursor, or a Unix-ms time) | the log, newest first: a line per try, drop and test; kept at least 7 days |
| POST /v1/webhooks/{id}/test | none | {event_id, ts, outcome, http_status, reason, latency_ms, refusal}: one signed test event, tried once |
| **create fields** |  |  |
| key_id | required | a live API key; deliveries bill under it; revoking it pauses the webhook |
| url | required | https, port 443, public host name (no IP), no user, password or fragment, max 2,048 characters |
| addresses | required | base58, 1 to the plan's cap; program ids count; PATCH replaces the list. Refused: System, Token, Token-2022, Associated Token, ComputeBudget and Vote programs |
| name | optional | max 48 characters |
| tx_status | optional | "all" (the default), "success" or "failed" |
| auth_header | optional | max 512 visible ASCII characters, sent as each delivery's Authorization header; cleared by "" on PATCH or a new url without one |
| status | PATCH only | "paused" or "active"; "active" also re-enables a disabled webhook |

> `400`: unknown field or wrong type (named), or over 100,000 addresses on any plan. `401`: no session or valid account API key. `403`: Read only account API key on a change, or a caller address off the key's list. `404`: webhook or key missing, revoked, deleted or another account's. `409`: reason in words: Free plan, the plan's webhook or address cap, inactive account, or a test of this webhook in flight. `503`: API, sender or delivery log did not answer; retry.

### delivery

| part | kind | what |
|---|---|---|
| webhook-id | header | event id, same on every try; dedupe on it |
| webhook-timestamp | header | unix seconds; refuse if over 5 minutes off |
| webhook-signature | header | "v1," plus base64 HMAC-SHA256 of {webhook-id}.{webhook-timestamp}.{body}, keyed with the decoded base64 after whsec_; two, space-separated, for a day after rotation |
| authorization | header | your auth_header, when set |
| body | JSON array | one transaction in getTransaction's shape: slot, blockTime (null while unknown), indexWithinBlock, transaction, meta, version; as a Helius raw webhook |
| over 64 KB | short form | past 65,536 bytes: slot, blockTime, indexWithinBlock, signature, err, status, truncated: true; rest via getTransaction |

> Vote transactions are never sent. Lookup-table addresses count. Success: a 2xx within 15 s, from name lookup to the answer's first line. No redirects. Each try: a new connection with headers host, content-type: application/json, content-length and those above; no User-Agent.

### retries and switch-off

> A try with no 2xx within 15 s is retried after 5 s, 5 min, 30 min, 2 h, 5 h, 10 h and 10 h: 8 tries over about 27 hours, then dropped. At most 2 calls per webhook in flight. While every try fails, new events wait: 5 s after the first failed try, then 5 minutes between tries, until one succeeds. Events past your plan's deliveries per second, or while out of credits, queue without spending tries. Queue: 10,000 events or 64 MiB per webhook; past that, the oldest drop. At most 200 transactions per slot per webhook, the first in block order; the rest are skipped. A paused webhook keeps its queue and captures nothing new; a delete or the switch-off drops it. Delivery is at least once, unordered.

> Skipped without notice: transactions while delivery is down past its replay window (a few hundred slots), or the chain stops finalizing over 1,024 slots. Fetch them with getSignaturesForAddress and getTransaction.

### statuses

| status | status_reason | what to do |
|---|---|---|
| active |  | delivering |
| paused | null | you paused it: PATCH status "active" |
| paused | key_revoked, key_deleted, key_suspended, key_banned | PATCH a live key_id or lift the suspension; banned: contact support |
| paused | account_suspended, account_closed, account_banned | account inactive; banned: contact support |
| paused | over_plan_limit | plan lowered: newest webhooks and lists past its caps pause. Delete one, shorten the list or move up a plan |
| disabled | no success in 5 days | fix the receiver, then PATCH status "active" |

### delivery log and test event

| field | kind | what |
|---|---|---|
| ts | number | Unix milliseconds (UTC) |
| event_id | string | the webhook-id sent; evt_test_ prefix on tests; empty on a drop with unknown id |
| slot | number | transaction slot; 0 on a test |
| kind | string | "delivery" or "test" |
| attempt | number | try number, 1 to 8; on a drop, tries made, 0 to 8 |
| outcome | string | "delivered"; "failed" (a delivery retries); "dropped" (event lost); "refused" (test not sent) |
| http_status | number | your url's status; 0 if no answer |
| reason | string | failed: timeout, connect, tls, http or refused (url not called: name unresolved or not public); empty if your url answered non-2xx. dropped: tries_spent, queue_full, disabled or switched_off. refused: the cause in words |
| latency_ms | number | name lookup to first answer line; 0 on a drop, refusal or uncalled url |
| cursor | string | send the last line's as ?before= for older lines |

> `GET /v1/webhooks/{id}/deliveries`: a line appears about a second after the event; a page shorter than the limit is the last; a missing webhook, or another account's, returns an empty list. For debugging, not billing; a line can be missing.

> `POST /v1/webhooks/{id}/test`: one try, up to 15 s. Body: a one-event list with slot 0, blockTime, indexWithinBlock 0, a signature of 64 ones, err null, status {"Ok": null} and test: true. Its webhook-id starts with evt_test_; skip it before getTransaction. A delivered test bills under the webhook's key; failed or refused, free. Tests reach paused and disabled webhooks and change neither status nor consecutive_failures, so test a receiver before you switch a webhook back on. Tests count against your plan's deliveries per second. Refused before sending: 200, outcome "refused", the cause in reason and its kind in refusal: credits, rate, plan, key (revoked, suspended or unknown), secret, host or refused.

### troubleshooting

| symptom | cause | action |
|---|---|---|
| paused, with a reason | see the reason | remove the cause; resumes on its own |
| disabled | 5 days of failed tries, no 2xx; quiet or paused webhooks never disable | fix the receiver, then PATCH status "active" |
| 429 on a change | over 120 creates, edits, tests, rotations and deletes per account in 10 minutes | wait up to 10 minutes, then send it again; reads are never throttled |
| 409 on create or PATCH | the plan's webhook or address cap, named in the message | delete one, shorten the list or move up a plan |
| signature does not verify | body parsed or re-encoded before the check, another webhook's secret, or clock over 5 minutes off | verify the raw bytes with this webhook's secret |
| the same event twice | retry after a timeout or non-2xx answer | dedupe on webhook-id; answer 200 before heavy work |
| nothing arriving | no matching finalized transaction yet, tx_status skips it, or your url redirects or answers after 15 s | send a test and read its log line; then check the addresses, tx_status and the last tries |

> Not offered: parsed (enhanced) payloads, account-change webhooks, Discord delivery, batching, replay of past events. Agents find these calls in the [SKILL.md](https://carbium.sh/docs/agents.md).
