Webhooks
A signed request to your backend whenever something happens to a video or a live stream.
import { verifyWebhook } from "crumbcast"; // A Next.js route handler; with Express, use express.raw()export async function POST(request) { // Throws unless signed with your secret, in the last 5 min const event = await verifyWebhook( await request.text(), request.headers.get("Crumbcast-Signature"), process.env.CRUMBCAST_WEBHOOK_SECRET, ); if (event.type === "video.ready") { await publish(event.data.video.id); } return new Response(null, { status: 204 });}import os from crumbcast import verify_webhook # A FastAPI route; with Flask, request.get_data()@app.post("/crumbcast")async def crumbcast_webhook(request: Request): # Raises unless signed with your secret, in the last 5 min event = verify_webhook( await request.body(), request.headers.get("Crumbcast-Signature"), os.environ["CRUMBCAST_WEBHOOK_SECRET"], ) if event["type"] == "video.ready": publish(event["data"]["video"]["id"]) return Response(status_code=204)use Crumbcast\Webhook;use Crumbcast\WebhookVerificationException; // A Laravel route; in plain PHP, file_get_contents('php://input')Route::post('/crumbcast', function (Request $request) { // Throws unless signed with your secret, in the last 5 min try { $event = Webhook::verify( $request->getContent(), $request->header('Crumbcast-Signature'), config('services.crumbcast.webhook_secret'), ); } catch (WebhookVerificationException) { abort(400); } if ($event['type'] === 'video.ready') { Publish::dispatch($event['data']['video']['id']); } return response()->noContent();});import crypto from "node:crypto"; // Crumbcast-Signature: t=<timestamp>,v1=<signature>const verify = (header, body, secret) => { const { t, v1 } = Object.fromEntries( header.split(",").map((part) => part.split("=")), ); const expected = crypto .createHmac("sha256", secret) .update(`${t}.${body}`) .digest("hex"); return v1.length === expected.length && crypto.timingSafeEqual( Buffer.from(v1), Buffer.from(expected), );};What you get
- Signed with your secretA Crumbcast-Signature header with a timestamp and an HMAC-SHA256 of the body.
- Retried until it landsA delivery that fails is tried again, further apart each time, up to 7 times over about 42 hours.
- Only the events you wantEvery event, or just the ones you pick, for each webhook.
- Videos and live streamsA video created, playable, encoded, failed, trashed, restored or deleted, and a live stream going live, dropping and ending.