# Sepiasend: instructions for coding agents

Sepiasend is a Resend-compatible email API. It sends through the user's own Cloudflare account, and
Cloudflare bills them for the email at its own price. This page tells you, the agent, how to set it
up for a user without them clicking through the dashboard.

- API base URL: `https://api.sepiasend.com`
- Dashboard: `https://sepiasend.com/app`
- Requests and responses use JSON. Errors look like Resend's: `{ "statusCode": 403, "name": "validation_error", "message": "..." }`.

## Before you start

Tell the user plainly if any of these is missing; Sepiasend cannot work without them.

1. A Cloudflare account. It is also how they sign in to Sepiasend.
2. The sending domain's DNS is on Cloudflare.
3. Cloudflare's Workers Paid plan ($5 a month, paid to Cloudflare). Cloudflare requires it to send to any address.

## 1. Get an API key (the user approves once)

```bash
curl -X POST https://api.sepiasend.com/agent/login
# { "device_code": "...", "user_code": "K7P2QXMD", "verification_url": "https://sepiasend.com/app/agent", "expires_in": 600, "interval": 3 }
```

Show the user `verification_url` and `user_code`. Ask them to open the link, type the code, check
the details shown and approve. The code is typed by hand on purpose; do not build a link that
contains it. If they have never used Sepiasend, the page first asks them to sign in with Cloudflare.
Then poll:

```bash
curl -X POST https://api.sepiasend.com/agent/token -H "Content-Type: application/json" -d '{"device_code": "..."}'
# 400 { "error": "authorization_pending" }   keep polling every `interval` seconds
# 200 { "api_key": "sep_...", "api_url": "https://api.sepiasend.com" }   returned once
# 400 { "error": "expired_token" }           start again from /agent/login
```

Treat `api_key` as a secret: it can manage domains and keys. Never commit it. Use it as
`Authorization: Bearer sep_...` for everything below.

## 2. Turn on a sending domain

Work out which domain to use before you turn anything on. The user may not have named one.

1. Look at what the app sends from today: the `from` address in its email code, and variables such
   as `EMAIL_FROM` or `RESEND_FROM`. The part after the `@` is the sending domain.
2. Check it against the two lists below. If it is in neither, its DNS is not in this Cloudflare
   account: tell the user, and do not pick a different domain for them.
3. If the app has no `from` address yet, or uses several domains, show the user the domains from
   `/domains/available` and ask which to use.

```bash
curl https://api.sepiasend.com/domains -H "Authorization: Bearer $KEY"             # already sending
curl https://api.sepiasend.com/domains/available -H "Authorization: Bearer $KEY"   # in the Cloudflare account, not sending yet
curl -X POST https://api.sepiasend.com/domains -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" -d '{"name": "example.com"}'
```

`name` may be a domain in the user's Cloudflare account or a subdomain of one, such as
`mail.example.com`. Cloudflare creates the SPF, DKIM and bounce records itself. There is nothing to
copy into DNS and nothing to wait for. A `422` means the domain is not in that Cloudflare account.

Domains are free and unlimited on every plan. Add as many as the user needs.

## 3. Create a key for the app

Give the app its own key that can only send:

```bash
curl -X POST https://api.sepiasend.com/api-keys -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" -d '{"name": "production", "permission": "sending_access"}'
# 201 { "id": "...", "object": "api_key", "token": "sep_..." }   the token is shown once
```

Add `"domain_id"` to restrict the key to one domain.

## 4. Point the app at Sepiasend

If the app uses a Resend SDK, do not change its code. Set the key from step 3 as `RESEND_API_KEY`
and set the base URL the way that SDK reads it:

| SDK | Variable | Value |
|---|---|---|
| Node.js (`resend` 4 or newer) | `RESEND_BASE_URL` | `https://api.sepiasend.com` |
| Python | `RESEND_API_URL` | `https://api.sepiasend.com` |
| PHP | `RESEND_BASE_URL` | `api.sepiasend.com` (no `https://`) |
| Ruby | `RESEND_BASE_URL` | `https://api.sepiasend.com/` (trailing slash) |
| Go | `RESEND_BASE_URL` | `https://api.sepiasend.com/` (trailing slash) |
| Rust | `RESEND_BASE_URL` | `https://api.sepiasend.com` |

If the app calls Resend's HTTP API directly, replace `https://api.resend.com` with
`https://api.sepiasend.com`. Put the values wherever the project keeps its secrets, then restart
or redeploy the app. Going back to Resend is the same edit in reverse.

## 5. Send a test and confirm it

```bash
curl -X POST https://api.sepiasend.com/emails -H "Authorization: Bearer $APP_KEY" \
  -H "Content-Type: application/json" \
  -d '{"from": "Acme <hello@example.com>", "to": ["the-user@their-inbox.com"], "subject": "Sepiasend works", "text": "Sent through Cloudflare."}'
# 200 { "id": "..." }
curl https://api.sepiasend.com/emails/ID -H "Authorization: Bearer $KEY"
# "last_event": "delivered" | "sent" | "bounced" | "suppressed" | "failed"
```

Send the test to an address the user controls. Do not use made-up addresses: bounces hurt their
sender reputation.

## What differs from Resend

- Not supported: `scheduled_at`, attachments given by `path` (send base64 `content`), audiences, contacts, broadcasts, receiving email, open and click tracking.
- Only the first `reply_to` address is used. An email can have at most 50 recipients across `to`, `cc` and `bcc`.
- Each recipient counts as one email toward the plan's monthly total. Over the total, sends return `429` with `monthly_quota_exceeded`.
- Transactional email only. Cloudflare does not allow marketing email through this service.
- `Idempotency-Key` is supported on `POST /emails`.

## Webhooks

```bash
curl -X POST https://api.sepiasend.com/webhooks -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json" -d '{"endpoint": "https://app.example.com/webhooks/email", "events": ["email.delivered", "email.bounced"]}'
# 201 { "object": "webhook", "id": "...", "signing_secret": "whsec_..." }
```

Events: `email.sent`, `email.delivered`, `email.bounced`, `email.suppressed`, `email.failed`. Payloads
and the `svix-id`, `svix-timestamp` and `svix-signature` headers match Resend's, so
`resend.webhooks.verify()` works with the `signing_secret`.

## Endpoints

| Method and path | What it does | Key needed |
|---|---|---|
| `POST /emails` | Send one email | any |
| `POST /emails/batch` | Send up to 100 emails | any |
| `GET /emails`, `GET /emails/:id` | Read the log | full access |
| `GET /domains`, `GET /domains/available`, `POST /domains`, `GET /domains/:id`, `DELETE /domains/:id` | Sending domains | full access |
| `GET /api-keys`, `POST /api-keys`, `DELETE /api-keys/:id` | API keys | full access |
| `GET /webhooks`, `POST /webhooks`, `DELETE /webhooks/:id` | Webhooks | full access |
| `POST /agent/login`, `POST /agent/token` | Get a key with the user's approval | none |
