> ## Documentation Index
> Fetch the complete documentation index at: https://docs.omniakey.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Webhooks

> Get a signed POST when a media task finishes instead of polling, and verify it with your signing secret.

Add `callback_url` when you create a media task. When the task finishes,
OmniaKey sends one signed `POST` with the finished task to that URL. Polling
`GET /v1/media/tasks/{id}` keeps working, so you can use both.

## Add a callback URL

Put `callback_url` at the top level of the create body:

```bash theme={null}
curl --fail-with-body https://api.omniakey.com/v1/media/tasks \
  -H "Authorization: Bearer your-omniakey-api-key" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: boat-with-callback-001" \
  -d '{
    "model": "gpt-image-2.6-flare",
    "type": "image",
    "operation": "generate",
    "input": {"prompt": "a paper boat on a quiet lake"},
    "parameters": {"size": "1024x1024", "quality": "medium"},
    "callback_url": "https://example.com/webhooks/omniakey"
  }'
```

The URL must:

* start with `https://`. Any port works.
* point to a public host. Private and loopback addresses, `localhost` and
  names such as `*.local` or `*.internal` are refused with
  `400 invalid_request` before anything is charged.
* be at most 2,048 characters, without a username or password.

`callback_url` is part of the idempotency check: the same `Idempotency-Key`
with a different `callback_url` returns `409 idempotency_key_reused`.

## Delivery state

For a task created with `callback_url`, the create response and
`GET /v1/media/tasks/{id}` include a `callback` object:

```json theme={null}
"callback": {
  "url": "https://example.com/webhooks/omniakey",
  "status": "delivered",
  "attempts": 1,
  "event_id": "msg_2c1f0e9a7b4d4a6c8e3b5d7f9a1c3e5b",
  "event_type": "media_task.succeeded",
  "last_attempt_at": "2026-11-18T09:12:41Z",
  "last_status_code": 200,
  "delivered_at": "2026-11-18T09:12:41Z"
}
```

| `status` | Meaning |
| - | - |
| `waiting` | The task hasn't finished yet |
| `pending` | An attempt is due at `next_attempt_at` |
| `delivered` | Your endpoint answered with a `2xx` status |
| `failed` | Every attempt failed. `last_error` says what happened last |

The task's details in the [media library](https://omniakey.com/dashboard/media)
show the same state. A delivered or failed event can be sent again there with
**Send again**, and it keeps its `webhook-id`.

## The event

```http theme={null}
POST /webhooks/omniakey HTTP/1.1
Content-Type: application/json
User-Agent: OmniaKey-Webhooks/1.0
webhook-id: msg_2c1f0e9a7b4d4a6c8e3b5d7f9a1c3e5b
webhook-timestamp: 1763457161
webhook-signature: v1,<base64 HMAC-SHA256 signature>

{"type":"media_task.succeeded","timestamp":"2026-11-18T09:12:40Z","data":{...}}
```

`data` is the object `GET /v1/media/tasks/{id}` returns, so a succeeded task
carries `assets[]` with freshly signed URLs. `timestamp` is when the event was
created and stays the same on every attempt.

| `type` | Sent when |
| - | - |
| `media_task.succeeded` | The task finished. Download `data.assets[]` before `expires_at` |
| `media_task.failed` | The task failed or expired. `data.error` says why |
| `media_task.canceled` | The task was canceled |
| `webhook.test` | You sent a test event from the dashboard |

OmniaKey sends the event after the task's billing is final, so a failed or
canceled task has normally been refunded by then. Most events leave within a
few seconds of the task finishing.

## Verify the signature

