# receive.mx webhooks (inbound parse)

Every email to your domain is parsed and POSTed to your HTTPS callback as JSON, with attachments, signed, with retries.
Plans (per domain): **Free** 3,000 emails/month, 10 MB each · **Starter** $9/month, 10,000, 25 MB · **Growth** $29/month,
50,000, 25 MB. Over the limit, mail is refused with "mailbox full" until the next month (billing for overage, $0.50 per
1,000, opens later). `GET /v1/plans`, `GET /v1/domains/{domain}/usage`. Base URL `https://receive.mx`. Auth: `Authorization: Bearer <domain_key>`.

## 1. Connect the domain
A domain or **subdomain** registered anywhere, with its DNS on your Cloudflare account (free; change the nameservers at
your registrar). Subdomain mode changes only that subdomain's records: the parent domain's own mail (e.g. `lexie.law` → Foundermail) is untouched.

```
POST /v1/domains/connect
{"domain": "in.lexie.law", "cloudflare_token": "…", "confirm_replace_mail_records": true}
→ 201 {"domain", "zone", "subdomain": true, "domain_key": "dk_…" (shown once), "removed_records": [...]}
```
Token permissions (that zone only): Zone Read · DNS Edit · Zone Settings Edit · Email Routing Rules Edit; Account:
Workers Scripts Edit. We enable Email Routing for the (sub)domain (MX `route1-3.mx.cloudflare.net`) and install one small
forwarder Worker per zone (`receivemx-<zone>`). Staging = connect a second subdomain (e.g. `in-staging.lexie.law`) with its
own key and callback.

## 2. Set the callback
```
PUT /v1/domains/{domain}/webhook
{"url": "https://app.example.com/receivemx/inbound", "enabled": true, "delivery": "at_least_once",
 "retention": "keep_30_days",                                 // or "passthrough" (see 5)
 "check_url": "https://app.example.com/receivemx/check"}        // optional; null turns it off
→ 200 {..., "secret": "whsec_…"}   // first time only: save it
GET /v1/domains/{domain}/webhook   // config, secret ids (never the secrets)
```

## 3. The callback
`POST {url}`, `Content-Type: application/json`, 30 s timeout. Headers:
- `X-Receivemx-Id: msg_…` and `Idempotency-Key: msg_…`: one id per email, the same on every retry and replay.
  **Store it and ignore a second call with an id you've already processed.**
- `X-Receivemx-Attempt: 1…`, and `X-Receivemx-Replay: 1` (plus `"replay": true` in the body) when you asked for a replay
- `X-Receivemx-Timestamp: <unix seconds>`
- `X-Receivemx-Signature: v1=<hex>[,v1=<hex>]`, where hex = HMAC-SHA256(secret, timestamp + "\n" + raw body), one per
  active secret. Accept if any matches; reject timestamps more than 5 minutes off.

```json
{
  "id": "msg_…", "domain": "in.lexie.law", "test": false,
  "received_at": "2026-10-11T09:14:03.000Z", "size": 48213,
  "envelope": {"from": "bounce@vendor.com", "to": ["supplier-dpas.k7f3q9@in.lexie.law"]},
  "headers": {"from": "...", "to": "...", "cc": "...", "subject": "...", "message_id": "<...>", "date": "...",
              "in_reply_to": "...", "references": "..."},
  "from": {"address": "priya@harbourfoods.com", "name": "Priya Shah"},
  "subject": "...",
  "auth": {"spf": "pass", "dkim": "pass", "dkim_domains": ["harbourfoods.com"], "dmarc": "pass",
           "source": "authentication-results:mx.cloudflare.net"},
  "text": "...", "text_truncated": false, "html": "...", "html_truncated": false,   // 256 KB each
  "code": "482913",                                                                   // one-time code if found
  "attachments": [
    {"filename": "Brightline DPA.pdf", "content_type": "application/pdf", "size": 412345, "sha256": "…",
     "inline": false, "content_id": null, "from_message": null,
     "content_base64": "…"}          // when the whole callback is under ~8 MB; otherwise "url" (signed, 24 h)
  ],
  "raw_url": "https://receive.mx/v1/files/…"   // the original .eml (signed; 1 h, or 24 h when attachments are URLs)
}
```
- **auth** comes from the topmost `Authentication-Results` header (added by Cloudflare's MX); values are lower-case
  verdicts (`pass`, `fail`, `softfail`, `none`, …). Headers further down the message are ignored, so a sender can't forge them.
- **Forwarded as attachment** (`message/rfc822`): the .eml is listed, and its own attachments are listed after it with
  `from_message: "<inner subject>"`. `inline: true` marks inline/related parts (logos, signatures).
- Every attachment type is passed through; filter on your side. Up to 25 MiB per message.

## 4. Responses, retries and delivery mode
Set `"delivery"` in `PUT /webhook`:
- `"at_least_once"` (default): timeout, network error, `408`, `429`, `5xx` are retried with backoff (1, 5, 15, 30 min,
  1, 2, 4, 8, 12 h …) for 72 h. You may get the same `id` twice, so dedupe on it. Nothing is lost if your app is down for
  a while.
- `"at_most_once"`: one attempt only. If it fails, it's marked `failed` and never resent on its own (you can still
  replay it by hand).
- Either way, other `4xx` are not retried: `413` = too large, `422` = refused.

## 5. What we keep
Set `"retention"` in `PUT /webhook`.

**`"passthrough"`: nothing stored beyond delivery.** The message is held only until your callback returns 2xx, then
deleted: email, headers, text, attachments, raw. Attachments sent as links (callbacks over ~8 MB) stay until those
links expire (24 h), then go. `raw_url` is null when everything fits in the body. The log keeps only generic facts: id,
time, size, attachment count, status, attempts. No sender, subject or file names. Replay isn't possible after
success (`410 NOTHING_TO_REPLAY`). A message that never gets delivered is deleted after 7 days.

**`"keep_30_days"` (default):**
- **Attachment content and the original .eml** are deleted after delivery: 1 h later (24 h if attachments were sent as
  URLs), or 7 days after receipt if delivery never succeeded.
- **The email itself** (headers, from, subject, text/html, auth verdicts, and each attachment's name, type, size and
  sha256) is kept with the delivery log for 30 days: `GET /deliveries/{id}` returns it as `email`.
- Replay works for 30 days. After the content is deleted, a replay lists the attachments with `"content_deleted": true`
  and no content, and `raw_url` is null.

## 6. Address check (optional)
`GET {check_url}?to=<address>`, signed the same way over an empty body. `404` = refuse at SMTP (sender gets a 5xx);
anything else = accept. Answers are cached for 5 minutes.

## 7. Deliveries, replay, test, secrets
```
GET  /v1/domains/{domain}/deliveries?status=pending|delivered|failed|refused|too_large&limit=50
GET  /v1/domains/{domain}/deliveries/{id}
POST /v1/domains/{domain}/deliveries/{id}/replay        // same id, X-Receivemx-Replay: 1
POST /v1/domains/{domain}/test                          // sends a test message with a small PDF ("test": true)
POST /v1/domains/{domain}/webhook/secrets {"overlap_hours": 24}   // rotate: new secret, old ones expire after overlap
DELETE /v1/domains/{domain}/webhook/secrets/{secret_id}           // revoke now
DELETE /v1/domains/{domain}                                       // disconnect
```

## Data handling
Bodies and attachments are never written to logs. Storage is R2 in the EU jurisdiction. Attachments and raw mail are
kept only until delivered (at most 7 days); the email text and attachment names are kept 30 days for the delivery log
and replay. No forwarding, no auto-replies.
