Webhooks

Let Luce call your server when a job ends, signed so you can trust it.

Add an endpoint in Settings → API → Webhooks. When a job ends we send a POST with the job in data: event job.completed or job.failed (cancelled jobs send job.failed with error code cancelled). Events go out within about a minute of the job ending.

# Luce sends:
POST /luce-webhook HTTP/1.1
Content-Type: application/json
Luce-Signature: t=1795576211,v1=5f2b…c9e1

{"id":"evt_api7QmVZk2rT9wXs1bYc_w1","object":"event","type":"job.completed","created_at":"…","data":{ …the job… }}

Check the signature

Each endpoint has a signing secret (whsec_…), shown once when you add it. The Luce-Signature header carries a timestamp t and v1, an HMAC-SHA256 of `${t}.${raw body}` with that secret. Recompute it over the raw body, compare in constant time, and refuse timestamps more than 5 minutes old.

import crypto from "node:crypto";

// Use the raw request body, exactly as received (before JSON.parse).
function verifyLuceSignature(rawBody, header, secret) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  const t = Number(parts.t);
  if (!t || Math.abs(Date.now() / 1000 - t) > 300) return false;
  const expected = crypto.createHmac("sha256", secret).update(`${t}.${rawBody}`).digest("hex");
  return crypto.timingSafeEqual(Buffer.from(expected, "hex"), Buffer.from(parts.v1 ?? "", "hex"));
}

app.post("/luce-webhook", express.raw({ type: "application/json" }), (req, res) => {
  if (!verifyLuceSignature(req.body.toString("utf8"), req.get("Luce-Signature") ?? "", process.env.LUCE_WEBHOOK_SECRET)) {
    return res.status(400).end();
  }
  const event = JSON.parse(req.body);
  if (event.type === "job.completed") saveImage(event.data.output.image_url);
  res.status(200).end(); // answer fast; do slow work afterwards
});

Answer fast, expect repeats

  • Reply with any 2xx within 10 seconds, then do slow work. Anything else counts as a failure.
  • We retry failures 6 times: after 1 min, 5 min, 30 min, 2 h, 8 h, 13 h. Then we give up, and Settings shows it.
  • The same event can arrive twice. Use the event id (or the job id) to ignore repeats.
  • Use “Send test” in Settings to send a signed ping event to your endpoint.
Webhooks — Luce API