# API reference

The complete HTTP API is described in OpenAPI 3.1:
**[openapi.yaml](/openapi.yaml)** (source:
[`spec/openapi.yaml`](https://github.com/curtcox/subetha/blob/main/spec/openapi.yaml)). Load it
into any OpenAPI viewer or client generator. A test fails the build if a route the relay serves
is missing from it.

Base URLs: `https://subetha.us` (production), `https://staging.subetha.us` (staging, providers
in test mode), `http://localhost:8787` (`wrangler dev`).

## Routes at a glance

| Area | Routes |
|---|---|
| Publish (ntfy) | `PUT`/`POST /{topic}`, `POST /` (JSON), `GET /file/{id}/{name}` (attachments) |
| Subscribe (ntfy) | `GET /{topics}/json`, `/sse`, `/raw`, `/ws`, `/auth`; `?poll=1`, `since`, `priority`, `tags` |
| Meta | `GET /v1/health`, `/v1/status`, `/v1/prices`, `/v1/prices/destinations`, `/v1/config` |
| Sign-in | `POST /v1/dba/signin/start`, `/v1/dba/signin/finish`, `GET /auth/dba/callback` (devicebasedauth.org) |
| Account & billing | `GET`/`POST`/`PATCH /v1/account`, `GET /v1/balance`, `/v1/ledger`, `/v1/usage`, `/v1/audit`, `/v1/payments`, `/v1/payments/{id}/receipt`, `POST /v1/billing/checkout` |
| Tokens | `GET`/`POST /v1/tokens`, `DELETE /v1/tokens/{id}` |
| Topics | `GET /v1/topics`, `GET`/`PUT`/`DELETE /v1/topics/{topic}` |
| Traces & acks | `GET /v1/traces/{id}?topic=`, `POST /v1/ack` |
| Endpoints | `GET`/`POST /v1/endpoints`, `PATCH`/`DELETE /v1/endpoints/{id}`, `POST /v1/endpoints/{id}/verify`, `/v1/endpoints/{id}/test` |
| Subscriptions | `GET`/`POST /v1/subscriptions`, `PATCH`/`DELETE /v1/subscriptions/{id}` |
| Push credentials | `GET`/`POST /v1/push/credentials`, `PATCH`/`DELETE /v1/push/credentials/{id}` (your own APNs/FCM keys) |
| Topic keys (E2EE) | `GET`/`PUT`/`DELETE /v1/keys` (wrapped topic keys) |
| Slack | `GET /v1/slack/install`, `/v1/slack/oauth`, `/v1/slack/teams`, `GET`/`POST`/`DELETE /v1/slack/bindings` |
| Devices | `GET /v1/devices`, `PATCH`/`DELETE /v1/devices/{id}`, `POST /v1/devices/dba` (attach a signing key), `GET`/`DELETE /v1/dba/link`, `POST /v1/dba/link/start`, `/v1/dba/link/finish` |
| Agents | `GET`/`POST /mcp`, `GET /.well-known/oauth-protected-resource` (and `…/mcp`) |
| Provider hooks | `POST /v1/hooks/stripe`, `/v1/hooks/clerk`, `/v1/hooks/slack/events`, `/v1/hooks/twilio/sms`, `/whatsapp`, `/voice-ack`, `/voice-status` |
| Local only | `POST /v1/dev/bootstrap` |

## Conventions

- **Auth:** `Authorization: Bearer <token>`; HTTP Basic with an empty user and the token as the
  password; `?auth=<base64url("Bearer <token>")>`; a devicebasedauth.org device credential
  (`Authorization: DBA …` + `DPoP`); or device request signatures
  ([Credentials](tokens.md#credentials), [Devices](devices.md)).
- **Money** is integer micro-dollars in responses (`*_usd_micros`); request fields that take
  dollars are named `*_usd`.
- **Times** are Unix seconds in messages and milliseconds in control-plane records
  (`created_at`).
- **CORS** is open: the API is meant to be called from web pages with publishable tokens.
  `X-Cost-Micros`, `X-Message-Id`, and `Retry-After` are exposed to scripts.
- **Rate limits:** 600 requests/minute per credential, 60/minute per IP without one; `429` with
  `Retry-After` when exceeded.

## Errors

Every error is JSON with a stable `error` code, the HTTP status, and a human-readable `message`,
plus fields specific to the error (`shortfall_usd_micros` on 402):

```json
{ "error": "spend_cap_exceeded", "http": 403, "message": "topic daily cap reached", "cap": "topic_daily" }
```

| Code | HTTP | Meaning |
|---|---:|---|
| `bad_request` | 400 | Malformed request. |
| `invalid_topic` | 400 | Topic name does not match the topic grammar. |
| `invalid_message` | 400 | Envelope failed validation. |
| `message_too_large` | 413 | Message body exceeds 4096 bytes (use an attachment). |
| `unsupported` | 400 | Feature not supported in this version (e.g. X-Delay). |
| `unauthorized` | 401 | Missing or invalid credentials. |
| `signature_required` | 401 | Topic policy requires a device signature. |
| `bad_signature` | 401 | Signature does not verify against the sender id. |
| `insufficient_balance` | 402 | Payer balance cannot cover the worst-case cost. Body carries shortfall_usd_micros. |
| `no_funded_owner` | 402 | Anonymous publish to a topic with no funded owner. |
| `forbidden` | 403 | Token scope or policy forbids this action. |
| `spend_cap_exceeded` | 403 | A token, topic, or account spend cap would be exceeded. |
| `transport_denied` | 403 | Policy leaves no allowed transport for a required delivery. |
| `topic_paused` | 403 | Topic, device, or account kill switch is on. |
| `not_found` | 404 | No such resource. |
| `conflict` | 409 | Resource already exists or idempotency key reused with a different body. |
| `rate_limited` | 429 | Rate or burst limit reached. Retry-After is set. |
| `internal` | 500 | Unexpected error. |
| `provider_unavailable` | 503 | An upstream provider is unavailable. |

The codes come from [`spec/errors.json`](https://github.com/curtcox/subetha/blob/main/spec/errors.json)
and won't change meaning; new ones may be added.

## Protocol

The message envelope, canonical JSON, signatures, encryption, dedupe, and topic grammar are
specified in [`spec/README.md`](https://github.com/curtcox/subetha/blob/main/spec/README.md), with
a [JSON Schema](https://github.com/curtcox/subetha/blob/main/spec/schema/message.schema.json) and
cross-SDK [test vectors](https://github.com/curtcox/subetha/tree/main/spec/vectors).