Events follow the [Standard Webhooks](https://www.standardwebhooks.com/) spec,
so its libraries verify them. Find your signing secret (`whsec_...`) under
**Task callbacks** in [Settings](https://omniakey.com/dashboard/settings#webhooks).
Each account has one secret, and an API key can't read it.

```python Python theme={null}
# pip install standardwebhooks flask
from flask import Flask, request
from standardwebhooks.webhooks import Webhook

app = Flask(__name__)
webhook = Webhook("whsec_...")


@app.post("/webhooks/omniakey")
def omniakey_webhook():
    # Raises WebhookVerificationError for a bad signature or an old timestamp.
    event = webhook.verify(request.get_data(), dict(request.headers))
    if event["type"] == "media_task.succeeded":
        save_assets(event["data"]["assets"])
    return "", 204
```

```javascript Node.js theme={null}
// npm install standardwebhooks express
import express from "express";
import { Webhook } from "standardwebhooks";

const app = express();
const webhook = new Webhook("whsec_...");

app.post("/webhooks/omniakey", express.raw({ type: "application/json" }), (req, res) => {
  let event;
  try {
    event = webhook.verify(req.body, req.headers);
  } catch {
    return res.status(400).end();
  }
  if (event.type === "media_task.succeeded") saveAssets(event.data.assets);
  res.status(204).end();
});
```

### Without a library

1. Base64-decode the part of the secret after `whsec_`. That is the HMAC key.
2. Build `{webhook-id}.{webhook-timestamp}.{raw body}`.
3. Compute its HMAC-SHA256 with that key.
4. Compare the result, in constant time, with each `v1,<base64>` entry in
   `webhook-signature`. Entries are separated by spaces.
5. Reject a `webhook-timestamp` more than 5 minutes away from your clock.

```javascript theme={null}
import crypto from "node:crypto";

export function verifyOmniaKeyWebhook(secret, headers, rawBody) {
  const id = headers["webhook-id"];
  const timestamp = headers["webhook-timestamp"];
  if (!id || !timestamp || Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false;
  const key = Buffer.from(secret.slice("whsec_".length), "base64");
  const expected = crypto.createHmac("sha256", key).update(`${id}.${timestamp}.${rawBody}`).digest();
  return String(headers["webhook-signature"] ?? "")
    .split(" ")
    .some((entry) => {
      const [version, signature] = entry.split(",");
      if (version !== "v1" || !signature) return false;
      const given = Buffer.from(signature, "base64");
      return given.length === expected.length && crypto.timingSafeEqual(given, expected);
    });
}
```

> [!WARNING]
>
> Verify the raw request body before you parse it. Parsing the JSON and
> serializing it again changes the bytes, and the signature no longer matches.

After you rotate the secret in Settings, every event carries two signatures
for 24 hours, one from the new secret and one from the old. Switch your server
to the new secret within that time and no event is lost.

## Retries

| Your endpoint | What OmniaKey does |
| - | - |
| Answers `2xx` within 15 seconds | Marks the event delivered |
| Answers `410 Gone` | Stops sending this event |
| Anything else: another status, a redirect, no answer within 15 seconds, a TLS or connection error | Tries again |

After the first attempt, retries follow at 5 seconds, 5 minutes, 30 minutes,
2 hours, 5 hours, 10 hours and 10 hours: 8 attempts over about 27.6 hours.
After the last one the callback is `failed`.

* Answer quickly and do slow work afterwards: an answer after 15 seconds
  counts as a failure.
* Redirects aren't followed, so use the final URL.
* Delivery is at least once. Every attempt of an event carries the same
  `webhook-id`, so skip an event whose `webhook-id` you've already handled.
* At most two attempts run at once per receiving host.
* When you retry a failed task from the dashboard, its new run sends one more
  event with a new `webhook-id`.

## Test your endpoint

Under **Task callbacks** in [Settings](https://omniakey.com/dashboard/settings#webhooks),
enter your URL and choose **Send test**. OmniaKey sends a signed `webhook.test`
event right away and shows your endpoint's status code and response time. No
media task is needed, so you can check your receiver before your first task.

## Network

* Requests come straight from OmniaKey's servers, not through a proxy.
  OmniaKey doesn't publish a fixed list of source IP addresses, so check the
  signature instead of allowlisting IPs.
* Your host name is resolved again before every attempt. An attempt to a name
  that resolves to a private or loopback address is refused, and the event is
  not tried again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.