# Webhook

> Draft

Set `webhook_url` when you submit a rewrite. When the job ends (`completed`, `failed` or `cancelled`), we send one `POST` to that URL.

> **Note:** Opens together with rewriting.

## Payload

`data` matches the [Job status](https://paperpurr.com/en/docs/jobs) response:

```json
{
  "type": "job.completed",
  "created": 1764583200,
  "data": {
    "job_id": "job_7Qm2xK",
    "status": "completed",
    "result": {
      "drafts": [
        "…"
      ]
    },
    "usage": {
      "input_words": 11,
      "charged_words": 11,
      "remaining_words": 18188
    },
    "expires_at": "2026-12-02T10:00:00Z"
  }
}
```

| type | When |
| --- | --- |
| `job.completed` | The job finished |
| `job.failed` | The job failed |
| `job.cancelled` | The job was cancelled |

## Verifying signatures

Each request carries a `PaperPurr-Signature` header in the form `t=<timestamp>,v1=<signature>`. The signature is the hex HMAC-SHA256 of “timestamp + `.` + raw body”, keyed with your webhook signing secret. Drop requests whose timestamp is more than 5 minutes from now.

Python:

```python
import hashlib
import hmac
import time

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

JS:

```javascript
import crypto from "node:crypto";

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

> **Warning:** Verify against the raw body. Parsing and re-serializing the JSON changes the bytes and breaks the signature.

## Delivery and retries

- Delivery counts only on a 2xx. Respond within 10 seconds and do slow work in the background.
- Failed deliveries are retried with backoff. The body and signature stay the same; dedupe by `job_id`.
- Only public `https://` URLs are accepted. Private and local addresses are rejected.
- Your signing secret will be in your account after launch.

Next: [Errors](https://paperpurr.com/en/docs/errors.md)
