graphlife Docs graphlife.app

Integration API

Webhooks

A webhook is the change feed, POSTed to a URL of yours as changes happen. It carries exactly what its registrar could read from the feed at the moment of sending.

Webhooks need the server to be configured with a secret key for sealing stored secrets; without it every route below answers 503 webhooks_not_configured.

Route Body and response
POST/v1/graphs/{id}/webhooks {"url", "events"?: [types], "label"?} answers 201 {"webhookId", "secret", "url", "events", "status"}. The secret is shown this once
GET/v1/graphs/{id}/webhooks {"webhooks": [{"webhookId", "url", "label", "events", "status", "disabledReason", "failures", "lastStatus", "lastDeliveryAt", "createdAt"}]}. Never the secret. Fields with no value are null
PATCH/v1/graphs/{id}/webhooks/{webhookId} {"enabled": true | false} answers the webhook as the list shows it
DELETE/v1/graphs/{id}/webhooks/{webhookId} {"deleted": true}
POST/v1/graphs/{id}/webhooks/{webhookId}/test sends one signed {"type": "ping"} now; answers {"delivered": true | false, "status"?, "error"?}. error is one of url_not_allowed, timeout, connect_failed, tls_failed. Five a minute
  • Who. The owner of a personal graph, an owner or admin of an organisation, or a key holding webhooks. A member's session answers 403 forbidden. A session sees and manages every webhook on the graph. A key sees and manages only the ones it registered; any other id is 404.
  • events is a list of event types, or ["*"] (the default). A reset is sent whatever the list says.
  • A graph may have 10 webhooks by default (409 too_many_webhooks).
  • A webhook starts at the current position: nothing from before it was registered is sent.
  • Moving a graph into an organisation deletes its webhooks, and so does unpublishing it.

A delivery#

POST <your url>
content-type: application/json
user-agent: GraphLife-Webhook/1
x-graphlife-webhook-id: <webhook id>
x-graphlife-delivery: <delivery id>
x-graphlife-timestamp: 1790000000
x-graphlife-signature: v1=<hex>
{"webhookId": "...", "graphId": "...", "deliveryId": "...",
 "sentAt": 1790000000,
 "events": [{"seq": 41, "type": "node.updated", "id": "...", "revision": 17,
             "at": 1790000000, "via": "key", "own": false}],
 "next": "s1.57.ab12cd34ef56", "reset": false}

Each delivery is one page of the feed, up to 100 events, with the same event objects. Follow the same rules as the feed: upsert on node.created and node.updated, list a new node's edges, list again on reset: true.

  • Answer with a 2xx within the timeout (10 seconds by default). Acknowledge first and process afterwards: deliveries of all webhooks on a server are sent one after another.
  • 410 disables the webhook at once (disabledReason: "gone").
  • Anything else is a failure: a redirect (never followed), an error status, no answer in time, an address that may not be called. The same position is sent again after 30 seconds, then 60, doubling up to an hour.
  • After 80 failures in a row (about three days) the webhook is disabled with disabledReason: "delivery_failed". PATCH {"enabled": true} switches it on again and it resumes from where it stopped, or with reset: true if the change log has been pruned past that point.
  • deliveryId is new on every attempt, and a retried batch is read again when it is sent, so it may hold different events. Tell events apart by (graphId, seq), never by deliveryId.
  • Before every delivery the server checks that the registrar could still register this webhook. A revoked or expired key, a key that lost the webhooks tool, an admin demoted or removed: the webhook is disabled with disabledReason: "no_access". A change to the registrar's groups arrives as reset: true.
  • Switching a webhook off by hand sets disabledReason: "manual".

Verifying the signature#

The signature is HMAC-SHA256, in lowercase hex.

  • Key: the whole secret string as UTF-8, whsec_ prefix included. Nothing is base64-decoded.
  • Message: the value of x-graphlife-timestamp, then a ., then the exact bytes of the request body.
  • Header: x-graphlife-signature: v1=<hex>.

Compare in constant time, and refuse a timestamp more than 300 seconds from your clock. Verify against the raw body bytes, before any JSON parsing.

Python:

import hmac, time

def verify(secret: str, timestamp: str, body: bytes, signature: str) -> bool:
    want = "v1=" + hmac.new(secret.encode(), f"{timestamp}.".encode() + body,
                            "sha256").hexdigest()
    fresh = abs(time.time() - int(timestamp)) <= 300
    return fresh and hmac.compare_digest(want, signature)

# verify(SECRET, request.headers["x-graphlife-timestamp"],
#        request.get_data(), request.headers["x-graphlife-signature"])

Node:

import { createHmac, timingSafeEqual } from "node:crypto";

function verify(secret, timestamp, body /* Buffer */, signature) {
  const want = "v1=" + createHmac("sha256", secret)
    .update(timestamp + ".").update(body).digest("hex");
  const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) <= 300;
  return fresh && want.length === signature.length &&
    timingSafeEqual(Buffer.from(want), Buffer.from(signature));
}

// verify(SECRET, req.headers["x-graphlife-timestamp"], rawBody,
//        req.headers["x-graphlife-signature"])

Where a webhook may be sent#

  • https only (unless the server sets WEBHOOK_ALLOW_HTTP=true), with no user name or password in the URL.
  • The host name is resolved when the webhook is registered and again before every delivery. Every address it resolves to must be allowed, and the connection is made to the address that was checked. A proxy configured on the server is not used, and redirects are not followed.
  • Link-local and cloud-metadata addresses, multicast and reserved ranges are never called.
  • Loopback and private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 127.0.0.0/8, fc00::/7, ::1 and the like) are refused unless the server sets WEBHOOK_ALLOW_PRIVATE=true. A self-hosted server whose receiver is on the same network will want that.
  • A refused address, a name that does not resolve and a malformed URL all answer 400 url_not_allowed.