Webhooks
Since 3.0 the server calls URLs for the document events of collections: an HTTP POST per event, signed, retried, logged.
Managing webhooks
Administrators manage them over the API (or in code, below):
POST /system/webhooks
{ "url": "https://hooks.example.org/orders", "events": ["document:create", "document:update"], "collections": ["orders"], "includeDocuments": true }The answer has the secret of the signatures — only this once. GET /system/webhooks lists them (stored and declared, without secrets), PATCH /system/webhooks/{id} changes url, events, collections, active, includeDocuments, DELETE removes one, GET /system/webhooks/{id}/deliveries shows the latest deliveries (7 days).
| Field | |
|---|---|
events | document:create, document:update, document:delete, document:restore, or * |
collections | as in the paths (orders, otherdb:orders); all collections when absent or empty ([]) |
includeDocuments | the current documents in the body, without secret fields, fields with read rules and draft working copies; default ids only |
In code (not changeable over the API):
import { webhooks } from '@sfxcode/mongocamp-server-core'
await server.use(webhooks([{ id: 'erp', url: process.env.ERP_HOOK!, events: ['document:create'], collections: ['orders'], secret: process.env.ERP_SECRET!, active: true }]))The request
POST /orders HTTP/1.1
content-type: application/json
x-mongocamp-event: document:create
x-mongocamp-delivery: 6756c0… (the id of the job: the same on a retry)
x-mongocamp-signature: t=1733300000,v1=5f2b…
{ "id": "6756c0…", "event": "document:create", "collection": "orders", "documentIds": [{ "$oid": "…" }], "at": "2026-12-04T…", "documents": [ … ] }v1 is the HMAC-SHA256 (hex) of <t>.<body> with the secret. Check it, and the age of t, before trusting a call:
import { verifyWebhookSignature } from '@sfxcode/mongocamp-server-core'
const ok = verifyWebhookSignature(secret, request.headers.get('x-mongocamp-signature') ?? '', await request.text()) // 5 minutes toleranceDelivery
- Every event is a job of the queue: a delivery that does not answer
2xxwithinWEBHOOKS_TIMEOUT(10 s) is retried (5 times, from 2 s, doubling). The id of the delivery stays the same, so a receiver can drop duplicates. - After
WEBHOOKS_DISABLE_AFTER(10) failed deliveries in a row a stored webhook is switched off (active: false,disabledAt); switching it on again resets the count. - Collections without a webhook cost nothing: the events of other collections are not even prepared.
Internal addresses (SSRF)
A webhook calls only http and https URLs without credentials, follows no redirect, and refuses addresses inside your network: private ranges, loopback, link-local (cloud metadata), unique local and others — the check is part of the connection's own DNS lookup, so a name that resolves to another address later cannot get around it. WEBHOOKS_ALLOW_PRIVATE=true allows them (development, internal receivers you trust).
