Webhooks

Get called when a task finishes, and verify that the call is from us.

Instead of polling, add a callback_url when you create a task. When the task reaches success or fail, we send a POST to it.

The callback URL

  • It must be a public https URL. Plain http, IP addresses and private hostnames are rejected when you create the task.
  • We resolve the hostname again when we deliver, and refuse addresses that point inside a private network. We do not follow redirects.

What we send

{
  "event": "task.succeeded",
  "data": {
    "id": "task_...",
    "status": "success",
    "cost": "0.025",
    "output": { "urls": ["https://cdn.nsfwapi.io/results/..."], "expires_at": "2026-12-15T00:00:00Z" }
  }
}

event is task.succeeded or task.failed. data is the same task object you get from GET /tasks/{id}.

Headers:

HeaderValue
X-Webhook-TimestampUnix time in seconds when we sent it.
X-Webhook-Signaturev1= followed by a hex HMAC-SHA256 signature.

Verify the signature

Find your signing secret in the console under Webhooks. It is one secret for your whole account.

The signature is HMAC-SHA256(secret, timestamp + "." + rawBody) as lowercase hex, prefixed with v1=. Use the raw request body exactly as received, before parsing it as JSON.

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

export function verify(rawBody, headers, secret) {
  const ts = headers["x-webhook-timestamp"];
  if (Math.abs(Date.now() / 1000 - Number(ts)) > 300) return false; // older than 5 minutes
  const expected = "v1=" + createHmac("sha256", secret).update(`${ts}.${rawBody}`).digest("hex");
  const got = headers["x-webhook-signature"] ?? "";
  return got.length === expected.length && timingSafeEqual(Buffer.from(got), Buffer.from(expected));
}
import hashlib, hmac, time

def verify(raw_body: bytes, headers: dict, secret: str) -> bool:
    ts = headers.get("x-webhook-timestamp", "")
    if not ts.isdigit() or abs(time.time() - int(ts)) > 300:  # older than 5 minutes
        return False
    signed = ts.encode() + b"." + raw_body
    expected = "v1=" + hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, headers.get("x-webhook-signature", ""))

Reject requests whose timestamp is more than five minutes old. That stops someone replaying an old callback.

Replying and retries

Answer with any 2xx status to say you got it. Anything else, or no answer, counts as a failure, and we retry after 1 minute, 5 minutes, 30 minutes and 2 hours, then stop. Every attempt is listed in the console under Webhooks.

Your handler may be called more than once for the same task, so make it safe to repeat.

A callback is a hint, not the truth

Treat a callback as a signal that something changed. If it matters, confirm with GET /tasks/{id} before you act. If you miss a callback, the task is still there to fetch.

Rotating the secret

Rotate it in the console if it leaks. The old secret stops working immediately, so update your server first or callbacks sent in the meantime will fail verification.