Webhooks

Pass a webhook_url when you create a video and, once it succeeds or fails, we POST the result to that URL: the same JSON GET /api/v1/videos/{id} returns, signed with your account’s secret. It is delivered once, so keep polling as the fallback.

AI-generated video: Clouds over the peaks
Clouds over the peaks
AI-generated video: Fox in a birch forest
Fox in a birch forest
AI-generated video: Cabin in falling snow
Cabin in falling snow
AI-generated video: Butterfly takes off
Butterfly takes off

What we send

POST /hooks/magi2 HTTP/1.1
Content-Type: application/json
User-Agent: Magi2AI-Webhooks/1
Magi2-Signature: t=1764062012,v1=5d2c8f…e41a

{
  "type": "video.succeeded",
  "data": {
    "id": "3f9c2b7e-5a1d-4c8e-9b0f-2d6e8a41c7d3",
    "status": "success",
    "model": "seedance-2-fast",
    "modelName": "Seedance 2.0 Fast",
    "resolution": "480p",
    "seconds": 5,
    "credits": 95,
    "outputs": ["https://cdn.magi2ai.dev/…/video.mp4"],
    …
  }
}
  • type is video.succeeded or video.failed;
  • data is exactly what GET /api/v1/videos/{id} returns;
  • a failed video has already had its credits returned, and data.error says why.

Signature

The Magi2-Signature header looks like this:

t=<unix seconds>,v1=<hex(HMAC-SHA256(secret, "<t>.<raw body>"))>

Your signing secret (it starts with whsec_) is on Settings → API keys, one per account. To verify:

  1. parse t and v1 from the header;
  2. reject a t more than 5 minutes from now (replay protection);
  3. compute HMAC-SHA256 of t + "." + raw body with the secret and compare it to v1 in constant time;
  4. only then parse the JSON.

Use the raw body. Parsing the JSON and serializing it again changes the bytes, and the signature won’t match.

Verify in Node.js

import crypto from 'node:crypto';
import express from 'express';

const app = express();
const SECRET = process.env.MAGI2_WEBHOOK_SECRET; // whsec_…

// Keep the raw body: the signature covers the exact bytes we sent.
app.post('/hooks/magi2', express.raw({ type: 'application/json' }), (req, res) => {
  const header = req.get('Magi2-Signature') ?? '';
  const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header);
  if (!match) return res.status(400).end();
  const [, t, v1] = match;
  if (Math.abs(Date.now() / 1000 - Number(t)) > 300) return res.status(400).end();

  const expected = crypto
    .createHmac('sha256', SECRET)
    .update(`${t}.${req.body.toString('utf8')}`)
    .digest('hex');
  const ok = crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(v1));
  if (!ok) return res.status(400).end();

  const event = JSON.parse(req.body.toString('utf8'));
  // event.type: "video.succeeded" | "video.failed"; event.data: the video
  res.status(200).end();
});

Verify in Python

import hashlib, hmac, os, re, time
from flask import Flask, request, abort

app = Flask(__name__)
SECRET = os.environ["MAGI2_WEBHOOK_SECRET"].encode()  # whsec_…

@app.post("/hooks/magi2")
def magi2_hook():
    header = request.headers.get("Magi2-Signature", "")
    m = re.fullmatch(r"t=(\d+),v1=([0-9a-f]{64})", header)
    if not m or abs(time.time() - int(m[1])) > 300:
        abort(400)
    body = request.get_data()  # raw bytes, before any JSON parsing
    expected = hmac.new(SECRET, m[1].encode() + b"." + body, hashlib.sha256).hexdigest()
    if not hmac.compare_digest(expected, m[2]):
        abort(400)
    event = request.get_json()
    # event["type"]: "video.succeeded" | "video.failed"
    return "", 200

Delivery rules

  • the URL must be https, carry no username or password, and not point at localhost, an internal hostname or an IP address; 500 characters at most;
  • one attempt per video, 10-second timeout, redirects not followed;
  • any 2xx counts as delivered. We don’t retry, so queue slow work and answer 200 first;
  • if a callback never arrives, read the result with GET /api/v1/videos/{id}.

Everything else is in the API docs.

Questions

Are webhooks retried?

No. Each video is delivered once, with a 10-second timeout. If one never arrives, poll GET /api/v1/videos/{id}.

Where do I find the signing secret?

Signed in, open Settings → API keys: the whsec_ secret can be revealed and copied there.

Why does my signature check fail?

Most often the HMAC was computed over JSON that was parsed and re-serialized. Use the raw bytes you received, and check you are using this account’s secret.

Last updated: