Webhooks
Webhooks send an HTTP POST request to the address you choose when something happens in Defthand (an inspection was published, a
corrective action became overdue…). That way you do not need to poll the API often.
Setting up a subscription
Section titled “Setting up a subscription”Subscriptions are managed with the webhook API. These endpoints are called not with an API key but with the session token of an administrator who has the webhook permissions, and they need the Integrations module to be on for your organization. If you cannot set up the subscription yourself, talk to the Defthand team.
| Method and address | What it does |
|---|---|
POST /api/v1/webhooks/subscriptions |
Creates a subscription. |
GET /api/v1/webhooks/subscriptions |
Lists subscriptions. |
DELETE /api/v1/webhooks/subscriptions/{id} |
Deletes a subscription. |
POST /api/v1/webhooks/subscriptions/{id}/test |
Sends a test delivery. |
GET /api/v1/webhooks/subscriptions/{id}/deliveries |
Delivery records. |
Subscription body:
{ "url": "https://erp.ornek.com.tr/defthand/webhook", "events": ["InspectionPublished", "ActionOverdue"], "secret": "a-long-and-random-secret-value"}| Field | Rule |
|---|---|
url |
A full http or https address. Private network, loopback, link-local and cloud metadata addresses are refused. Use HTTPS. |
events |
At least one event name, or "*" for all public events. Event names: Webhook events. |
secret |
The secret value for the signature. If you give one, every delivery is signed; giving one is strongly recommended. The secret is stored encrypted and never shown again. |
Validation errors are in English; for example an event not in the list returns
'X' is not a public webhook event. Use "*" or one of: …. An event of a module switched off for your organization is refused with the code
Webhooks.EventModuleNotEnabled.
Delivery
Section titled “Delivery”Each delivery is a POST request; the body is the event’s JSON (Content-Type: application/json). Headers:
| Header | Value |
|---|---|
X-Webhook-Event |
The event’s name, for example InspectionPublished. |
X-Webhook-Delivery-Id |
The id of this delivery attempt. |
X-Webhook-Signature |
sha256=<hexadecimal HMAC-SHA256> (if the subscription has a secret). |
The envelope fields of every body: id (the event id; it stays the same on retries), occurredOnUtc, tenantId, correlationId,
source. Bodies contain no people’s names, e-mails, titles, comments or free text; only ids, codes, numbers and times.
Verifying the signature
Section titled “Verifying the signature”The signature is the HMAC-SHA256 of the request’s raw body (its UTF-8 bytes) with the subscription’s secret, written as lower-case
hexadecimal and prefixed with sha256=. Verify it before parsing the body as JSON, and use a constant-time comparison.
import hmac, hashlib
def verify(raw_body: bytes, signature_header: str, secret: str) -> bool: expected = "sha256=" + hmac.new(secret.encode("utf-8"), raw_body, hashlib.sha256).hexdigest() return hmac.compare_digest(expected, signature_header or "")import crypto from "node:crypto";
function verify(rawBody, signatureHeader, secret) { const expected = "sha256=" + crypto.createHmac("sha256", secret).update(rawBody).digest("hex"); const a = Buffer.from(expected), b = Buffer.from(signatureHeader ?? ""); return a.length === b.length && crypto.timingSafeEqual(a, b);}Response and retries
Section titled “Response and retries”- Return
2xx; the delivery counts as successful. Do the work in the background and answer quickly. - If you return
408,429or5xx, or the connection fails, the delivery is retried: after about 30 seconds, 2 minutes, 10 minutes and 1 hour (at most 5 attempts including the first). - Other
4xxresponses count as permanent errors and are not retried. - Every attempt is kept as a delivery record: event, HTTP status, success, attempt number, time and error message.
De-duplication
Section titled “De-duplication”The same event may arrive more than once (for example if your response timed out). The id in the body is the event’s id and does not
change on retries: store the ids you processed and skip repeats. X-Webhook-Delivery-Id, on the other hand, is different on every
attempt.
If a module is switched off
Section titled “If a module is switched off”If the Integrations module or the event’s module is switched off for your organization, no new events are sent; subscriptions are not deleted and deliveries already queued are completed.