Webhooks
OpenCanvas POSTs a file's change events to one URL of yours, so you can react without holding a stream open: index a saved file, notify on a comment. Deliveries are signed, retried, and survive restarts.
Turn them on
Webhooks are off unless the host's entry has a [host.webhooks] table:
[host.webhooks]
url = "https://host.example.com/opencanvas/hooks" # must be on your callback_base origin
events = ["saved", "commented", "mentioned"] # or ["*"]; the event types below
# secret = "env:OC_WEBHOOK_SECRET" # default: callback_secret, else jwt_secret
# max_attempts = 12 # 1-20
| Setting | Meaning |
|---|---|
url | Where to POST. It must be on the same origin (scheme, host and port) as your callback_base; anything else stops the server at start-up and is refused again before every delivery. To receive on another domain, relay from your callback origin. |
events | The event types to send, or ["*"] for all. |
secret | The signing secret. Default: the host's callback secret, else its jwt_secret. |
max_attempts | Attempts before an event is dropped, 1 to 20 (default 12, about a day). |
Webhooks need a database (SQLite, MySQL or MariaDB): with Redis-only storage the server refuses to start with [host.webhooks].
The request
One POST per event. The body is the event, the same JSON as on the events stream: ids, user ids, versions and counts, never document text.
POST /opencanvas/hooks HTTP/1.1
Content-Type: application/json
X-OpenCanvas-Event: commented
X-OpenCanvas-Event-Id: ev_01m87jv4t3ggvh8n6d6nh38xc0
Idempotency-Key: ev_01m87jv4t3ggvh8n6d6nh38xc0
X-OpenCanvas-Delivery-Attempt: 1
X-OpenCanvas-Signature: t=1795550974,v1=4c430dee70e32585ea42f8f640c4f9197fd3f8d781cbaca4294566f61d08f656,v2=d85e216af3489979b6aaadd7c54574683679cfa8e0dee639e3d229db0e650f57
{"at":1795550974787,"data":{"added":1,"blockId":"b_01m87jv4rtkdxymn93jwtx35sk","by":["u_42"],"threadId":"t_01m87jv4sqj5z6jz5q2n5zdtdw"},"fileId":"doc_1","id":"ev_01m87jv4t3ggvh8n6d6nh38xc0","type":"commented"}
| Header | Meaning |
|---|---|
X-OpenCanvas-Event | The event type. |
X-OpenCanvas-Event-Id | The event id (ev_<ulid>). |
Idempotency-Key | The event id again: deduplicate on it. |
X-OpenCanvas-Delivery-Attempt | 1 for the first attempt, then 2, 3, ... |
X-OpenCanvas-Signature | t=<unix seconds>,v1=<hex>,v2=<hex>. Verify v2. |
No bearer token is sent; the signature is the credential.
Payloads
type | Example data |
|---|---|
edited | {"blockIds": ["b_01m87jv4rtkdxymn93jwtx35sk"], "removedBlockIds": [], "databaseIds": ["db_01m87jv4sc2twxjhzzxzh7mene"], "by": ["u_42"]} |
commented | {"threadId": "t_01m87jv4sqj5z6jz5q2n5zdtdw", "blockId": "b_01m87jv4rtkdxymn93jwtx35sk", "added": 1, "by": ["u_42"]} |
mentioned | {"mentioned": ["u_7"], "by": "u_42", "blockId": "b_01m87jv4rtkdxymn93jwtx35sk"} |
member.joined | {"userId": "u_42", "role": "edit"} |
member.left | {"userId": "u_42"} |
saved | {"version": "18"} |
restored | {"restoreId": "01m87k0a2b3c4d5e6f7g8h9j0k", "from": "17", "by": "u_42"} |
session.closed | {"reason": "host"} or {"reason": "idle"} |
resync is never sent to webhooks; it only exists on streams.
Verify the signature
v2 is HMAC-SHA256 of "<t>.POST.<path?query>.<body>" with your secret, in lower-case hex:
- Read the raw body bytes before any JSON parsing; a re-serialised body does not match.
- Use the path and query as your server received them (behind a proxy that rewrites paths, the path OpenCanvas sent to).
- Compare in constant time, and reject a
tmore than 5 minutes from your clock.tis the time of the attempt, so each retry has a new signature.
v1 (timestamp and body only) is kept for old callback verifiers; do not use it for webhooks.
# A sketch of the check: compare the hex in constant time in real code.
secret='whsec_test_example'
signature='t=1795550974,v1=4c430dee70e32585ea42f8f640c4f9197fd3f8d781cbaca4294566f61d08f656,v2=d85e216af3489979b6aaadd7c54574683679cfa8e0dee639e3d229db0e650f57'
path_query='/opencanvas/hooks'
body='{"at":1795550974787,"data":{"added":1,"blockId":"b_01m87jv4rtkdxymn93jwtx35sk","by":["u_42"],"threadId":"t_01m87jv4sqj5z6jz5q2n5zdtdw"},"fileId":"doc_1","id":"ev_01m87jv4t3ggvh8n6d6nh38xc0","type":"commented"}'
t=$(printf '%s' "$signature" | tr ',' '\n' | sed -n 's/^t=//p')
v2=$(printf '%s' "$signature" | tr ',' '\n' | sed -n 's/^v2=//p')
expected=$(printf '%s.POST.%s.%s' "$t" "$path_query" "$body" \
| openssl dgst -sha256 -hmac "$secret" -hex | sed 's/^.*= //')
[ "$expected" = "$v2" ] && echo "signature ok" || echo "signature WRONG"
# Then reject it if t is more than 300 seconds from now:
# [ $(( $(date +%s) - t )) -le 300 ] && [ $(( t - $(date +%s) )) -le 300 ]import { verifyWebhook } from "@opencanvas/client";
// In your receiver: the raw body (before JSON parsing) and the path and query as received.
const ok = await verifyWebhook({
secret: process.env.OC_WEBHOOK_SECRET!, // "whsec_test_example" for the test vector
signature: req.headers["x-opencanvas-signature"] as string,
body: rawBody, // string or Uint8Array, exactly as received
pathQuery: req.originalUrl, // "/opencanvas/hooks"
// nowSecs: 1795550974, // pin the clock to check the test vector
});
if (!ok) return res.status(401).end();
res.status(204).end(); // answer fast, then do the work
const event = JSON.parse(rawBody);use opencanvas_client::{verify_webhook, WebhookRequest};
let ok = verify_webhook(
secret.as_bytes(), // b"whsec_test_example" for the test vector
WebhookRequest {
signature: &signature_header, // X-OpenCanvas-Signature
method: "POST",
path_query: &path_and_query, // "/opencanvas/hooks"
body: &raw_body, // the bytes as received
},
now_secs, // 1795550974 for the test vector
);
if !ok {
return StatusCode::UNAUTHORIZED;
}import hashlib
import hmac
import time
def verify_webhook(secret: bytes, signature: str, path_query: str, body: bytes,
now=None, tolerance=300) -> bool:
parts = {}
for item in signature.split(","):
k, _, v = item.strip().partition("=")
parts.setdefault(k, v)
try:
t = int(parts["t"])
v2 = parts["v2"]
except (KeyError, ValueError):
return False
if abs((time.time() if now is None else now) - t) > tolerance:
return False
mac = hmac.new(secret, f"{t}.POST.{path_query}.".encode() + body, hashlib.sha256).hexdigest()
return hmac.compare_digest(mac, v2)
# The test vector:
assert verify_webhook(
b"whsec_test_example",
"t=1795550974,v1=4c430dee70e32585ea42f8f640c4f9197fd3f8d781cbaca4294566f61d08f656,v2=d85e216af3489979b6aaadd7c54574683679cfa8e0dee639e3d229db0e650f57",
"/opencanvas/hooks",
"{\"at\":1795550974787,\"data\":{\"added\":1,\"blockId\":\"b_01m87jv4rtkdxymn93jwtx35sk\",\"by\":[\"u_42\"],\"threadId\":\"t_01m87jv4sqj5z6jz5q2n5zdtdw\"},\"fileId\":\"doc_1\",\"id\":\"ev_01m87jv4t3ggvh8n6d6nh38xc0\",\"type\":\"commented\"}".encode(),
now=1795550974,
)Test vector
Your verifier must accept this delivery (with the clock at t) and refuse it when any byte of the body, the path or the signature changes:
| Input | Value |
|---|---|
| Secret | whsec_test_example |
Time (Unix seconds, also t) | 1795550974 |
| Path and query | /opencanvas/hooks |
| Body (exact bytes) | {"at":1795550974787,"data":{"added":1,"blockId":"b_01m87jv4rtkdxymn93jwtx35sk","by":["u_42"],"threadId":"t_01m87jv4sqj5z6jz5q2n5zdtdw"},"fileId":"doc_1","id":"ev_01m87jv4t3ggvh8n6d6nh38xc0","type":"commented"} |
X-OpenCanvas-Signature | t=1795550974,v1=4c430dee70e32585ea42f8f640c4f9197fd3f8d781cbaca4294566f61d08f656,v2=d85e216af3489979b6aaadd7c54574683679cfa8e0dee639e3d229db0e650f57 |
Answer and retries
Answer any 2xx quickly (10-second timeout) and do the work afterwards.
| Your answer | What OpenCanvas does |
|---|---|
2xx | Delivered. |
Network error, timeout, 408, 425, 429, 5xx | Retried after 10 s, 30 s, 1 min, 5 min, 15 min, 30 min, 1 h, 2 h, 4 h, 8 h, then every 12 h, until max_attempts; then dropped. A longer Retry-After (seconds, up to 12 h) wins. |
Any other 3xx or 4xx | Dropped at once. Redirects are never followed. |
- At least once. An event can arrive twice: deduplicate on
Idempotency-Key. - Order is not guaranteed across retries: order a file's events by
atand the id (a ULID). - Durable. Events are written to a database outbox (
webhook_outbox) by the server that runs the file's live room before they are sent, so they survive restarts, and exactly one server of a cluster sends each. An event can be lost only if that server dies between the change and the outbox write.
Metrics
oc_webhook_deliveries_total{host,result} (ok, retry, rejected, gave_up, refused) and oc_webhook_delivery_seconds{host} (one attempt's time), on the operations dashboard's "Webhooks" row.
Last changed .