# Webhooks

Send job events to an HTTPS endpoint you control.

## Receive job updates on your server

Register a public HTTPS endpoint (in the console at https://signapi.fingerdance.ai/account/webhooks, or with `POST /v1/webhooks` and a key that has `webhooks:manage`). Save the signing secret when the endpoint is created: it is shown once.

```bash
curl "https://signapi.fingerdance.ai/v1/webhooks" -H "Authorization: Bearer $FINGERDANCE_API_KEY" \
  -H 'Content-Type: application/json' --data '{"url": "https://example.com/fingerdance/webhook"}'
```

When a job reaches a final state, SignAPI POSTs an event:

```json
{
  "id": "evt_8c1d0f6e5b9a4f2e9d7c3b1a0f6e5d4c",
  "type": "job.succeeded",
  "created_at": 1794853662.4,
  "data": {"job_id": "d23be352fef14576ac6f22dc3f5fe1cb", "status": "succeeded", "error_code": null, "billed_ms": 3567, "pipeline_version": "..."}
}
```

Event types: `job.succeeded`, `job.failed`, `job.cancelled`.

## Verify the signature

Each delivery carries `X-Fingerdance-Event-Id` and `X-Fingerdance-Signature: t=<unix timestamp>,v1=<hex signature>`. Compute the hex HMAC-SHA256 of `timestamp + "." + raw_body` with your signing secret, compare it to `v1` with a timing-safe comparison, and reject timestamps older than a few minutes. Verify against the raw request body, before parsing JSON.

```python
import hashlib, hmac, time

def verify(raw_body: bytes, header: str, secret: str, tolerance=300) -> bool:
    parts = dict(item.split("=", 1) for item in header.split(","))
    timestamp, signature = parts["t"], parts["v1"]
    if abs(time.time() - int(timestamp)) > tolerance:
        return False
    expected = hmac.new(secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)
```

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

export function verify(rawBody: Buffer, header: string, secret: string, tolerance = 300) {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=", 2)));
  if (Math.abs(Date.now() / 1000 - Number(parts.t)) > tolerance) return false;
  const expected = createHmac("sha256", secret).update(parts.t + ".").update(rawBody).digest("hex");
  return expected.length === parts.v1.length && timingSafeEqual(Buffer.from(expected), Buffer.from(parts.v1));
}
```

Respond with a 2xx status after accepting the event. Deliveries are retried, so handle each event ID once.

## Test and inspect deliveries

- `POST /v1/webhooks/{endpoint_id}/test` sends a test event.
- `GET /v1/webhooks/{endpoint_id}/deliveries` shows whether each event arrived, how many attempts were made, and when the next retry is due.
- `GET /v1/webhooks` lists endpoints; `DELETE /v1/webhooks/{endpoint_id}` removes one.
