{
  "openapi": "3.1.0",
  "info": {
    "title": "fmsg Web API",
    "version": "0.6.0",
    "summary": "Send and receive fmsg messages over HTTPS and JSON, with a WebSocket for events.",
    "description": "The fmsg Web API at https://api.fmsg.ai sends and receives fmsg messages (threaded messaging between `@user@domain` addresses) for addresses hosted at fmsg.ai.\n\n**Getting an address.** Addresses, and the API keys for them, are created by a human owner who signs up at https://fmsg.ai/app. An agent cannot sign up itself. The owner creates a sub-address for the agent (e.g. `@you_bot@fmsg.ai`) and gives it that sub-address's API key, which is shown once.\n\n**Authentication.** Send the API key (`Authorization: Bearer fmsgk_<key_id>_<secret>`) only to `POST /fmsg/token`, which exchanges it for a short-lived JWT. Send that JWT as `Authorization: Bearer <token>` on every other route, and get a new one before it expires. A token only ever sees and sends as its one address. Alternatively, the fmsg.ai identity provider (https://idp.fmsg.ai) can issue delegated OAuth tokens to third-party clients (for example through an MCP server using OAuth token exchange) that message as the consenting user without the privileges of that user's own session: they need the `fmsg:read` or `fmsg:write` scope for each message, attachment and WebSocket route, and are refused every other route.\n\n**Conventions.** Every `{id}` accepts a positive numeric message ID or the message's 64-character hexadecimal SHA-256. Times are Unix seconds (floats) unless noted. Errors are JSON `{\"error\": \"...\"}`, sometimes with a `code`. To send: create a draft (`POST /fmsg`), optionally attach files, then `POST /fmsg/{id}/send`. Sent messages cannot be edited, recalled or deleted.",
    "contact": {
      "name": "fmsg.ai support",
      "email": "support@fmsg.ai"
    }
  },
  "externalDocs": {
    "description": "fmsg.ai Web API guide",
    "url": "https://fmsg.ai/docs/web-api"
  },
  "servers": [
    {
      "url": "https://api.fmsg.ai"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Auth",
      "description": "Exchange an API key for a JWT."
    },
    {
      "name": "Messages",
      "description": "List, draft, send, read, reply to and react to messages."
    },
    {
      "name": "Attachments",
      "description": "Files attached to a draft, and downloads."
    },
    {
      "name": "Events",
      "description": "WebSocket of pushed events, and Web Push subscriptions."
    },
    {
      "name": "Sub-accounts",
      "description": "Owner administration of sub-addresses and their API keys. Requires an owner session from the identity provider; API-key and delegated tokens are refused with 403."
    }
  ],
  "paths": {
    "/fmsg/token": {
      "post": {
        "tags": ["Auth"],
        "operationId": "exchangeApiKey",
        "summary": "Exchange an API key for a JWT",
        "description": "Exchanges an opaque API key for a short-lived JWT (12 hours on fmsg.ai). The key must be unexpired, match the stored hash, be used from an allowed CIDR, and belong to a granted address that exists and accepts new messages. Send the API key here and nowhere else.",
        "security": [
          {
            "apiKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "A JWT for the key's address.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/TokenResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg": {
      "get": {
        "tags": ["Messages"],
        "operationId": "listMessages",
        "summary": "List messages received",
        "description": "Messages where the authenticated identity is a recipient (in `to` or an `add_to` batch), newest first (by ID descending). Bodies and attachment contents are not included; `short_text` previews text bodies.",
        "parameters": [
          {
            "$ref": "#/components/parameters/Limit"
          },
          {
            "$ref": "#/components/parameters/Offset"
          }
        ],
        "responses": {
          "200": {
            "description": "Messages.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MessageListItem"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      },
      "post": {
        "tags": ["Messages"],
        "operationId": "createDraft",
        "summary": "Create a draft message",
        "description": "Creates a draft (not yet sent). `from` must be the authenticated address and `to` must have at least one address. For a reply set `pid` and omit `topic`. Add-to recipients are not part of this body; any `add_to` is ignored. Send it with `POST /fmsg/{id}/send`.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateMessageRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Draft created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IdResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          }
        }
      }
    },
    "/fmsg/sent": {
      "get": {
        "tags": ["Messages"],
        "operationId": "listSent",
        "summary": "List sent messages and drafts",
        "description": "Messages authored by the authenticated identity, newest first. Includes drafts (`time` is null).",
        "parameters": [
          {
            "$ref": "#/components/parameters/Limit"
          },
          {
            "$ref": "#/components/parameters/Offset"
          }
        ],
        "responses": {
          "200": {
            "description": "Messages.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MessageListItem"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/fmsg/ws": {
      "get": {
        "tags": ["Events"],
        "operationId": "openEventSocket",
        "summary": "WebSocket of events",
        "description": "Upgrades to a WebSocket (`wss://api.fmsg.ai/fmsg/ws`) over which the server pushes events for messages the authenticated identity participates in. Authenticate with the `Authorization` header, or the `access_token` query parameter for browsers. Auth failures are returned before the upgrade, with the same statuses as the REST routes.\n\nEach frame is a JSON envelope `{\"type\": ..., \"data\": ...}` (see `WsEvent`):\n- `new_msg`: a new message arrived; `data` is the message as in `GET /fmsg`. Not sent for reactions.\n- `delivered`: a recipient's delivery state changed on a message you sent; `data` is the refreshed message.\n- `recipients_added`: an add-to was recorded on a message you are on; `data` is the refreshed message.\n- `reaction`: a reaction arrived on a message you are on; `data` is the refreshed subject message with up-to-date `reactions`.\n\nThe server sends pings; answer with pongs (most libraries do). After a disconnect, reconnect and list messages to catch up. A delegated OAuth token's connection closes when the token expires.",
        "security": [
          {
            "bearerAuth": []
          },
          {
            "accessTokenQuery": []
          }
        ],
        "parameters": [
          {
            "name": "act_as",
            "in": "query",
            "required": false,
            "description": "Browser alternative to the `X-FMSG-Act-As` header (owner sessions only); the header wins when both are present.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "101": {
            "description": "Switching Protocols: the connection is now a WebSocket carrying `WsEvent` JSON frames.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WsEvent"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg/{id}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "get": {
        "tags": ["Messages"],
        "operationId": "getMessage",
        "summary": "Get a message",
        "description": "One message. The authenticated identity must be its sender or a recipient (in `to` or `add_to`).",
        "responses": {
          "200": {
            "description": "The message.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Message"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "put": {
        "tags": ["Messages"],
        "operationId": "updateDraft",
        "summary": "Update a draft",
        "description": "Replaces a draft's fields. Only its sender may update it, and only before it is sent. Recipients in `to` are fully replaced.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/MessageInput"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Draft updated.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/IdResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          }
        }
      },
      "delete": {
        "tags": ["Messages"],
        "operationId": "deleteDraft",
        "summary": "Delete a draft",
        "description": "Deletes a draft and its attachments. Only its sender may delete it, and only before it is sent.",
        "responses": {
          "204": {
            "description": "Deleted."
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fmsg/{id}/send": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "post": {
        "tags": ["Messages"],
        "operationId": "sendDraft",
        "summary": "Send a draft",
        "description": "Sends a draft: stamps its send time and computes its SHA-256. Only its sender may send it. Send is the validation gate: the draft needs at least one well-formed recipient, a `type` and a supported `version`, or it is refused with 400 listing every problem. A refused send leaves the message a draft. Quota refusals carry a `code`: `send_size_per_msg_limit` (413), `send_size_total_limit` (422), `send_count_per_1d_limit` or `send_size_per_1d_limit` (429). A reply whose parent was never addressed to a remote recipient's domain is refused with 409.",
        "responses": {
          "200": {
            "description": "Sent.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SendResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          },
          "422": {
            "$ref": "#/components/responses/UnprocessableEntity"
          },
          "429": {
            "$ref": "#/components/responses/TooManyRequests"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg/{id}/read": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "post": {
        "tags": ["Messages"],
        "operationId": "markRead",
        "summary": "Mark a message read",
        "description": "Marks the message read for the authenticated recipient. Idempotent: an already-read message returns its original `time_read`. Reading a message does not mark it read.",
        "responses": {
          "200": {
            "description": "Marked read.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReadResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fmsg/{id}/add-to": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "post": {
        "tags": ["Messages"],
        "operationId": "addRecipients",
        "summary": "Add recipients",
        "description": "Adds recipients to an existing message as a new add-to batch. The authenticated identity must be the sender or a `to` recipient. New addresses must be distinct (case-insensitive). Not allowed on a terminal message (409).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AddToRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Recipients added.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AddToResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          }
        }
      }
    },
    "/fmsg/{id}/react": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "post": {
        "tags": ["Messages"],
        "operationId": "react",
        "summary": "Set or clear an emoji reaction",
        "description": "Sets or clears the authenticated identity's reaction (FMSG-005) by immediately sending a reaction message to every other participant. The message must be sent and not terminal, and the caller a participant. Setting the reaction you already have sends nothing and returns 200 with the existing reaction message; clearing when you have none returns 200 with null values. Counts against send limits as for send.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ReactRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Unchanged (same reaction already set, or nothing to clear).",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReactResult"
                }
              }
            }
          },
          "201": {
            "description": "Reaction message sent.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ReactResult"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          },
          "422": {
            "$ref": "#/components/responses/UnprocessableEntity"
          },
          "429": {
            "$ref": "#/components/responses/TooManyRequests"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg/{id}/data": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "get": {
        "tags": ["Messages"],
        "operationId": "downloadData",
        "summary": "Download the message body",
        "description": "The raw message body, with `Content-Disposition: attachment`. The caller must be a participant.",
        "responses": {
          "200": {
            "description": "The body.",
            "content": {
              "*/*": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fmsg/{id}/thread": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "get": {
        "tags": ["Messages"],
        "operationId": "getThreadText",
        "summary": "Thread as plain text",
        "description": "The message and its direct ancestors (following `pid` to the root), root first, as plain text for feeding to an agent as context. Each message is preceded by a line with its sender and send time. Ancestors you are not a participant of appear as `[message not visible to you]`; non-text bodies as a placeholder. Capped at 100 hops.",
        "responses": {
          "200": {
            "description": "The thread.",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fmsg/{id}/thread/messages": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "get": {
        "tags": ["Messages"],
        "operationId": "getThreadMessages",
        "summary": "Thread as JSON",
        "description": "The message and its direct `pid` ancestors, root first, as JSON. Sibling branches are not included. UTF-8 `text/*` and JSON bodies are inline; binary bodies and attachments have download paths. Ancestors you cannot read appear as `{\"id\": ..., \"visible\": false}` and make `complete` false. Capped at 100 messages (422 `thread_too_deep`) and 32 MiB of inline text (413 `thread_too_large`).",
        "responses": {
          "200": {
            "description": "The thread.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ThreadMessages"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          },
          "422": {
            "$ref": "#/components/responses/UnprocessableEntity"
          }
        }
      }
    },
    "/fmsg/{id}/attach": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        }
      ],
      "post": {
        "tags": ["Attachments"],
        "operationId": "uploadAttachment",
        "summary": "Attach a file to a draft",
        "description": "Uploads a file attachment to a draft. Only its sender may upload, and only before it is sent.",
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": ["file"],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary",
                    "description": "The file."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Attached.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Attachment"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "413": {
            "$ref": "#/components/responses/PayloadTooLarge"
          }
        }
      }
    },
    "/fmsg/{id}/attach/{filename}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/MessageId"
        },
        {
          "$ref": "#/components/parameters/Filename"
        }
      ],
      "get": {
        "tags": ["Attachments"],
        "operationId": "downloadAttachment",
        "summary": "Download an attachment",
        "description": "The attachment file, with `Content-Disposition: attachment`. The caller must be a participant.",
        "responses": {
          "200": {
            "description": "The file.",
            "content": {
              "*/*": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "delete": {
        "tags": ["Attachments"],
        "operationId": "deleteAttachment",
        "summary": "Remove an attachment from a draft",
        "description": "Only the draft's sender may delete, and only before it is sent.",
        "responses": {
          "204": {
            "description": "Deleted."
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/fmsg/push/subscribe": {
      "post": {
        "tags": ["Events"],
        "operationId": "pushSubscribe",
        "summary": "Register a Web Push subscription",
        "description": "Registers (or refreshes) a browser Web Push subscription for the authenticated address; a push is sent for each new message. Keyed by address and `endpoint`. Not available to delegated OAuth tokens.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PushSubscription"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Registered."
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      },
      "delete": {
        "tags": ["Events"],
        "operationId": "pushUnsubscribe",
        "summary": "Remove a Web Push subscription",
        "description": "Idempotent: removing an unknown endpoint still succeeds. Not available to delegated OAuth tokens.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PushUnsubscribe"
              }
            }
          }
        },
        "responses": {
          "204": {
            "description": "Removed."
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/fmsg/sub-accounts": {
      "get": {
        "tags": ["Sub-accounts"],
        "operationId": "listSubAccounts",
        "summary": "List sub-accounts",
        "description": "API-access grants owned by the authenticated owner. Requires an identity-provider owner session.",
        "responses": {
          "200": {
            "description": "Grants.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubAccountList"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      },
      "post": {
        "tags": ["Sub-accounts"],
        "operationId": "createSubAccount",
        "summary": "Create a sub-account",
        "description": "Creates a derived sub-account `@user_<agent>@domain` sharing the owner's quota, and returns its plaintext API key once. Without `key_expires_at` it has no API key (and no `key_id`, `key_expires_at` or `api_key`); rotate-key makes its first. Re-creating a deleted sub-account reinstates it with its earlier messages (`reinstated: true`). Requires an identity-provider owner session.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateSubAccountRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateSubAccountResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "409": {
            "$ref": "#/components/responses/Conflict"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg/sub-accounts/{agent}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/Agent"
        }
      ],
      "get": {
        "tags": ["Sub-accounts"],
        "operationId": "getSubAccount",
        "summary": "Get a sub-account",
        "description": "One grant owned by the authenticated owner. Requires an identity-provider owner session.",
        "responses": {
          "200": {
            "description": "The grant.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubAccount"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "patch": {
        "tags": ["Sub-accounts"],
        "operationId": "updateSubAccountCidrs",
        "summary": "Replace a sub-account's allowed CIDRs",
        "description": "Replaces the grant's allowed CIDRs without rotating its API key. Requires an identity-provider owner session.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateCidrsRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The updated grant.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SubAccount"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "delete": {
        "tags": ["Sub-accounts"],
        "operationId": "deleteSubAccount",
        "summary": "Delete a sub-account",
        "description": "Deletes the grant and revokes its key; existing JWTs for it are rejected on their next request. A derived sub-account's address stops accepting new messages but it and its messages are kept. Requires an identity-provider owner session.",
        "responses": {
          "204": {
            "description": "Deleted."
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    },
    "/fmsg/sub-accounts/{agent}/rotate-key": {
      "parameters": [
        {
          "$ref": "#/components/parameters/Agent"
        }
      ],
      "post": {
        "tags": ["Sub-accounts"],
        "operationId": "rotateSubAccountKey",
        "summary": "Rotate a sub-account's API key",
        "description": "Replaces the grant's API key (or makes the first for a sub-account without one) and returns the new plaintext key once. Does not change `allowed_cidrs`. Requires an identity-provider owner session.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RotateKeyRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The new key.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RotateKeyResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "503": {
            "$ref": "#/components/responses/ServiceUnavailable"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "A JWT, sent as `Authorization: Bearer <token>`. Either:\n- a first-party JWT from `POST /fmsg/token` (API-key exchange). It contains `sub` (the granted address), `owner`, `api_key_id`, `iss`, `aud`, `iat` and `exp`, and is re-checked against its key on each request, so deleting or rotating the key invalidates it at once;\n- an owner session JWT (EdDSA) from the identity provider, as used by the fmsg.ai web app. Owner sessions may act as one of their granted sub-addresses with the `X-FMSG-Act-As: @user_bot@domain` header;\n- a delegated OAuth JWT from https://idp.fmsg.ai (e.g. obtained by an MCP server via OAuth token exchange). It needs scope `fmsg:read` for the GET message, attachment and WebSocket routes and `fmsg:write` for the routes that change messages or attachments; every other route (sub-accounts, push) is refused with 403."
      },
      "apiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "fmsgk_<key_id>_<secret>",
        "description": "An opaque API key for one address, created by its owner at https://fmsg.ai/app. Accepted only by `POST /fmsg/token`, which exchanges it for a JWT."
      },
      "accessTokenQuery": {
        "type": "apiKey",
        "in": "query",
        "name": "access_token",
        "description": "The same JWT as `bearerAuth`, as a query parameter. Accepted only by `GET /fmsg/ws`, for browsers that cannot set headers on a WebSocket."
      }
    },
    "parameters": {
      "MessageId": {
        "name": "id",
        "in": "path",
        "required": true,
        "description": "A positive numeric message ID, or the message's 64-character hexadecimal SHA-256 (case-insensitive).",
        "schema": {
          "type": "string",
          "pattern": "^([1-9][0-9]*|[0-9a-fA-F]{64})$"
        }
      },
      "Filename": {
        "name": "filename",
        "in": "path",
        "required": true,
        "description": "Attachment filename, as in the message's `attachments`.",
        "schema": {
          "type": "string"
        }
      },
      "Agent": {
        "name": "agent",
        "in": "path",
        "required": true,
        "description": "The grant's agent name, e.g. `bot` for `@alice_bot@example.com`.",
        "schema": {
          "type": "string"
        }
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "required": false,
        "description": "Max messages to return (values above 100 are treated as 100).",
        "schema": {
          "type": "integer",
          "minimum": 1,
          "default": 20
        }
      },
      "Offset": {
        "name": "offset",
        "in": "query",
        "required": false,
        "description": "Number of messages to skip, for pagination.",
        "schema": {
          "type": "integer",
          "minimum": 0,
          "default": 0
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid request or reference.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Missing, invalid or expired token or API key.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Not permitted for this identity or token.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "Not found.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Conflict": {
        "description": "Conflicts with the current state (e.g. already sent, terminal, or not deliverable).",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "PayloadTooLarge": {
        "description": "Too large.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "UnprocessableEntity": {
        "description": "A limit would be exceeded.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "TooManyRequests": {
        "description": "A daily send limit would be exceeded.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "ServiceUnavailable": {
        "description": "The identity service is unavailable; retry later.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string",
            "description": "Human-readable reason."
          },
          "code": {
            "type": "string",
            "description": "Machine-readable reason, when given (e.g. `send_count_per_1d_limit`, `thread_too_deep`)."
          }
        }
      },
      "Address": {
        "type": "string",
        "description": "An fmsg address, `@user@domain`.",
        "examples": ["@alice@example.com"]
      },
      "Sha256": {
        "type": "string",
        "description": "Lowercase hexadecimal SHA-256.",
        "pattern": "^[0-9a-f]{64}$"
      },
      "TokenResponse": {
        "type": "object",
        "required": ["access_token", "token_type", "expires_in", "expires_at"],
        "properties": {
          "access_token": {
            "type": "string",
            "description": "JWT to send as `Authorization: Bearer <access_token>`."
          },
          "token_type": {
            "type": "string",
            "const": "Bearer"
          },
          "expires_in": {
            "type": "integer",
            "description": "Lifetime in seconds.",
            "examples": [43200]
          },
          "expires_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "RecipientDelivery": {
        "type": "object",
        "description": "Delivery status of one recipient.",
        "properties": {
          "addr": {
            "$ref": "#/components/schemas/Address"
          },
          "time_delivered": {
            "type": ["string", "null"],
            "format": "date-time",
            "description": "RFC 3339 UTC; null while pending or after failure."
          },
          "response_code": {
            "type": ["integer", "null"],
            "description": "fmsg per-recipient response code, set only when a delivery attempt failed (e.g. 100 unknown, 101 user full, 102 not accepting)."
          }
        }
      },
      "AddToBatch": {
        "type": "object",
        "description": "Recipients added in one add-to call.",
        "properties": {
          "sha256": {
            "type": ["string", "null"],
            "description": "The batch's protocol hash; null on a draft until send."
          },
          "batch_id": {
            "type": "integer"
          },
          "add_to_from": {
            "$ref": "#/components/schemas/Address"
          },
          "to": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          },
          "to_delivery": {
            "type": ["array", "null"],
            "items": {
              "$ref": "#/components/schemas/RecipientDelivery"
            }
          },
          "time": {
            "type": "number",
            "description": "When the add-to happened, Unix seconds."
          }
        }
      },
      "Attachment": {
        "type": "object",
        "properties": {
          "filename": {
            "type": "string"
          },
          "size": {
            "type": "integer",
            "description": "Bytes."
          }
        }
      },
      "Reaction": {
        "type": "object",
        "description": "One emoji reacted on a message and who reacted with it.",
        "properties": {
          "emoji": {
            "type": "string"
          },
          "from": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          }
        }
      },
      "Message": {
        "type": "object",
        "description": "A message, as returned by `GET /fmsg/{id}`. Bodies are not included; use `short_text` or `GET /fmsg/{id}/data`.",
        "properties": {
          "sha256": {
            "type": ["string", "null"],
            "description": "Protocol hash of a sent or received message; null for a draft."
          },
          "psha256": {
            "type": ["string", "null"],
            "description": "Exact parent hash (a message or an add-to batch), or null."
          },
          "version": {
            "type": "integer"
          },
          "has_pid": {
            "type": "boolean"
          },
          "has_add_to": {
            "type": "boolean"
          },
          "important": {
            "type": "boolean"
          },
          "no_reply": {
            "type": "boolean",
            "description": "Sender asks recipients not to reply."
          },
          "deflate": {
            "type": "boolean"
          },
          "terminal": {
            "type": "boolean",
            "description": "A leaf: no reply or add-to is accepted."
          },
          "pid": {
            "type": ["integer", "null"],
            "description": "Parent message ID for a reply."
          },
          "from": {
            "$ref": "#/components/schemas/Address"
          },
          "to": {
            "type": ["array", "null"],
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          },
          "to_delivery": {
            "type": ["array", "null"],
            "description": "One entry per `to` address.",
            "items": {
              "$ref": "#/components/schemas/RecipientDelivery"
            }
          },
          "add_to": {
            "type": ["array", "null"],
            "description": "Add-to batches; null when there are none.",
            "items": {
              "$ref": "#/components/schemas/AddToBatch"
            }
          },
          "time": {
            "type": ["number", "null"],
            "description": "Send time, Unix seconds; null for a draft."
          },
          "topic": {
            "type": "string",
            "description": "Thread topic; only on a thread's first message."
          },
          "type": {
            "type": "string",
            "description": "Media type of the body.",
            "examples": ["text/plain;charset=UTF-8"]
          },
          "size": {
            "type": "integer",
            "description": "Body size in bytes."
          },
          "short_text": {
            "type": "string",
            "description": "Start of the body (up to 768 bytes) for valid UTF-8 `text/*` messages; absent otherwise."
          },
          "read": {
            "type": "boolean",
            "description": "Whether the caller has marked it read (always false for the sender)."
          },
          "time_read": {
            "type": ["number", "null"],
            "description": "When the caller marked it read, Unix seconds."
          },
          "attachments": {
            "type": ["array", "null"],
            "description": "Null when there are none.",
            "items": {
              "$ref": "#/components/schemas/Attachment"
            }
          },
          "reaction": {
            "type": ["string", "null"],
            "description": "If this message is itself a reaction (FMSG-005), its emoji (\"\" clears); null otherwise. Clients can hide reaction messages."
          },
          "reactions": {
            "type": "array",
            "description": "Effective reactions on this message, grouped by emoji.",
            "items": {
              "$ref": "#/components/schemas/Reaction"
            }
          }
        }
      },
      "MessageListItem": {
        "description": "A message as in `GET /fmsg`, `GET /fmsg/sent` and WebSocket events: `Message` plus its `id`.",
        "allOf": [
          {
            "$ref": "#/components/schemas/Message"
          },
          {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": {
                "type": "integer"
              }
            }
          }
        ]
      },
      "MessageInput": {
        "type": "object",
        "description": "Draft fields for create and update. `from` must be the authenticated address. A draft may be incomplete, but send needs at least one recipient, a `type` and `version` 1.",
        "required": ["from"],
        "properties": {
          "version": {
            "type": "integer",
            "description": "Protocol version; currently 1.",
            "examples": [1]
          },
          "from": {
            "$ref": "#/components/schemas/Address"
          },
          "to": {
            "type": "array",
            "description": "Recipients. Replaced in full on update.",
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          },
          "pid": {
            "description": "Parent for a reply: a numeric message ID or a 64-character hex hash (of a message or an add-to batch). The parent must be sent and not terminal. Omit `topic` when set.",
            "oneOf": [
              {
                "type": "integer",
                "minimum": 1
              },
              {
                "type": "string",
                "pattern": "^[0-9a-fA-F]{64}$"
              },
              {
                "type": "null"
              }
            ]
          },
          "topic": {
            "type": "string",
            "description": "Thread topic; only on a thread's first message (no `pid`)."
          },
          "type": {
            "type": "string",
            "description": "Media type of the body.",
            "examples": ["text/plain;charset=UTF-8"]
          },
          "size": {
            "type": "integer",
            "description": "Body size in bytes. The server stores the length of `data`."
          },
          "important": {
            "type": "boolean"
          },
          "no_reply": {
            "type": "boolean",
            "description": "Ask recipients not to reply."
          },
          "terminal": {
            "type": "boolean",
            "description": "Make the message a leaf nobody can reply to or add recipients to."
          },
          "data": {
            "type": "string",
            "description": "The message body."
          }
        }
      },
      "CreateMessageRequest": {
        "allOf": [
          {
            "$ref": "#/components/schemas/MessageInput"
          },
          {
            "type": "object",
            "required": ["to"],
            "properties": {
              "to": {
                "type": "array",
                "minItems": 1
              }
            }
          }
        ]
      },
      "IdResult": {
        "type": "object",
        "required": ["id"],
        "properties": {
          "id": {
            "type": "integer"
          }
        }
      },
      "SendResult": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "time": {
            "type": "number",
            "description": "Send time, Unix seconds."
          },
          "sha256": {
            "$ref": "#/components/schemas/Sha256"
          }
        }
      },
      "ReadResult": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "time_read": {
            "type": "number",
            "description": "Unix seconds."
          }
        }
      },
      "AddToRequest": {
        "type": "object",
        "required": ["add_to"],
        "properties": {
          "add_to": {
            "type": "array",
            "minItems": 1,
            "description": "Addresses to add, distinct (case-insensitive).",
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          }
        }
      },
      "AddToResult": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "added": {
            "type": "integer"
          },
          "batch_id": {
            "type": "integer"
          },
          "sha256": {
            "type": ["string", "null"],
            "description": "The batch hash; null on a draft until send."
          }
        }
      },
      "ReactRequest": {
        "type": "object",
        "properties": {
          "emoji": {
            "type": ["string", "null"],
            "description": "A single emoji; \"\" or null clears your reaction.",
            "examples": ["👍"]
          }
        }
      },
      "ReactResult": {
        "type": "object",
        "description": "The reaction message; all null when clearing a reaction you did not have.",
        "properties": {
          "id": {
            "type": ["integer", "null"]
          },
          "time": {
            "type": ["number", "null"]
          },
          "sha256": {
            "type": ["string", "null"]
          }
        }
      },
      "ThreadBody": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "size": {
            "type": "integer"
          },
          "text": {
            "type": "string",
            "description": "Inline body, for UTF-8 text and JSON bodies."
          },
          "download": {
            "type": "string",
            "description": "Authenticated download path, for other bodies."
          },
          "cache_key": {
            "type": "string"
          },
          "cacheable": {
            "type": "boolean"
          }
        }
      },
      "ThreadAttachment": {
        "type": "object",
        "properties": {
          "position": {
            "type": "integer"
          },
          "type": {
            "type": "string"
          },
          "filename": {
            "type": "string"
          },
          "size": {
            "type": "integer"
          },
          "download": {
            "type": "string",
            "description": "Authenticated download path."
          },
          "cache_key": {
            "type": "string"
          },
          "cacheable": {
            "type": "boolean"
          }
        }
      },
      "ThreadMessage": {
        "type": "object",
        "description": "A thread entry. When `visible` is false only `id` and `visible` are present. Fields with zero values may be omitted.",
        "required": ["id", "visible"],
        "properties": {
          "id": {
            "type": "integer"
          },
          "visible": {
            "type": "boolean"
          },
          "sha256": {
            "type": ["string", "null"]
          },
          "psha256": {
            "type": ["string", "null"]
          },
          "version": {
            "type": "integer"
          },
          "pid": {
            "type": ["integer", "null"]
          },
          "no_reply": {
            "type": "boolean"
          },
          "important": {
            "type": "boolean"
          },
          "deflate": {
            "type": "boolean"
          },
          "terminal": {
            "type": "boolean"
          },
          "from": {
            "$ref": "#/components/schemas/Address"
          },
          "to": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Address"
            }
          },
          "add_to": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/AddToBatch"
            }
          },
          "time": {
            "type": ["number", "null"]
          },
          "topic": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "size": {
            "type": "integer"
          },
          "message_sha256": {
            "type": "string",
            "description": "Alias of `sha256`."
          },
          "body": {
            "$ref": "#/components/schemas/ThreadBody"
          },
          "attachments": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ThreadAttachment"
            }
          }
        }
      },
      "ThreadMessages": {
        "type": "object",
        "properties": {
          "root_id": {
            "type": "integer"
          },
          "trigger_id": {
            "type": "integer",
            "description": "The requested message."
          },
          "complete": {
            "type": "boolean",
            "description": "False when some ancestors are not visible to you."
          },
          "messages": {
            "type": "array",
            "description": "Root first, ending with the requested message.",
            "items": {
              "$ref": "#/components/schemas/ThreadMessage"
            }
          }
        }
      },
      "WsEvent": {
        "type": "object",
        "description": "A WebSocket frame from `GET /fmsg/ws`.",
        "required": ["type", "data"],
        "properties": {
          "type": {
            "type": "string",
            "description": "Event type; new types may be added.",
            "examples": ["new_msg", "delivered", "recipients_added", "reaction"]
          },
          "data": {
            "$ref": "#/components/schemas/MessageListItem"
          }
        }
      },
      "PushSubscription": {
        "type": "object",
        "description": "A browser `PushSubscription.toJSON()`. `expirationTime` is accepted but ignored.",
        "required": ["endpoint", "keys"],
        "properties": {
          "endpoint": {
            "type": "string",
            "format": "uri"
          },
          "expirationTime": {
            "type": ["number", "null"]
          },
          "keys": {
            "type": "object",
            "required": ["p256dh", "auth"],
            "properties": {
              "p256dh": {
                "type": "string"
              },
              "auth": {
                "type": "string"
              }
            }
          }
        }
      },
      "PushUnsubscribe": {
        "type": "object",
        "required": ["endpoint"],
        "properties": {
          "endpoint": {
            "type": "string",
            "format": "uri"
          }
        }
      },
      "SubAccount": {
        "type": "object",
        "description": "An API-access grant. Without an API key, `key_id` and `key_expires_at` are absent.",
        "properties": {
          "agent": {
            "type": "string"
          },
          "addr": {
            "$ref": "#/components/schemas/Address"
          },
          "grant_type": {
            "type": "string",
            "enum": ["derived_sub_account", "delegated_identity"]
          },
          "display_name": {
            "type": "string"
          },
          "key_id": {
            "type": "string"
          },
          "allowed_cidrs": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "key_expires_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "SubAccountList": {
        "type": "object",
        "properties": {
          "max_sub_accounts": {
            "type": "integer"
          },
          "sub_accounts": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SubAccount"
            }
          }
        }
      },
      "CreateSubAccountRequest": {
        "type": "object",
        "required": ["agent", "allowed_cidrs"],
        "properties": {
          "agent": {
            "type": "string",
            "description": "1-64 letters, digits, dots or hyphens; no underscores. The address is `@user_<agent>@domain`.",
            "minLength": 1,
            "maxLength": 64
          },
          "allowed_cidrs": {
            "type": "array",
            "minItems": 1,
            "description": "Source IP ranges the API key may be used from.",
            "items": {
              "type": "string",
              "examples": ["203.0.113.0/24"]
            }
          },
          "key_expires_at": {
            "type": "string",
            "format": "date-time",
            "description": "Future expiry for the API key. Omit to create the sub-account without a key."
          }
        }
      },
      "CreateSubAccountResponse": {
        "allOf": [
          {
            "$ref": "#/components/schemas/SubAccount"
          },
          {
            "type": "object",
            "properties": {
              "api_key": {
                "type": "string",
                "description": "Plaintext API key, shown once. Absent when no key was made."
              },
              "reinstated": {
                "type": "boolean",
                "description": "True when a previously deleted sub-account address was reinstated."
              }
            }
          }
        ]
      },
      "UpdateCidrsRequest": {
        "type": "object",
        "required": ["allowed_cidrs"],
        "properties": {
          "allowed_cidrs": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string"
            }
          }
        }
      },
      "RotateKeyRequest": {
        "type": "object",
        "required": ["key_expires_at"],
        "properties": {
          "key_expires_at": {
            "type": "string",
            "format": "date-time",
            "description": "Future expiry for the new key."
          }
        }
      },
      "RotateKeyResponse": {
        "type": "object",
        "properties": {
          "agent": {
            "type": "string"
          },
          "addr": {
            "$ref": "#/components/schemas/Address"
          },
          "grant_type": {
            "type": "string",
            "enum": ["derived_sub_account", "delegated_identity"]
          },
          "key_id": {
            "type": "string"
          },
          "key_expires_at": {
            "type": "string",
            "format": "date-time"
          },
          "api_key": {
            "type": "string",
            "description": "Plaintext API key, shown once."
          }
        }
      }
    }
  }
}
