openapi: 3.1.0
info:
  title: SubEtha API
  version: 0.0.1
  summary: Transport-agnostic, ntfy-compatible pub/sub relay.
  description: |
    SubEtha relays messages published to a **topic** to every subscriber over whatever transport
    works (WebSocket, SSE, push, email, Slack, SMS, voice, MCP, Reticulum/LXMF, …).

    * The ntfy-compatible paths (`/{topic}`, `/{topics}/json|sse|raw|ws`, `POST /`) work with stock
      ntfy clients and `curl`.
    * The control plane lives under `/v1/*`.
    * Every metered action is prepaid. An insufficient balance returns HTTP 402 with
      `shortfall_usd_micros`. `X-Dry-Run: 1` returns the route plan and cost without delivering.
    * Money is integer micro-dollars (µ$, `*_usd_micros`). Request bodies that take dollars use
      `*_usd` (decimal).
    * Errors are JSON with a stable `error` code (see the `Error` schema and `spec/errors.json`).
    * Requests are rate limited per credential (600/min) or, without one, per IP (60/min).
      Over the limit: HTTP 429 `rate_limited` with `Retry-After`. Provider hooks, `/v1/health`, and
      `/v1/status` are exempt.

    **Topic path parameters.** A topic is `@handle/name` or `t_<26 base32 chars>`. In paths it is
    written literally (`/@alice/doorbell/json`); the slash in a namespaced topic is part of the
    topic. Tools that percent-encode path parameters (`%40alice%2Fdoorbell`) also work, because
    the relay decodes the path before parsing it. Several topics can be subscribed at once,
    separated by commas (`/t_a…,t_b…/json`).

    The message envelope is defined normatively by
    [`spec/schema/message.schema.json`](https://github.com/curtcox/subetha/blob/main/spec/schema/message.schema.json);
    `components.schemas.Envelope` mirrors it (a test keeps the two in sync).
  license:
    name: MIT
  contact:
    url: https://github.com/curtcox/subetha
externalDocs:
  description: Developer docs
  url: https://subetha.us/docs/
servers:
  - url: https://subetha.us
    description: Production
  - url: https://staging.subetha.us
    description: Staging (providers in test mode)
  - url: http://localhost:8787
    description: Local (wrangler dev)
security:
  - bearerAuth: []
  - basicAuth: []
  - authQuery: []
  - dbaCredential: []
  - deviceSignature: []
  - {}
tags:
  - name: publish
    description: ntfy-compatible publishing.
  - name: subscribe
    description: ntfy-compatible subscriptions (JSON stream, SSE, raw, WebSocket, poll).
  - name: meta
  - name: account
  - name: billing
  - name: tokens
  - name: topics
  - name: traces
  - name: endpoints
    description: Verified non-SDK destinations (email, SMS, voice, webhook, push, LXMF).
  - name: subscriptions
    description: Topic → endpoint bindings (the topic owner pays for delivery).
  - name: slack
  - name: devices
    description: Device enrollment (RFC 8628-style device flow) and per-device policies.
  - name: hooks
    description: Inbound provider webhooks. Called by Stripe, Twilio, and Slack, not by clients.
  - name: mcp
    description: Remote MCP server for agents (Streamable HTTP, API tokens or OAuth via Clerk).
  - name: dev
    description: Local development only.

paths:
  # ------------------------------------------------------------------ ntfy-compatible
  /:
    post:
      tags: [publish]
      operationId: publishJson
      summary: Publish a JSON envelope
      description: |
        ntfy JSON publishing, extended to full SubEtha envelopes. Signed messages (spec §4) and
        E2EE messages (spec §5) must use this form. ntfy aliases are accepted: `priority` as a
        name (`high`), `attach` + `filename`, and `markdown: true`. `delay` is rejected
        (`unsupported`). Route headers (`X-Route-Mode`, …) override `route` in the body.
      parameters:
        - $ref: "#/components/parameters/IdempotencyKey"
        - $ref: "#/components/parameters/XDryRun"
        - $ref: "#/components/parameters/XRouteMode"
        - $ref: "#/components/parameters/XMaxCost"
        - $ref: "#/components/parameters/XDeny"
        - $ref: "#/components/parameters/XAllow"
        - $ref: "#/components/parameters/XDeadline"
      requestBody:
        required: true
        content:
          application/json:
            schema:
              allOf:
                - $ref: "#/components/schemas/Envelope"
                - type: object
                  properties:
                    priority:
                      oneOf:
                        - type: integer
                          minimum: 1
                          maximum: 5
                        - enum: [min, low, default, high, max, urgent]
                    attach: { type: string, description: ntfy alias for `attachment.url`. }
                    filename: { type: string, description: ntfy alias for `attachment.name`. }
                    markdown: { type: boolean, description: "`true` sets `content_type: text/markdown`." }
            example: { topic: "@alice/doorbell", title: Front door, message: Opened, priority: 4, tags: [door] }
      responses:
        "200": { $ref: "#/components/responses/Published" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
        "413": { $ref: "#/components/responses/Error" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /{topic}:
    parameters:
      - $ref: "#/components/parameters/Topic"
    put:
      tags: [publish]
      operationId: publishPut
      summary: Publish (ntfy, PUT)
      description: Same as `POST /{topic}`.
      parameters: &publishParams
        - $ref: "#/components/parameters/XTitle"
        - $ref: "#/components/parameters/XPriority"
        - $ref: "#/components/parameters/XTags"
        - $ref: "#/components/parameters/XClick"
        - $ref: "#/components/parameters/XIcon"
        - $ref: "#/components/parameters/XActions"
        - $ref: "#/components/parameters/XAttach"
        - $ref: "#/components/parameters/XMarkdown"
        - $ref: "#/components/parameters/XContentType"
        - $ref: "#/components/parameters/XHops"
        - $ref: "#/components/parameters/XExpires"
        - $ref: "#/components/parameters/XFilename"
        - $ref: "#/components/parameters/XMessage"
        - $ref: "#/components/parameters/XDelay"
        - $ref: "#/components/parameters/XRouteMode"
        - $ref: "#/components/parameters/XMaxCost"
        - $ref: "#/components/parameters/XDeny"
        - $ref: "#/components/parameters/XAllow"
        - $ref: "#/components/parameters/XDeadline"
        - $ref: "#/components/parameters/IdempotencyKey"
        - $ref: "#/components/parameters/XDryRun"
        - $ref: "#/components/parameters/XSender"
        - $ref: "#/components/parameters/XSignature"
        - $ref: "#/components/parameters/XMessageId"
        - $ref: "#/components/parameters/XTime"
        - $ref: "#/components/parameters/MessageQuery"
        - $ref: "#/components/parameters/TitleQuery"
        - $ref: "#/components/parameters/PriorityQuery"
        - $ref: "#/components/parameters/TagsQuery"
      requestBody: &publishBody
        description: |
          The message text (UTF-8, at most 4096 bytes); empty bodies use `X-Message`/`?message=` or
          "triggered". With `X-Filename`, the body is the attachment (up to 15 MB).
        content:
          text/plain:
            schema: { type: string, maxLength: 4096 }
            example: Backup done
          application/octet-stream:
            schema: { type: string, format: binary }
      responses: &publishResponses
        "200": { $ref: "#/components/responses/Published" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
        "413": { $ref: "#/components/responses/Error" }
        "429": { $ref: "#/components/responses/RateLimited" }
    post:
      tags: [publish]
      operationId: publish
      summary: Publish (ntfy)
      description: |
        `curl -d "Backup done" https://subetha.us/@me/backups`. ntfy headers, their short aliases,
        and query-string equivalents (`?title=…`) are accepted. The publisher pays for the publish;
        the topic owner pays for fan-out and delivery. Anonymous publishes need the topic policy
        `public_publish` and a funded owner (else `no_funded_owner`, HTTP 402).
      parameters: *publishParams
      requestBody: *publishBody
      responses: *publishResponses
    get:
      tags: [publish]
      operationId: topicPage
      summary: Open the topic in the web console
      description: Browsers visiting a topic URL are redirected to the console. ntfy's "publish via GET" is not supported.
      security: []
      responses:
        "302":
          description: Redirect to `/console/#topic=…`.

  /{topics}/json:
    parameters:
      - $ref: "#/components/parameters/Topics"
    get:
      tags: [subscribe]
      operationId: subscribeJson
      summary: Subscribe (newline-delimited JSON)
      description: |
        A long-lived NDJSON stream. The first line is an `open` event, then each message as it
        arrives, with periodic `keepalive` events. With `poll=1` the cached messages are returned
        and the connection closes (`since` defaults to `all` for polls). The topic owner pays for
        the connection and live deliveries.
      parameters: &subscribeParams
        - $ref: "#/components/parameters/Since"
        - $ref: "#/components/parameters/Poll"
        - $ref: "#/components/parameters/PriorityFilter"
        - $ref: "#/components/parameters/TagsFilter"
        - $ref: "#/components/parameters/Scheduled"
      responses:
        "200":
          description: One JSON envelope per line.
          content:
            application/x-ndjson:
              schema: { $ref: "#/components/schemas/Envelope" }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /{topics}/sse:
    parameters:
      - $ref: "#/components/parameters/Topics"
    get:
      tags: [subscribe]
      operationId: subscribeSse
      summary: Subscribe (server-sent events)
      description: 'Each event is `id: <message id>` and `data: <JSON envelope>`. Resume with `since=<last id>`.'
      parameters: *subscribeParams
      responses:
        "200":
          description: An `text/event-stream` of envelopes.
          content:
            text/event-stream:
              schema: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /{topics}/raw:
    parameters:
      - $ref: "#/components/parameters/Topics"
    get:
      tags: [subscribe]
      operationId: subscribeRaw
      summary: Subscribe (message bodies only)
      description: One message body per line (newlines in bodies become spaces). Keepalives are empty lines.
      parameters: *subscribeParams
      responses:
        "200":
          description: Plain text.
          content:
            text/plain:
              schema: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /{topics}/ws:
    parameters:
      - $ref: "#/components/parameters/Topics"
    get:
      tags: [subscribe]
      operationId: subscribeWs
      summary: Subscribe (WebSocket)
      description: |
        Upgrade to a WebSocket. Each server frame is one JSON envelope. Clients may send
        `{"event":"ack","id":"<message id>"}` to acknowledge a message (stops escalation).
        One topic per WebSocket. Browsers pass the token as `?auth=<base64("Bearer <token>")>`.
      parameters: *subscribeParams
      responses:
        "101":
          description: Switching protocols.
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /{topics}/auth:
    parameters:
      - $ref: "#/components/parameters/Topics"
    get:
      tags: [subscribe]
      operationId: checkTopicAccess
      summary: Check access (ntfy)
      description: 'ntfy clients call this to check credentials. Returns `{"success": true}` when every topic exists and the caller may read it.'
      responses:
        "200":
          description: Access OK.
          content:
            application/json:
              schema:
                type: object
                properties: { success: { const: true } }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ meta
  /v1/health:
    get:
      tags: [meta]
      operationId: health
      security: []
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  ok: { const: true }
                  environment: { enum: [local, staging, production] }
                  time: { type: integer, description: Unix seconds. }
  /v1/prices:
    get:
      tags: [meta]
      operationId: prices
      summary: The generated price table
      description: Generated from `pricing/costs.yaml`. Every SKU at every tier has a margin above the floor.
      security: []
      responses:
        "200":
          description: Price table.
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
  /v1/prices/destinations:
    get:
      tags: [meta]
      operationId: destinationPrices
      summary: Today's SMS and voice prices for numbers outside the US
      description: |
        Refreshed daily from Twilio's live prices (each country's highest current price, marked up
        with the `deliver_sms_intl` / `deliver_voice_intl` SKUs). Prices are µ$ per SMS segment or
        call minute, per tier. A `null` price means the country isn't priced today, and numbers
        there aren't served. Accounts opt into countries with `telephony_countries`.
      security: []
      responses:
        "200":
          description: Per-country prices.
          content:
            application/json:
              schema:
                type: object
                properties:
                  updated_at: { type: [integer, "null"], description: Unix seconds of the last refresh. }
                  countries:
                    type: object
                    additionalProperties:
                      type: object
                      properties:
                        name: { type: string }
                        calling_code: { type: string }
                        sms_usd_micros: { type: [object, "null"], additionalProperties: { type: integer } }
                        voice_usd_micros_per_minute: { type: [object, "null"], additionalProperties: { type: integer } }
  /v1/status:
    get:
      tags: [meta]
      operationId: status
      summary: Component health and configured transports (for the status page)
      security: []
      responses:
        "200":
          description: Healthy.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Status" }
        "503":
          description: A core component (D1 or KV) is failing.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Status" }
  /v1/config:
    get:
      tags: [meta]
      operationId: config
      summary: Public client configuration
      security: []
      responses:
        "200":
          description: Config for web clients.
          content:
            application/json:
              schema:
                type: object
                properties:
                  environment: { enum: [local, staging, production] }
                  clerk_publishable_key: { type: [string, "null"] }
                  vapid_public_key:
                    type: [string, "null"]
                    description: Web Push application server key (base64url). Present once Web Push is configured.

  # ------------------------------------------------------------------ account
  /v1/account:
    get:
      tags: [account]
      operationId: getAccount
      responses:
        "200":
          description: The caller's account.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Account" }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    post:
      tags: [account]
      operationId: createAccount
      summary: Create an account (Clerk session required)
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                handle: { type: string, pattern: "^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$" }
                email: { type: string, format: email }
      responses:
        "201":
          description: Created.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: string }
                  handle: { type: [string, "null"] }
        "401": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
    patch:
      tags: [account]
      operationId: updateAccount
      summary: Set the handle (once), caps, the kill switch, auto-recharge, or SMS/voice countries
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                handle: { type: string }
                daily_cap_micros: { type: [integer, "null"], minimum: 0 }
                monthly_cap_micros: { type: [integer, "null"], minimum: 0, description: Account-wide monthly spend cap (UTC calendar month). }
                allow_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" }, description: Account-wide transport allow-list intersected with topic/token/message policies (`null` = all). }
                deny_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" }, description: Account-wide transport deny-list (`null` = none). }
                oauth_daily_cap_micros:
                  type: [integer, "null"]
                  minimum: 0
                  description: Daily spend cap per OAuth client (MCP agents signed in through the authorization server). `null` = $1; `0` turns OAuth access off.
                paused: { type: boolean, description: Account-wide kill switch. }
                telephony_countries:
                  type: array
                  items: { type: string, pattern: "^[A-Z]{2}$" }
                  description: |
                    Countries outside the US whose numbers may be SMS/voice endpoints (opt-in per
                    country; see `GET /v1/prices/destinations` for the list and today's prices).
                    Removing a country stops deliveries to its numbers. `[]` = US only.
                auto_recharge:
                  description: |
                    Off-session top-up with the saved card when the balance drops below
                    `threshold_usd` (`null` turns it off). Needs a card saved by a checkout with
                    `save_card: true` (else `conflict`). Not a subscription; it stops after a decline.
                  oneOf:
                    - type: "null"
                    - type: object
                      required: [threshold_usd, amount_usd]
                      properties:
                        threshold_usd: { type: number, minimum: 0 }
                        amount_usd: { type: number }
      responses:
        "200":
          description: The updated account record.
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
  /v1/balance:
    get:
      tags: [account]
      operationId: getBalance
      responses:
        "200":
          description: Balance.
          content:
            application/json:
              schema:
                type: object
                properties:
                  available_usd_micros: { type: integer }
                  reserved_usd_micros: { type: integer }
        "401": { $ref: "#/components/responses/Error" }
  /v1/ledger:
    get:
      tags: [account]
      operationId: getLedger
      summary: Append-only ledger entries
      parameters:
        - name: after
          in: query
          schema: { type: integer, default: 0 }
          description: Return entries with a sequence number greater than this.
        - name: limit
          in: query
          schema: { type: integer, default: 100 }
      responses:
        "200":
          description: Ledger entries (`topup`, `reserve`, `settle`, `release`, `refund`, `adjust`).
          content:
            application/json:
              schema:
                type: object
                properties:
                  entries:
                    type: array
                    items: { type: object, additionalProperties: true }
  /v1/payments:
    get:
      tags: [billing]
      operationId: listPayments
      summary: Card payments credited to the account (newest first)
      parameters:
        - name: limit
          in: query
          schema: { type: integer, default: 50, minimum: 1, maximum: 200 }
      responses:
        "200":
          description: Payments, with the top-up fee and credit for each and any refund or dispute.
          content:
            application/json:
              schema:
                type: object
                properties:
                  payments:
                    type: array
                    items: { $ref: "#/components/schemas/Payment" }
        "403": { $ref: "#/components/responses/Error" }
  /v1/payments/{id}/receipt:
    get:
      tags: [billing]
      operationId: getReceipt
      summary: A printable HTML receipt for one payment
      parameters:
        - { name: id, in: path, required: true, schema: { type: string }, description: The Stripe payment intent id. }
      responses:
        "200":
          description: The receipt.
          content:
            text/html:
              schema: { type: string }
        "404": { $ref: "#/components/responses/Error" }
  /v1/usage:
    get:
      tags: [account]
      operationId: getUsage
      summary: Spend by category and day
      description: |
        Computed from the ledger: each charge's ref says what it was for, and a reservation and
        its settlement (or a credit lease and its return) share the ref, so a ref's net is what it
        cost. Each finished charge counts on the UTC day it started. In-flight deliveries and
        credit leases that topics still hold are `pending_micros`. Categories: `publish`,
        `deliveries` (paid transports), `live` (live delivery, retention, connections, charged
        through topic leases), `rent` (devices and endpoints), and `other` (dry runs, duplicate
        publishes, downloads, endpoint verification, inbound email/SMS).
      parameters:
        - name: days
          in: query
          schema: { type: integer, default: 30, minimum: 1, maximum: 90 }
      responses:
        "200":
          description: Usage.
          content:
            application/json:
              schema:
                type: object
                properties:
                  categories: { type: object, additionalProperties: { type: string }, description: Display names. }
                  since: { type: integer, description: Start of the first day (Unix ms). }
                  days:
                    type: array
                    items:
                      type: object
                      properties:
                        day: { type: string, format: date }
                        micros: { type: object, additionalProperties: { type: integer } }
                        total: { type: integer }
                  totals: { type: object, additionalProperties: { type: integer } }
                  total_micros: { type: integer }
                  pending_micros: { type: integer }
                  live_by_topic:
                    type: array
                    items: { type: object, properties: { topic: { type: string }, micros: { type: integer } } }
                  by_sku:
                    type: array
                    description: Spend grouped by pricing SKU (sorted descending).
                    items: { type: object, properties: { sku: { type: string }, micros: { type: integer } } }
        "403": { $ref: "#/components/responses/Error" }
  /v1/audit:
    get:
      tags: [account]
      operationId: getAuditLog
      summary: Audit log (newest first)
      description: Security-relevant changes to the account (tokens, devices, endpoints, topics, billing settings, freezes), with the actor that made them.
      parameters:
        - name: before
          in: query
          schema: { type: integer }
          description: Return entries older than this id (from `next_before`).
        - name: limit
          in: query
          schema: { type: integer, default: 50, minimum: 1, maximum: 200 }
      responses:
        "200":
          description: Audit entries.
          content:
            application/json:
              schema:
                type: object
                properties:
                  entries:
                    type: array
                    items:
                      type: object
                      properties:
                        id: { type: integer }
                        actor: { type: string, description: "Principal kind: token, user, device, or system." }
                        action: { type: string, examples: [token.create, device.enroll] }
                        detail: {}
                        time: { type: integer, description: Unix milliseconds. }
                  next_before: { type: [integer, "null"] }
        "403": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ billing
  /v1/billing/checkout:
    post:
      tags: [billing]
      operationId: createCheckout
      summary: Start a Stripe Checkout top-up
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [amount_usd]
              properties:
                amount_usd: { type: number, description: "Top-up amount in USD (minimum $5)." }
                success_url: { type: string, format: uri }
                cancel_url: { type: string, format: uri }
                save_card: { type: boolean, description: Save the card for auto-recharge. }
      responses:
        "200":
          description: Checkout session; redirect the user to `url`.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: string }
                  url: { type: string, format: uri }
                  creditMicros: { type: integer }
                  feeMicros: { type: integer }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ tokens
  /v1/tokens:
    get:
      tags: [tokens]
      operationId: listTokens
      responses:
        "200":
          description: Tokens (secrets are never returned).
          content:
            application/json:
              schema:
                type: object
                properties:
                  tokens:
                    type: array
                    items: { $ref: "#/components/schemas/Token" }
        "403": { $ref: "#/components/responses/Error" }
    post:
      tags: [tokens]
      operationId: createToken
      summary: Create a scoped, capped API token
      description: |
        Scopes: `admin`, `*`, `publish`, `subscribe`, or `publish:<pattern>` / `subscribe:<pattern>`
        where the pattern is `*`, an exact topic, or `@handle/*`. **Publishable tokens** (for web
        pages) set `origins`, exactly one non-wildcard topic scope, and a `daily_cap_usd`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [scopes]
              properties:
                name: { type: string }
                scopes:
                  type: array
                  minItems: 1
                  items: { type: string, examples: ["publish:@alice/*", "subscribe:@alice/doorbell", admin] }
                transports:
                  type: array
                  items: { $ref: "#/components/schemas/Transport" }
                  description: Allow-list of transports this token's publishes may use.
                deny_transports:
                  type: array
                  items: { $ref: "#/components/schemas/Transport" }
                  description: Deny-list of transports this token may not use.
                daily_cap_usd: { type: number }
                total_cap_usd: { type: number }
                origins:
                  type: array
                  items: { type: string, pattern: "^https?://[^/]+$" }
                ips:
                  type: array
                  maxItems: 32
                  items: { type: string, examples: ["203.0.113.0/24", "2001:db8::/32"] }
                  description: IPv4/IPv6 addresses or CIDR ranges the token may be used from (matched against the client address).
                expires_in_s: { type: integer }
      responses:
        "201":
          description: The token secret, shown once.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: string }
                  token: { type: string, pattern: "^se_[a-z2-7]{16}_[a-z2-7]{32}$" }
                  scopes: { type: array, items: { type: string } }
                  note: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
  /v1/tokens/{id}:
    delete:
      tags: [tokens]
      operationId: revokeToken
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      responses:
        "200":
          description: Revoked.
          content:
            application/json:
              schema: { type: object, properties: { revoked: { const: true } } }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ topics
  /v1/topics:
    get:
      tags: [topics]
      operationId: listTopics
      responses:
        "200":
          description: Topics owned by the caller's account.
          content:
            application/json:
              schema:
                type: object
                properties:
                  topics:
                    type: array
                    items:
                      type: object
                      properties:
                        name: { type: string }
                        policy: { $ref: "#/components/schemas/TopicPolicy" }
                        endpoint_subscriptions: { type: integer, description: Aggregate count of verified endpoint subscriptions on this topic. }
                        created_at: { type: integer }
  /v1/topics/{topic}:
    parameters:
      - $ref: "#/components/parameters/Topic"
    get:
      tags: [topics]
      operationId: getTopic
      responses:
        "200":
          description: Topic metadata (owner only).
          content:
            application/json:
              schema: { $ref: "#/components/schemas/TopicMeta" }
        "404": { $ref: "#/components/responses/Error" }
    put:
      tags: [topics]
      operationId: putTopic
      summary: Create or configure a topic
      description: You can create topics in your own `@handle/…` namespace and secret `t_…` topics.
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                policy: { $ref: "#/components/schemas/TopicPolicy" }
      responses:
        "200":
          description: Updated.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/TopicMeta" }
        "201":
          description: Created.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/TopicMeta" }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
    delete:
      tags: [topics]
      operationId: deleteTopic
      summary: Delete a topic (owner only)
      description: Closes live subscribers, returns held credit leases, clears retained messages and traces, and removes the topic's subscriptions, Slack bindings, and wrapped keys.
      responses:
        "200":
          description: Deleted.
          content:
            application/json:
              schema: { type: object, properties: { deleted: { const: true } } }
        "400": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ traces & acks
  /v1/keys:
    put:
      tags: [topics]
      operationId: putWrappedKeys
      summary: Upload E2EE topic keys wrapped to devices (spec §5.1)
      description: |
        Stores wrapped keys for topics you own, replacing any with the same topic, kid, and
        recipient device. The relay can't open them. Up to 100 per request, 1000 per topic.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [keys]
              properties:
                keys:
                  type: array
                  minItems: 1
                  maxItems: 100
                  items: { $ref: "#/components/schemas/WrappedKey" }
      responses:
        "200":
          description: Stored.
          content:
            application/json:
              schema: { type: object, properties: { stored: { type: integer } } }
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
    get:
      tags: [topics]
      operationId: listWrappedKeys
      summary: Wrapped keys addressed to this device, or (admin) all of your topics' wrapped keys
      parameters:
        - { name: topic, in: query, schema: { type: string } }
      responses:
        "200":
          description: Wrapped keys, oldest first.
          content:
            application/json:
              schema:
                type: object
                properties:
                  keys:
                    type: array
                    items:
                      allOf:
                        - { $ref: "#/components/schemas/WrappedKey" }
                        - { type: object, properties: { created_at: { type: integer } } }
        "402": { $ref: "#/components/responses/Error" }
    delete:
      tags: [topics]
      operationId: deleteWrappedKeys
      summary: Delete wrapped keys for one of your topics
      parameters:
        - { name: topic, in: query, required: true, schema: { type: string } }
        - { name: kid, in: query, schema: { type: string } }
        - { name: device, in: query, schema: { type: string }, description: Recipient device id. }
      responses:
        "200":
          description: How many were deleted.
          content:
            application/json:
              schema: { type: object, properties: { deleted: { type: integer } } }
        "404": { $ref: "#/components/responses/Error" }
  /v1/traces/{id}:
    get:
      tags: [traces]
      operationId: getTrace
      summary: Per-message route and cost trace (topic owner only)
      parameters:
        - { name: id, in: path, required: true, schema: { type: string }, description: Message id (ULID). }
        - { name: topic, in: query, required: true, schema: { type: string } }
      responses:
        "200":
          description: Trace.
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        "400": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /v1/ack:
    post:
      tags: [traces]
      operationId: ack
      summary: Acknowledge a message (stops escalation)
      description: The caller needs read access to the topic (tokens need its subscribe scope; secret `t_` topics are readable by name).
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [topic, id]
              properties:
                topic: { type: string }
                id: { type: string }
                via: { type: string, default: http, maxLength: 16, description: Transport the message arrived by. }
      responses:
        "200":
          description: Acknowledged (idempotent).
          content:
            application/json:
              schema: { type: object, properties: { acked: { const: true } } }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ endpoints
  /v1/endpoints:
    get:
      tags: [endpoints]
      operationId: listEndpoints
      responses:
        "200":
          description: Endpoints (addresses masked).
          content:
            application/json:
              schema:
                type: object
                properties:
                  endpoints:
                    type: array
                    items: { $ref: "#/components/schemas/Endpoint" }
    post:
      tags: [endpoints]
      operationId: createEndpoint
      summary: Add an endpoint
      description: |
        `email`, `sms`, `voice`, and `whatsapp` endpoints get a 6-digit code (charged like one
        delivery) and must be verified within 30 minutes (5 attempts). `sms`/`voice`/`whatsapp`
        accept US numbers (`+1NXXNXXXXXX`) by default, plus numbers in countries the account opted
        into via `telephony_countries` (see `GET /v1/prices/destinations`). `webhook` endpoints
        return a signing secret once. Slack endpoints are created by `POST /v1/slack/bindings`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [kind, address]
              properties:
                kind: { enum: [email, sms, voice, whatsapp, webhook, webpush, apns, fcm, lxmf] }
                address:
                  type: string
                  description: |
                    email address; E.164 phone number (US or opted-in country); `https://` URL;
                    Web Push subscription JSON (`{"endpoint":…,"keys":{"p256dh":…,"auth":…}}`);
                    APNs device token (hex); FCM registration token; LXMF destination hash (32 hex).
                name: { type: string }
                credential_id:
                  type: string
                  description: Optional BYO push credential id (`pcred_…`) for `apns` or `fcm` endpoints.
      responses:
        "201":
          description: Created.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: "#/components/schemas/Endpoint"
                  - type: object
                    properties:
                      secret: { type: string, description: Webhook signing secret (`whsec_…`), shown once. }
                      note: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }
  /v1/endpoints/{id}:
    patch:
      tags: [endpoints]
      operationId: updateEndpoint
      summary: Rename an endpoint or set an SMS endpoint's inbound topic
      description: |
        Texts from a verified SMS endpoint's number to the SubEtha number publish to its
        `inbound_topic` (which must be a topic you own), charged as `inbound_sms_us`, or
        `inbound_mms_us` when the text carries a JPEG, PNG, or GIF picture, which becomes the
        message's attachment. `null` turns inbound texts off.
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string, maxLength: 100 }
                inbound_topic: { type: [string, "null"] }
                credential_id: { type: [string, "null"], description: "APNs/FCM endpoints: bind to a BYO push credential id (`null` = account default or operator credentials)." }
      responses:
        "200":
          description: Updated.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Endpoint" }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    delete:
      tags: [endpoints]
      operationId: deleteEndpoint
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      responses:
        "200":
          description: Deleted (with its subscriptions).
          content:
            application/json:
              schema: { type: object, properties: { deleted: { const: true } } }
        "404": { $ref: "#/components/responses/Error" }
  /v1/endpoints/{id}/verify:
    post:
      tags: [endpoints]
      operationId: verifyEndpoint
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [code]
              properties:
                code: { type: string, pattern: "^[0-9]{6}$" }
      responses:
        "200":
          description: Verified.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Endpoint" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /v1/endpoints/{id}/test:
    post:
      tags: [endpoints]
      operationId: testEndpoint
      summary: Send a test message to an endpoint (endpoint inspector)
      description: |
        Delivers one sample message to a verified endpoint now and reports what happened. For
        `webhook` endpoints the response includes the exact request the relay sent (headers,
        `X-SubEtha-Signature`, body) and the receiver's response (status, headers, first 4 KB of
        the body), so you can debug a signature check. Other transports report the provider's
        outcome. Costs one delivery of the endpoint's transport, charged whether or not the
        receiver accepts it (SMS/voice count toward the new-funds hold).
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                topic: { type: string, description: "Topic named in the sample message (default `@subetha/inspector`)." }
                title: { type: string, maxLength: 256 }
                message: { type: string, maxLength: 1000 }
                priority: { type: integer, minimum: 1, maximum: 5 }
                tags: { type: array, maxItems: 10, items: { type: string } }
      responses:
        "200":
          description: The attempt, delivered or not.
          content:
            application/json:
              schema:
                type: object
                required: [result, message, charged_usd_micros, latency_ms]
                properties:
                  result: { enum: [delivered, failed] }
                  error: { type: string }
                  endpoint: { type: object, properties: { id: { type: string }, kind: { $ref: "#/components/schemas/Transport" } } }
                  message: { $ref: "#/components/schemas/Envelope" }
                  charged_usd_micros: { type: integer }
                  latency_ms: { type: integer }
                  detail: { description: Provider outcome (HTTP status, message id, SMS segments). }
                  request:
                    type: object
                    description: Webhooks only. The request as sent.
                    properties:
                      method: { const: POST }
                      url: { type: string }
                      headers: { type: object, additionalProperties: { type: string } }
                      body: { type: string }
                  response:
                    type: object
                    description: Webhooks only. What the receiver answered.
                    properties:
                      status: { type: integer }
                      headers: { type: object, additionalProperties: { type: string } }
                      body: { type: string }
                      truncated: { type: boolean }
        "400": { $ref: "#/components/responses/Error" }
        "402": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
        "429": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ push credentials (BYO)
  /v1/push/credentials:
    get:
      tags: [endpoints]
      operationId: listPushCredentials
      summary: List BYO APNs and FCM push credentials (private keys redacted)
      responses:
        "200":
          description: Stored push credentials for this account.
          content:
            application/json:
              schema:
                type: object
                properties:
                  credentials:
                    type: array
                    items: { $ref: "#/components/schemas/PushCredential" }
    post:
      tags: [endpoints]
      operationId: createPushCredential
      summary: Add BYO APNs or FCM credentials for your own mobile app
      description: |
        Private keys are validated with WebCrypto on upload, stored write-only, and never returned
        in responses. The first credential of a kind becomes the account default for that kind;
        endpoints can also pin a specific `credential_id`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [kind]
              properties:
                kind: { enum: [apns, fcm] }
                name: { type: string, maxLength: 100 }
                is_default: { type: boolean }
                key_p8: { type: string, description: "APNs: PEM-encoded PKCS#8 P-256 private key (.p8)." }
                key_id: { type: string, pattern: "^[A-Z0-9]{10}$", description: "APNs: 10-character Apple Key ID." }
                team_id: { type: string, pattern: "^[A-Z0-9]{10}$", description: "APNs: 10-character Apple Team ID." }
                topic: { type: string, description: "APNs: app bundle identifier (apns-topic)." }
                sandbox: { type: boolean, description: "APNs: send to api.sandbox.push.apple.com." }
                service_account: { description: "FCM: Firebase service-account JSON string or object." }
                project_id: { type: string, description: "FCM: Firebase project id (if not using service_account)." }
                client_email: { type: string, description: "FCM: service-account email." }
                private_key: { type: string, description: "FCM: PEM-encoded PKCS#8 RSA private key." }
                private_key_id: { type: string, description: "FCM: optional key id." }
      responses:
        "201":
          description: Created (private key omitted).
          content:
            application/json:
              schema: { $ref: "#/components/schemas/PushCredential" }
        "400": { $ref: "#/components/responses/Error" }
  /v1/push/credentials/{id}:
    patch:
      tags: [endpoints]
      operationId: updatePushCredential
      summary: Rename a push credential or make it the account default
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string, maxLength: 100 }
                is_default: { type: boolean }
      responses:
        "200":
          description: Updated.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/PushCredential" }
        "400": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    delete:
      tags: [endpoints]
      operationId: deletePushCredential
      summary: Delete a BYO push credential
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      responses:
        "200":
          description: Deleted.
          content:
            application/json:
              schema: { type: object, properties: { deleted: { const: true } } }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ subscriptions
  /v1/subscriptions:
    get:
      tags: [subscriptions]
      operationId: listSubscriptions
      responses:
        "200":
          description: Endpoint subscriptions.
          content:
            application/json:
              schema:
                type: object
                properties:
                  subscriptions:
                    type: array
                    items: { $ref: "#/components/schemas/Subscription" }
    post:
      tags: [subscriptions]
      operationId: createSubscription
      summary: Deliver a topic to an endpoint
      description: |
        Only the topic owner can add endpoint subscriptions (the owner pays for delivery).
        `after_s` makes this an escalation step: it is used only if the message is still unacked
        after that many seconds. `min_priority` skips lower-priority messages.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [topic, endpoint_id]
              properties:
                topic: { type: string }
                endpoint_id: { type: string }
                filter: { $ref: "#/components/schemas/SubscriptionFilter" }
                after_s: { type: integer, minimum: 0, maximum: 3600 }
                min_priority: { type: integer, minimum: 1, maximum: 5 }
                paused: { type: boolean, description: Start the subscription paused (muted). }
      responses:
        "201":
          description: Created.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Subscription" }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
  /v1/subscriptions/{id}:
    patch:
      tags: [subscriptions]
      operationId: updateSubscription
      summary: Update a subscription's filter, escalation delay, min_priority, or paused state
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                filter: { $ref: "#/components/schemas/SubscriptionFilter" }
                after_s: { type: integer, minimum: 0, maximum: 3600 }
                min_priority: { type: [integer, "null"], minimum: 1, maximum: 5 }
                paused: { type: boolean }
      responses:
        "200":
          description: Updated.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Subscription" }
        "400": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    delete:
      tags: [subscriptions]
      operationId: deleteSubscription
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
      responses:
        "200":
          description: Deleted.
          content:
            application/json:
              schema: { type: object, properties: { deleted: { const: true } } }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ Slack
  /v1/slack/install:
    get:
      tags: [slack]
      operationId: slackInstall
      summary: Get the Slack OAuth install URL
      responses:
        "200":
          description: Redirect the user to `url`.
          content:
            application/json:
              schema: { type: object, properties: { url: { type: string, format: uri } } }
        "503": { $ref: "#/components/responses/Error" }
  /v1/slack/oauth:
    get:
      tags: [slack]
      operationId: slackOAuthCallback
      summary: Slack OAuth redirect target
      security: []
      parameters:
        - { name: code, in: query, required: true, schema: { type: string } }
        - { name: state, in: query, required: true, schema: { type: string } }
      responses:
        "302":
          description: Back to the console (`/console/#tab=endpoints&slack=installed&team=…&team_id=…`).
        "403": { $ref: "#/components/responses/Error" }
  /v1/slack/teams:
    get:
      tags: [slack]
      operationId: slackTeams
      summary: Slack workspaces the app is installed in for this account
      responses:
        "200":
          description: Installed workspaces (use `team_id` with `POST /v1/slack/bindings`).
          content:
            application/json:
              schema:
                type: object
                properties:
                  teams:
                    type: array
                    items:
                      type: object
                      properties:
                        team_id: { type: string }
                        team_name: { type: string }
                        created_at: { type: integer }
        "403": { $ref: "#/components/responses/Error" }
  /v1/slack/bindings:
    get:
      tags: [slack]
      operationId: listSlackBindings
      summary: List bound Slack channels for this account
      responses:
        "200":
          description: Slack channel ↔ topic bindings.
          content:
            application/json:
              schema:
                type: object
                properties:
                  bindings:
                    type: array
                    items:
                      type: object
                      properties:
                        team_id: { type: string }
                        team_name: { type: [string, "null"] }
                        channel: { type: string }
                        topic: { type: string }
                        endpoint_id: { type: [string, "null"] }
                        created_at: { type: integer }
        "403": { $ref: "#/components/responses/Error" }
    post:
      tags: [slack]
      operationId: slackBind
      summary: Bind a Slack channel to a topic (both directions by default)
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [team_id, channel, topic]
              properties:
                team_id: { type: string }
                channel: { type: string, pattern: "^[CG][A-Z0-9]{6,}$" }
                topic: { type: string }
                inbound: { type: boolean, default: true, description: Channel messages publish to the topic. }
                outbound: { type: boolean, default: true, description: Topic messages post to the channel. }
      responses:
        "201":
          description: Bound.
          content:
            application/json:
              schema:
                type: object
                properties:
                  bound: { const: true }
                  endpoint_id: { type: [string, "null"] }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    delete:
      tags: [slack]
      operationId: slackUnbind
      summary: Unbind a Slack channel from its topic
      parameters:
        - { name: team_id, in: query, required: true, schema: { type: string } }
        - { name: channel, in: query, required: true, schema: { type: string } }
      responses:
        "200":
          description: Unbound (and any outbound Slack endpoint created for the binding is removed).
          content:
            application/json:
              schema:
                type: object
                properties:
                  deleted: { const: true }
        "400": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ devices
  /v1/devices:
    get:
      tags: [devices]
      operationId: listDevices
      summary: Enrolled devices
      responses:
        "200":
          description: Devices (including revoked ones, with `revoked_at`).
          content:
            application/json:
              schema:
                type: object
                properties:
                  devices:
                    type: array
                    items: { $ref: "#/components/schemas/Device" }
    post:
      tags: [devices]
      operationId: enrollDevice
      summary: "Enroll a device directly by its id (retired: 410)"
      deprecated: true
      description: >-
        subetha's own device enrollment is retired. Devices enroll at devicebasedauth.org for the
        audience `subetha.us`, and may then attach a signing key with `POST /v1/devices/dba`.
      security: []
      responses:
        "410":
          description: Retired; the body names the replacement and links the docs.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error: { const: unsupported }
                  http: { const: 410 }
                  message: { type: string }
                  docs: { type: string }
  /v1/devices/{id}:
    patch:
      tags: [devices]
      operationId: updateDevice
      summary: Rename, pause/resume (kill switch), or set transport allow/deny policies for an enrolled device
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, pattern: "^dev_[a-z2-7]{52}$" } }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string, maxLength: 64 }
                capabilities: { type: array, maxItems: 32, items: { $ref: "#/components/schemas/Transport" } }
                paused: { type: boolean, description: Device kill switch. Paused devices cannot sign requests until resumed. }
                allow_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
                deny_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
      responses:
        "200":
          description: Updated device.
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Device" }
        "400": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }
    delete:
      tags: [devices]
      operationId: revokeDevice
      parameters:
        - { name: id, in: path, required: true, schema: { type: string, pattern: "^dev_[a-z2-7]{52}$" } }
      responses:
        "200":
          description: Revoked.
          content:
            application/json:
              schema: { type: object, properties: { revoked: { const: true } } }
        "404": { $ref: "#/components/responses/Error" }
  /v1/devices/dba:
    post:
      tags: [devices]
      operationId: attachDeviceKey
      summary: Attach a signing key to a devicebasedauth.org device
      description: |
        A device enrolled at devicebasedauth.org (credential for `subetha.us`) attaches an Ed25519
        signing key, which E2EE topic keys can then be wrapped to and which signs requests
        (`deviceSignature`) and messages. Needs grants for `subetha:publish` and
        `subetha:subscribe` on every topic (the key signs for all of them). The device proves it
        holds the key by signing `"subetha-enroll-v1\n" + device_id + "\n" + time` (SDK
        `enrollmentProof(key)`). Revoking the devicebasedauth.org credential also stops the key.
      security: [{ dbaCredential: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [device_id, time, proof]
              properties:
                device_id: { type: string, pattern: "^dev_[a-z2-7]{52}$", description: The key's Ed25519 identity. }
                time: { type: integer, description: Unix seconds (±300 s). }
                proof: { type: string, description: base64url Ed25519 signature. }
                name: { type: string, maxLength: 64 }
                capabilities: { type: array, maxItems: 32, items: { $ref: "#/components/schemas/Transport" } }
      responses:
        "201":
          description: Attached.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: string }
                  enrolled: { const: true }
                  dba_device: { type: string, description: "The devicebasedauth.org device, as `dba:<id>`." }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
  /v1/device/code:
    post:
      tags: [devices]
      operationId: deviceCode
      summary: "Device flow, step 1 (retired: 410)"
      deprecated: true
      description: >-
        subetha's own device enrollment is retired. Devices enroll at devicebasedauth.org for the
        audience `subetha.us`, and may then attach a signing key with `POST /v1/devices/dba`.
      security: []
      responses:
        "410":
          description: Retired; the body names the replacement and links the docs.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error: { const: unsupported }
                  http: { const: 410 }
                  message: { type: string }
                  docs: { type: string }
  /v1/device/approve:
    post:
      tags: [devices]
      operationId: deviceApprove
      summary: "Device flow, step 2 (retired: 410)"
      deprecated: true
      description: >-
        subetha's own device enrollment is retired. Devices enroll at devicebasedauth.org for the
        audience `subetha.us`, and may then attach a signing key with `POST /v1/devices/dba`.
      security: []
      responses:
        "410":
          description: Retired; the body names the replacement and links the docs.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error: { const: unsupported }
                  http: { const: 410 }
                  message: { type: string }
                  docs: { type: string }
  /v1/device/token:
    post:
      tags: [devices]
      operationId: deviceToken
      summary: "Device flow, step 3 (retired: 410)"
      deprecated: true
      description: >-
        subetha's own device enrollment is retired. Devices enroll at devicebasedauth.org for the
        audience `subetha.us`, and may then attach a signing key with `POST /v1/devices/dba`.
      security: []
      responses:
        "410":
          description: Retired; the body names the replacement and links the docs.
          content:
            application/json:
              schema:
                type: object
                properties:
                  error: { const: unsupported }
                  http: { const: 410 }
                  message: { type: string }
                  docs: { type: string }
  /v1/dba/link:
    get:
      tags: [devices]
      operationId: dbaLink
      summary: The account's devicebasedauth.org link (admin)
      responses:
        "200":
          description: Link state.
          content:
            application/json:
              schema:
                type: object
                properties:
                  linked: { type: boolean }
                  dba_account: { type: [string, "null"] }
                  linked_at: { type: [integer, "null"], description: Unix milliseconds. }
                  issuer: { type: string, format: uri }
                  audience: { const: subetha.us }
    delete:
      tags: [devices]
      operationId: unlinkDba
      summary: Unlink devicebasedauth.org (admin)
      description: Its devices stop working here (within a minute everywhere).
      responses:
        "200":
          description: Unlinked.
          content:
            application/json:
              schema: { type: object, properties: { linked: { const: false } } }
        "404": { $ref: "#/components/responses/Error" }
  /v1/dba/link/start:
    post:
      tags: [devices]
      operationId: startDbaLink
      summary: Start linking a devicebasedauth.org account (admin)
      description: >-
        Returns the devicebasedauth.org sign-in URL (OpenID Connect, authorization code with PKCE).
        After sign-in it returns to `/auth/dba/callback`. Attempts last 10 minutes; at most 10 at
        once per account.
      requestBody:
        content:
          application/json:
            schema: { type: object }
      responses:
        "200":
          description: Sign-in URL.
          content:
            application/json:
              schema:
                type: object
                properties:
                  url: { type: string, format: uri }
                  expires_in: { type: integer }
        "429": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }
  /v1/dba/link/finish:
    post:
      tags: [devices]
      operationId: finishDbaLink
      summary: Finish linking with the callback's code and state (admin)
      description: >-
        Single use. One devicebasedauth.org account links to at most one subetha account; linking
        another replaces this account's previous link.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [state]
              properties:
                code: { type: string }
                state: { type: string }
                error: { type: string, description: The callback's error, when sign-in was not completed. }
      responses:
        "200":
          description: Linked.
          content:
            application/json:
              schema:
                type: object
                properties:
                  linked: { const: true }
                  dba_account: { type: string }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "409": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }
  /v1/dba/signin/start:
    post:
      tags: [account]
      operationId: startDbaSignin
      summary: Start signing in with devicebasedauth.org
      description: >-
        Returns the devicebasedauth.org authorization URL (OpenID Connect, PKCE S256) to send the
        browser to. It comes back through `/auth/dba/callback`. Keep the URL's `state` and finish
        only a sign-in that this browser started.
      security: []
      responses:
        "200":
          description: Where to send the browser.
          content:
            application/json:
              schema:
                type: object
                properties:
                  url: { type: string, format: uri }
                  expires_in: { type: integer, description: Seconds this attempt stays valid. }
        "429": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }
  /v1/dba/signin/finish:
    post:
      tags: [account]
      operationId: finishDbaSignin
      summary: Finish signing in with devicebasedauth.org
      description: >-
        Single use. Signs in to the subetha account that the devicebasedauth.org account is linked
        to. A devicebasedauth.org account seen for the first time gets a new linked account with
        no handle; set one with `PATCH /v1/account`. Returns an API token with the `admin` scope
        that expires in 30 days. It is listed under tokens and can be revoked there.
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [state]
              properties:
                code: { type: string }
                state: { type: string }
                error: { type: string, description: The callback's error, when sign-in was not completed. }
      responses:
        "200":
          description: Signed in.
          content:
            application/json:
              schema:
                type: object
                properties:
                  token: { type: string }
                  expires_at: { type: integer, description: Unix milliseconds. }
                  account_id: { type: string }
                  dba_account: { type: string }
                  created: { type: boolean, description: True when this sign-in created the account. }
        "400": { $ref: "#/components/responses/Error" }
        "401": { $ref: "#/components/responses/Error" }
        "503": { $ref: "#/components/responses/Error" }
  /auth/dba/callback:
    get:
      tags: [devices]
      operationId: dbaCallback
      summary: devicebasedauth.org sign-in returns here
      description: Redirects to the console with `code` and `state` in the URL fragment. The console posts them to `/v1/dba/signin/finish` when the state belongs to a sign-in, and otherwise to `/v1/dba/link/finish`.
      security: []
      parameters:
        - { name: code, in: query, schema: { type: string } }
        - { name: state, in: query, schema: { type: string } }
        - { name: error, in: query, schema: { type: string } }
      responses:
        "302": { description: "To `/console/#dba-callback=1&code=…&state=…`." }
  /mcp:
    get:
      tags: [mcp]
      operationId: mcpGet
      summary: Not supported (no server-initiated streams)
      responses:
        "405":
          description: Use POST.
    post:
      tags: [mcp]
      operationId: mcp
      summary: Remote MCP server (Streamable HTTP, JSON-RPC 2.0, JSON responses)
      description: |
        Methods: `initialize`, `ping`, `tools/list`, `tools/call`, and notifications (202, no body).
        Batches are accepted. Tools: `publish`, `poll`, `wait_for_message` (long-poll, up to 50 s),
        `list_topics`, `create_topic`, `estimate_cost`. Tools call the public API in-process with
        the caller's credentials, so scopes, policies, and prepaid charges apply unchanged.
        Without `Authorization` the response is 401 with a `WWW-Authenticate` header pointing at
        `/.well-known/oauth-protected-resource`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: "#/components/schemas/JsonRpcRequest"
                - type: array
                  items: { $ref: "#/components/schemas/JsonRpcRequest" }
      responses:
        "200":
          description: JSON-RPC response(s).
          content:
            application/json:
              schema: { type: object, additionalProperties: true }
        "202":
          description: Notification accepted.
        "400":
          description: JSON-RPC parse error.
        "401":
          description: Authentication required.
  /.well-known/oauth-protected-resource:
    get:
      tags: [mcp]
      operationId: oauthProtectedResource
      summary: OAuth 2.0 protected resource metadata (RFC 9728) for the MCP server
      security: []
      responses:
        "200":
          description: Metadata.
          content:
            application/json:
              schema:
                type: object
                properties:
                  resource: { type: string, format: uri }
                  authorization_servers: { type: array, items: { type: string, format: uri } }
                  bearer_methods_supported: { type: array, items: { type: string } }
                  resource_documentation: { type: string, format: uri }

  /.well-known/oauth-protected-resource/mcp:
    get:
      tags: [mcp]
      operationId: oauthProtectedResourceMcp
      summary: Path-scoped OAuth 2.0 protected resource metadata (RFC 9728) for /mcp
      security: []
      responses:
        "200":
          description: Metadata.
          content:
            application/json:
              schema:
                type: object
                properties:
                  resource: { type: string, format: uri }
                  authorization_servers: { type: array, items: { type: string, format: uri } }
                  bearer_methods_supported: { type: array, items: { type: string } }
                  resource_documentation: { type: string, format: uri }

  # ------------------------------------------------------------------ attachments
  /file/{id}/{name}:
    get:
      tags: [publish]
      operationId: downloadAttachment
      summary: Download an attachment
      description: |
        Attachment URLs are returned in `attachment.url` when a file is published with
        `X-Filename`. Anyone with the URL can download until the message expires; each download is
        charged to the topic owner as one API request. Images, audio, video, plain text, and PDF are
        served inline; everything else as a download. `/file/{id}` (without the name) also works.
      security: []
      parameters:
        - { name: id, in: path, required: true, schema: { type: string } }
        - { name: name, in: path, required: true, schema: { type: string } }
      responses:
        "200":
          description: The file.
          content:
            application/octet-stream:
              schema: { type: string, format: binary }
        "402": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ hooks
  /v1/hooks/stripe:
    post:
      tags: [hooks]
      operationId: stripeWebhook
      summary: Stripe events (checkout completion, auto-recharge payment intents, refunds, disputes)
      security: []
      parameters:
        - { name: Stripe-Signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, additionalProperties: true }
      responses:
        "200":
          description: Received (idempotent by event id).
        "401": { $ref: "#/components/responses/Error" }
  /v1/hooks/twilio/sms:
    post:
      tags: [hooks]
      operationId: twilioInboundSms
      summary: Inbound SMS/MMS or WhatsApp (STOP, ACK <id>, or publish to the bound topic)
      security: []
      parameters:
        - { name: X-Twilio-Signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema: { type: object, additionalProperties: { type: string } }
      responses:
        "200":
          description: TwiML.
          content:
            text/xml:
              schema: { type: string }
        "401": { $ref: "#/components/responses/Error" }
  /v1/hooks/twilio/whatsapp:
    post:
      tags: [hooks]
      operationId: twilioInboundWhatsapp
      summary: Inbound WhatsApp via Twilio (STOP, ACK <id>, or publish to the bound topic)
      security: []
      parameters:
        - { name: X-Twilio-Signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema: { type: object, additionalProperties: { type: string } }
      responses:
        "200":
          description: TwiML.
          content:
            text/xml:
              schema: { type: string }
        "401": { $ref: "#/components/responses/Error" }
  /v1/hooks/twilio/voice-ack:
    post:
      tags: [hooks]
      operationId: twilioVoiceAck
      summary: DTMF acknowledgement from a voice call (press 1)
      security: []
      parameters:
        - { name: topic, in: query, required: true, schema: { type: string } }
        - { name: id, in: query, required: true, schema: { type: string } }
        - { name: t, in: query, required: true, schema: { type: string }, description: Ack token bound to topic and id. }
        - { name: X-Twilio-Signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema: { type: object, additionalProperties: { type: string } }
      responses:
        "200":
          description: TwiML.
          content:
            text/xml:
              schema: { type: string }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
  /v1/hooks/twilio/voice-status:
    post:
      tags: [hooks]
      operationId: twilioVoiceStatus
      summary: Call status callback (settles a voice delivery to its actual duration)
      security: []
      parameters:
        - { name: acct, in: query, required: true, schema: { type: string } }
        - { name: ref, in: query, required: true, schema: { type: string } }
        - { name: max, in: query, required: true, schema: { type: integer } }
        - { name: t, in: query, required: true, schema: { type: string }, description: Token bound to acct, ref, and max. }
        - { name: X-Twilio-Signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema: { type: object, additionalProperties: { type: string } }
      responses:
        "200":
          description: TwiML.
          content:
            text/xml:
              schema: { type: string }
        "401": { $ref: "#/components/responses/Error" }
        "403": { $ref: "#/components/responses/Error" }
  /v1/hooks/slack/events:
    post:
      tags: [hooks]
      operationId: slackEvents
      summary: Slack Events API (url_verification and channel messages)
      security: []
      parameters:
        - { name: X-Slack-Signature, in: header, required: true, schema: { type: string } }
        - { name: X-Slack-Request-Timestamp, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, additionalProperties: true }
      responses:
        "200":
          description: OK (or the url_verification challenge).
        "401": { $ref: "#/components/responses/Error" }
  /v1/hooks/clerk:
    post:
      tags: [hooks]
      operationId: clerkWebhook
      summary: Clerk lifecycle webhook (user.updated email sync; user.deleted and organization.deleted pause account and revoke credentials)
      security: []
      parameters:
        - { name: svix-id, in: header, required: true, schema: { type: string } }
        - { name: svix-timestamp, in: header, required: true, schema: { type: string } }
        - { name: svix-signature, in: header, required: true, schema: { type: string } }
      requestBody:
        required: true
        content:
          application/json:
            schema: { type: object, additionalProperties: true }
      responses:
        "200":
          description: Received (idempotent by svix-id).
        "401": { $ref: "#/components/responses/Error" }

  # ------------------------------------------------------------------ dev
  /v1/dev/bootstrap:
    post:
      tags: [dev]
      operationId: devBootstrap
      summary: Create a funded account and admin token (local only)
      description: Only when `ENVIRONMENT=local` and `DEV_BOOTSTRAP_SECRET` is set; 404 elsewhere.
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [secret]
              properties:
                secret: { type: string, examples: [local-dev-only] }
                handle: { type: string }
                credit_usd: { type: number }
      responses:
        "201":
          description: Account and token.
          content:
            application/json:
              schema:
                type: object
                properties:
                  account_id: { type: string }
                  handle: { type: [string, "null"] }
                  token: { type: string }
        "401": { $ref: "#/components/responses/Error" }
        "404": { $ref: "#/components/responses/Error" }

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: |
        An API token (`se_…`), a Clerk session JWT, or an OAuth access token from the
        authorization server named in `/.well-known/oauth-protected-resource` (JWT access tokens
        per RFC 9068, or opaque tokens checked by RFC 7662 introspection). OAuth access tokens
        get publish + subscribe only, under the account's `oauth_daily_cap_micros` per client.
    basicAuth:
      type: http
      scheme: basic
      description: For ntfy clients. Empty username, the token as the password.
    dbaCredential:
      type: http
      scheme: dba
      description: |
        A devicebasedauth.org device credential for the audience `subetha.us`:
        `Authorization: DBA <credential>` plus `DPoP: <proof>` signed by the device's key for this
        method and URL (the DBA SDKs and `dba headers`/`dba curl` make both), or
        `Authorization: Bearer <dba-access+jwt>`. Verified offline (issuer signature, revocation
        status list, proof). The device's devicebasedauth.org account must be linked
        (`/v1/dba/link`). Grants: `subetha:publish` and `subetha:subscribe`, with topic names or
        `@handle/*` as resources. Such a device is listed as `dba:<id>` and cannot manage the account.
    deviceSignature:
      type: apiKey
      in: header
      name: X-Device-Id
      description: |
        Devices with a signing key (attached with `POST /v1/devices/dba`) sign each request instead of sending a token. Headers:
        `X-Device-Id: dev_…`, `X-Device-Time: <unix seconds, ±300 s>`,
        `X-Device-Nonce: <16–64 random base64url chars, single use>`,
        `X-Device-Signed-Headers: x-device-id,x-device-time,x-device-nonce,<content and routing headers>`, and
        `X-Device-Signature: base64url(Ed25519(key, "subetha-req-v2\n" + METHOD + "\n" + host + "\n" + path?query + "\n" + hex(sha256(body)) + ("\n" + name + ":" + value) per signed header))`.
        Content and routing headers (X-Title, X-Priority, X-Route-Mode, X-Max-Cost, Content-Type,
        Idempotency-Key, … ; spec §8.1) must be signed or the request is rejected.
        A replayed signature is rejected, and the host binds it to one deployment.
        Devices can publish and subscribe but not manage the account. The SDK does this with
        `new SubEtha({ deviceKey, signRequests: true })`.
    authQuery:
      type: apiKey
      in: query
      name: auth
      description: ntfy-style `?auth=<base64url("Bearer <token>")>`, used by browsers for WebSockets.

  parameters:
    Topic:
      name: topic
      in: path
      required: true
      description: "`@handle/name` or `t_<26 base32>` (the slash is literal; `%2F` also works)."
      schema:
        type: string
        pattern: "^(@[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?/[A-Za-z0-9_-]{1,64}|t_[a-z2-7]{26})$"
      examples:
        namespaced: { value: "@alice/doorbell" }
        secret: { value: t_mfrggzdfmztwq2lknnwg23tpoa }
    Topics:
      name: topics
      in: path
      required: true
      description: One topic, or several separated by commas (not for `ws`).
      schema: { type: string }
      examples:
        one: { value: "@alice/doorbell" }
        several: { value: "@alice/doorbell,@alice/backups" }
    Since:
      name: since
      in: query
      description: Replay cached messages first. A message id (exclusive), Unix seconds, a duration (`10m`, `2h`, `1d`), or `all`.
      schema: { type: string, examples: [all, 10m, "1763750000", 01JD0000000000000000000000] }
    Poll:
      name: poll
      in: query
      description: "`1` returns cached messages and closes."
      schema: { enum: ["1", "true", "yes"] }
    PriorityFilter:
      name: priority
      in: query
      description: Only these priorities (numbers or names, comma-separated). Aliases `prio`, `p`.
      schema: { type: string, examples: ["4,5", "high,urgent"] }
    TagsFilter:
      name: tags
      in: query
      description: Only messages carrying all these tags. Aliases `tag`, `ta`.
      schema: { type: string, examples: ["door,warning"] }
    Scheduled:
      name: scheduled
      in: query
      description: Accepted for ntfy compatibility; scheduled messages are not supported in v0.
      schema: { type: string }
    XTitle:
      name: X-Title
      in: header
      description: Message title. Aliases `Title`, `t`; query `?title=`. RFC 2047 encoded words are decoded.
      schema: { type: string, maxLength: 256 }
    XPriority:
      name: X-Priority
      in: header
      description: 1–5 or `min|low|default|high|max|urgent`. Aliases `Priority`, `prio`, `p`; query `?priority=`.
      schema: { type: string, examples: ["4", high] }
    XTags:
      name: X-Tags
      in: header
      description: Comma-separated tags. Aliases `Tags`, `Tag`, `ta`; query `?tags=`.
      schema: { type: string }
    XClick:
      name: X-Click
      in: header
      description: URL opened when the notification is clicked. Alias `Click`.
      schema: { type: string }
    XIcon:
      name: X-Icon
      in: header
      description: Notification icon URL. Alias `Icon`.
      schema: { type: string }
    XActions:
      name: X-Actions
      in: header
      description: "ntfy action buttons: `view, Open, https://…; http, Close, https://…, clear=true` or a JSON array. Alias `Actions`."
      schema: { type: string }
    XAttach:
      name: X-Attach
      in: header
      description: URL of an attachment. Aliases `Attach`, `a`.
      schema: { type: string }
    XMarkdown:
      name: X-Markdown
      in: header
      description: "`yes|true|1` sets `content_type: text/markdown`. Aliases `Markdown`, `md`."
      schema: { type: string }
    XExpires:
      name: X-Expires
      in: header
      description: Unix seconds; capped by the topic's retention.
      schema: { type: integer }
    XFilename:
      name: X-Filename
      in: header
      description: |
        Upload the request body as an attachment with this file name (up to 15 MB; aliases
        `Filename`, `f`). The file is stored in R2 until the message expires and `attachment.url`
        points at `/file/{id}/{name}`. The publisher pays `attachment_upload` plus storage.
        Put the message text in `X-Message`.
      schema: { type: string, maxLength: 200 }
    XMessage:
      name: X-Message
      in: header
      description: Message text when the body is an attachment or empty. Aliases `Message`, `m`; query `?message=`.
      schema: { type: string }
    XDelay:
      name: X-Delay
      in: header
      description: Scheduled delivery is not supported in v0 and is rejected (`unsupported`). Also `Delay`, `X-At`, `X-In`.
      schema: { type: string }
    XRouteMode:
      name: X-Route-Mode
      in: header
      description: "`any` (cheapest viable path, escalate if unacked), `fast`, `all` (every allowed transport), `cheap` (free transports only)."
      schema: { enum: [any, fast, all, cheap], default: any }
    XMaxCost:
      name: X-Max-Cost
      in: header
      description: Maximum delivery cost for this message, in USD (decimal). Deliveries that would exceed it are dropped from the plan.
      schema: { type: string, examples: ["0.02"] }
    XDeny:
      name: X-Deny
      in: header
      description: Comma-separated transports never to use for this message.
      schema: { type: string, examples: ["sms,voice"] }
    XAllow:
      name: X-Allow
      in: header
      description: Comma-separated transports that may be used (all others are denied).
      schema: { type: string, examples: ["ws,webpush"] }
    XDeadline:
      name: X-Deadline
      in: header
      description: Delivery deadline in seconds (≥ 1). Escalation steps scheduled at or after this deadline are dropped from the plan, and queued jobs past the deadline are skipped with their reservations released.
      schema: { type: integer, minimum: 1 }
    XContentType:
      name: X-Content-Type
      in: header
      description: Payload MIME type (`text/plain`, `text/markdown`, `application/json`, or `application/octet-stream`).
      schema: { enum: ["text/plain", "text/markdown", "application/json", "application/octet-stream"] }
    XHops:
      name: X-Hops
      in: header
      description: Remaining edge-relay hop count (0..16), decremented by edge relays to stop loops.
      schema: { type: integer, minimum: 0, maximum: 16 }
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      description: |
        Retrying with the same key returns the original message (`X-Duplicate: 1`) without
        delivering or charging again. Reusing a key with a different body is `conflict` (409).
      schema: { type: string, maxLength: 256 }
    XDryRun:
      name: X-Dry-Run
      in: header
      description: "`1` returns the route plan and cost estimate; nothing is delivered (charged as one API request)."
      schema: { enum: ["1", "true", "yes"] }
    XSender:
      name: X-Sender
      in: header
      description: Signer's device id (`dev_…`), for header-signed messages.
      schema: { type: string }
    XSignature:
      name: X-Signature
      in: header
      description: "`ed25519:<base64url>` over the canonical envelope (spec §4). Requires `X-Message-Id` and `X-Time`."
      schema: { type: string }
    XMessageId:
      name: X-Message-Id
      in: header
      description: Sender-chosen ULID.
      schema: { type: string }
    XTime:
      name: X-Time
      in: header
      description: Sender-chosen Unix seconds.
      schema: { type: integer }
    MessageQuery:
      name: message
      in: query
      description: Message text when the body is empty. Alias `m`.
      schema: { type: string }
    TitleQuery:
      name: title
      in: query
      schema: { type: string }
    PriorityQuery:
      name: priority
      in: query
      schema: { type: string }
    TagsQuery:
      name: tags
      in: query
      schema: { type: string }

  headers:
    XCostMicros:
      description: µ$ charged to the caller for this request (or estimated, for dry runs).
      schema: { type: integer }
    XMessageId:
      description: The message id.
      schema: { type: string }
    XDuplicate:
      description: "`1` when an idempotency key matched an earlier publish."
      schema: { type: string }

  responses:
    Published:
      description: The stored envelope, or a dry-run estimate when `X-Dry-Run` was set.
      headers:
        X-Cost-Micros: { $ref: "#/components/headers/XCostMicros" }
        X-Message-Id: { $ref: "#/components/headers/XMessageId" }
        X-Duplicate: { $ref: "#/components/headers/XDuplicate" }
      content:
        application/json:
          schema:
            oneOf:
              - $ref: "#/components/schemas/Envelope"
              - $ref: "#/components/schemas/DryRun"
    Error:
      description: Error (see `Error.error` for the stable code).
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    RateLimited:
      description: Rate limited.
      headers:
        Retry-After:
          schema: { type: integer }
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }

  schemas:
    Transport:
      enum: [ws, sse, poll, webpush, apns, fcm, webhook, email, slack, sms, voice, whatsapp, mcp, lxmf, ble, audio, qr, lan]
      description: Transport ids (spec §10). `ble`, `audio`, `qr`, and `lan` are edge transports and never touch the relay.

    Envelope:
      type: object
      description: |
        The message envelope (spec §2), a superset of ntfy's message JSON. Mirrors
        `spec/schema/message.schema.json`. Unknown fields are preserved by relays.
      required: [topic]
      additionalProperties: true
      properties:
        id: { type: string, pattern: "^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$", description: ULID; the dedupe key across transports. }
        time: { type: integer, minimum: 0, description: Unix seconds. }
        expires: { type: integer, minimum: 0, description: Unix seconds. }
        event: { enum: [message, open, keepalive, poll_request] }
        topic:
          type: string
          pattern: "^(@[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?/[A-Za-z0-9_-]{1,64}|t_[a-z2-7]{26})$"
        title: { type: string, maxLength: 256 }
        message: { type: string, maxLength: 4096 }
        priority: { type: integer, minimum: 1, maximum: 5, description: 3 is the default and is omitted. }
        tags: { type: array, items: { type: string, maxLength: 64 }, maxItems: 16 }
        click: { type: string, maxLength: 2048 }
        icon: { type: string, maxLength: 2048 }
        actions:
          type: array
          maxItems: 3
          items:
            type: object
            required: [action, label]
            properties:
              action: { enum: [view, http, broadcast] }
              label: { type: string }
              url: { type: string }
              clear: { type: boolean }
        attachment:
          type: object
          required: [name, url]
          properties:
            name: { type: string }
            url: { type: string }
            size: { type: integer, minimum: 0 }
            type: { type: string }
            expires: { type: integer }
        content_type: { enum: [text/plain, text/markdown, application/json, application/octet-stream] }
        sender: { type: string, pattern: "^dev_[a-z2-7]{52}$", description: Signer's device id. }
        sig: { type: string, pattern: "^ed25519:[A-Za-z0-9_-]{86}$" }
        enc:
          type: object
          required: [alg, kid, nonce]
          description: Present when protected fields are end-to-end encrypted (spec §5).
          properties:
            alg: { const: xchacha20poly1305 }
            kid: { type: string, maxLength: 64 }
            nonce: { type: string, pattern: "^[A-Za-z0-9_-]{32}$" }
        route:
          type: object
          properties:
            mode: { enum: [any, fast, all, cheap] }
            max_cost_usd_micros: { type: integer, minimum: 0 }
            deny: { type: array, items: { $ref: "#/components/schemas/Transport" } }
            allow: { type: array, items: { $ref: "#/components/schemas/Transport" } }
            deadline_s: { type: integer, minimum: 1 }
        hops: { type: integer, minimum: 0, maximum: 16, description: Remaining edge-relay hops. }

    DryRun:
      type: object
      properties:
        dry_run: { const: true }
        estimate_usd_micros: { type: integer, description: Worst-case cost if published now. }
        plan:
          type: array
          items: { $ref: "#/components/schemas/PlannedDelivery" }
        message: { $ref: "#/components/schemas/Envelope" }

    PlannedDelivery:
      type: object
      properties:
        subscriber: { type: string, description: Opaque subscriber id. }
        transport: { $ref: "#/components/schemas/Transport" }
        sku: { type: [string, "null"] }
        cost_micros: { type: integer }
        after_s: { type: integer, description: Escalation delay; 0 = immediately. }

    Error:
      type: object
      required: [error, http, message]
      properties:
        error:
          description: Stable machine-readable code (spec/errors.json).
          enum:
            - bad_request
            - invalid_topic
            - invalid_message
            - message_too_large
            - unsupported
            - unauthorized
            - signature_required
            - bad_signature
            - insufficient_balance
            - no_funded_owner
            - forbidden
            - spend_cap_exceeded
            - transport_denied
            - topic_paused
            - not_found
            - conflict
            - rate_limited
            - internal
            - provider_unavailable
        http: { type: integer }
        message: { type: string }
        shortfall_usd_micros: { type: integer, description: "On `insufficient_balance`: how much more is needed." }
        payer: { type: string, description: "`topic_owner` when the owner's balance, not the caller's, was short." }
      additionalProperties: true
      example: { error: insufficient_balance, http: 402, message: "balance cannot cover the worst-case cost", shortfall_usd_micros: 1234 }

    Account:
      type: object
      properties:
        id: { type: string }
        handle: { type: [string, "null"] }
        email: { type: [string, "null"] }
        tier: { type: string, description: Markup tier from trailing-12-month top-ups. }
        balance_usd_micros: { type: integer }
        reserved_usd_micros: { type: integer }
        daily_cap_micros: { type: [integer, "null"], description: Account-wide daily spend cap. }
        monthly_cap_micros: { type: [integer, "null"], description: Account-wide monthly spend cap (UTC calendar month). }
        allow_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" }, description: Account-wide transport allow-list (`null` = all). }
        deny_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" }, description: Account-wide transport deny-list (`null` = none). }
        oauth_daily_cap_micros: { type: [integer, "null"], description: "Daily cap per OAuth client; null = the $1 default, 0 = OAuth off." }
        telephony_countries: { type: array, items: { type: string }, description: "Opt-in countries outside the US for SMS/voice endpoints." }
        telephony_circuit: { type: [string, "null"], description: "Set when global or per-account SMS/voice circuit breaker is open." }
        has_saved_card: { type: boolean, description: "Whether a Stripe card is saved on file for auto-recharge." }
        auto_recharge:
          description: "Configured off-session top-up rule, or null when disabled."
          oneOf:
            - type: "null"
            - type: object
              properties:
                threshold_micros: { type: integer }
                amount_micros: { type: integer }
        auto_recharge_last_error: { type: [string, "null"], description: "Last off-session auto-recharge failure reason, cleared when auto-recharge is re-enabled or a top-up succeeds." }
        paused: { type: boolean }
        created_at: { type: integer }

    WrappedKey:
      type: object
      description: An E2EE topic key sealed to one device (spec §5.1). Uploads must have exactly these fields.
      required: [alg, topic, kid, to, epk, nonce, ct]
      properties:
        alg: { const: x25519-xchacha20poly1305 }
        topic: { type: string }
        kid: { type: string, pattern: "^[A-Za-z0-9_.-]{1,64}$" }
        to: { type: string, pattern: "^dev_[a-z2-7]{52}$" }
        epk: { type: string, pattern: "^[A-Za-z0-9_-]{43}$" }
        nonce: { type: string, pattern: "^[A-Za-z0-9_-]{32}$" }
        ct: { type: string, pattern: "^[A-Za-z0-9_-]{64}$" }

    Payment:
      type: object
      properties:
        id: { type: string, description: Stripe payment intent id. }
        kind: { type: string, enum: [checkout, auto_recharge] }
        amount_micros: { type: integer }
        fee_micros: { type: integer }
        credit_micros: { type: integer }
        refunded_micros: { type: integer }
        disputed: { type: boolean }
        created_at: { type: integer }

    Token:
      type: object
      properties:
        id: { type: string }
        name: { type: string }
        scopes: { type: array, items: { type: string } }
        transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
        deny_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
        daily_cap_micros: { type: [integer, "null"] }
        total_cap_micros: { type: [integer, "null"] }
        origins: { type: [array, "null"], items: { type: string } }
        ips: { type: [array, "null"], items: { type: string } }
        expires_at: { type: [integer, "null"] }
        created_at: { type: integer }
        revoked_at: { type: [integer, "null"] }

    TopicPolicy:
      type: object
      properties:
        public_publish: { type: boolean, description: Anyone may publish (charged to the owner, rate-limited and capped). }
        public_read: { type: boolean, description: Anyone may subscribe to a namespaced topic. Secret topics are readable by name. }
        anon_rate_per_min: { type: integer, default: 60 }
        anon_daily_cap_micros: { type: integer, default: 1000000 }
        rate_per_min: { type: integer, minimum: 1, description: Topic-wide publish rate limit (messages per minute, token bucket). }
        burst: { type: integer, minimum: 1, description: Token-bucket burst size for `rate_per_min` (defaults to `rate_per_min`). }
        require_signature: { type: boolean }
        allowed_publishers: { type: array, items: { type: string } }
        allowed_readers: { type: array, items: { type: string } }
        allow_transports: { type: array, items: { $ref: "#/components/schemas/Transport" } }
        deny_transports: { type: array, items: { $ref: "#/components/schemas/Transport" } }
        max_cost_per_message_micros: { type: integer }
        daily_cap_micros: { type: integer }
        monthly_cap_micros: { type: integer }
        retention_s: { type: integer }
        paused: { type: boolean }
        min_priority:
          type: object
          additionalProperties: { type: integer, minimum: 1, maximum: 5 }
          description: "Minimum priority per transport, e.g. `{\"sms\": 4}`."
        email_senders: { type: array, items: { type: string }, description: Inbound email senders allowed to publish (`*` = anyone). }
        quiet_hours:
          type: object
          properties:
            start: { type: string, pattern: "^([01]\\d|2[0-3]):[0-5]\\d$" }
            end: { type: string, pattern: "^([01]\\d|2[0-3]):[0-5]\\d$" }
            tz: { type: string, description: "Optional IANA timezone (e.g. `America/Chicago`). Defaults to `UTC`." }

    TopicMeta:
      type: object
      properties:
        name: { type: string }
        owner: { type: string }
        policy: { $ref: "#/components/schemas/TopicPolicy" }
        created_at: { type: integer }
        tier: { type: string }
        subscribers:
          type: object
          description: Aggregate subscriber counts (live streams/WebSockets and verified queued endpoints).
          properties:
            live: { type: integer }
            queued: { type: integer }

    Endpoint:
      type: object
      properties:
        id: { type: string, examples: [ep_01JD0000000000000000000000] }
        kind: { $ref: "#/components/schemas/Transport" }
        address: { type: string, description: Masked. }
        verified: { type: boolean }
        created_at: { type: integer }
        name: { type: string }
        inbound_topic: { type: string, description: "SMS endpoints: the topic that texts from this number publish to." }
        credential_id: { type: string, description: "APNs/FCM endpoints: pinned BYO push credential id." }

    PushCredential:
      type: object
      properties:
        id: { type: string, examples: [pcred_01JD0000000000000000000000] }
        kind: { enum: [apns, fcm] }
        name: { type: string }
        is_default: { type: boolean }
        created_at: { type: integer }
        key_id: { type: string, description: "APNs only." }
        team_id: { type: string, description: "APNs only." }
        topic: { type: string, description: "APNs only (bundle id)." }
        sandbox: { type: boolean, description: "APNs only." }
        project_id: { type: string, description: "FCM only." }
        client_email: { type: string, description: "FCM only." }

    SubscriptionFilter:
      type: object
      properties:
        priority: { type: array, items: { type: integer, minimum: 1, maximum: 5 } }
        tags: { type: array, items: { type: string } }

    Subscription:
      type: object
      properties:
        id: { type: string }
        topic: { type: string }
        endpoint_id: { type: string }
        kind: { $ref: "#/components/schemas/Transport" }
        filter: { $ref: "#/components/schemas/SubscriptionFilter" }
        after_s: { type: integer }
        min_priority: { type: [integer, "null"] }
        paused: { type: boolean }
        created_at: { type: integer }

    Device:
      type: object
      properties:
        id: { type: string, pattern: "^dev_[a-z2-7]{52}$" }
        name: { type: string }
        capabilities: { type: array, items: { type: string } }
        paused: { type: boolean }
        allow_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
        deny_transports: { type: [array, "null"], items: { $ref: "#/components/schemas/Transport" } }
        created_at: { type: integer }
        last_seen_at: { type: [integer, "null"] }
        revoked_at: { type: [integer, "null"] }

    JsonRpcRequest:
      type: object
      required: [jsonrpc, method]
      properties:
        jsonrpc: { const: "2.0" }
        id: { type: [string, integer, "null"] }
        method: { type: string, examples: [initialize, tools/list, tools/call] }
        params: { type: object, additionalProperties: true }

    Status:
      type: object
      properties:
        ok: { type: boolean }
        environment: { enum: [local, staging, production] }
        components:
          type: object
          additionalProperties:
            type: object
            properties:
              ok: { type: boolean }
              ms: { type: integer }
              error: { type: string }
        transports:
          type: object
          description: Which relay transports are configured (true) on this deployment.
          additionalProperties: { type: boolean }
        telephony_circuit: { type: [string, "null"], description: Set when the SMS/voice circuit breaker has tripped. }
        time: { type: integer }
