Content Delivery · Webhooks
Implementing the handler
An event can arrive twice. Your handler verifies it, stores it once and answers within 10 seconds.
Steps
- 1Read the raw body.
- 2Verify
X-Spectatr-Signature. On failure return 401 and store nothing. - 3Parse the JSON.
- 4Store it, skipping a body you already have.
- 5Return 200.
- 6Process it afterwards: upsert by
id, fetch files, update your systems.
Example
const express = require("express");
const crypto = require("crypto");
const SECRET = process.env.SPECTATR_WEBHOOK_SECRET;
const app = express();
app.post("/hooks/spectatr", express.raw({ type: "application/json", limit: "5mb" }), async (req, res) => {
if (!verify(SECRET, req.body, req.get("X-Spectatr-Signature"))) {
return res.status(401).end();
}
const event = JSON.parse(req.body.toString("utf8"));
const bodyHash = crypto.createHash("sha256").update(req.body).digest("hex");
await saveIfNew(bodyHash, event); // unique index on bodyHash
res.status(200).end();
queueForProcessing(event); // slow work runs after the response
});Processing
- clip.created
- Create the clip, keyed by
id. - clip.updated
- Replace its name, start and end.
- clip.deleted
- Remove the clip and its files.
- render.available
- Upsert the file, keyed by
aspectRatio,language,overlayandreplay. - metadata.updated
- Replace the stored metadata.
- Anything else
- Ignore events and fields you do not use. Never reject them.