{
  "openapi": "3.0.0",
  "info": {
    "title": "KwikMail API",
    "version": "1.1.0",
    "description": "Disposable email infrastructure for AI Agents. All inboxes use the m1.kwikmail.uk domain. Authenticate with a Bearer API key (km_* prefix)."
  },
  "servers": [
    {
      "url": "https://api.kwikmail.uk",
      "description": "Production"
    },
    {
      "url": "https://api-staging.kwikmail.uk",
      "description": "Staging"
    }
  ],
  "security": [
    {
      "BearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "inbox",
      "description": "Inbox management — create, read, and delete inboxes. Inboxes are created implicitly when you lock them with a password or receive an email."
    },
    {
      "name": "auth",
      "description": "Inbox authentication — lock, unlock, and manage inbox passwords. Locking an inbox with a password is the recommended way to create and reserve an inbox."
    },
    {
      "name": "send",
      "description": "Send and forward emails from your inbox."
    },
    {
      "name": "email",
      "description": "Manage individual emails — mark as read, toggle burn-on-read, download attachments."
    },
    {
      "name": "alias",
      "description": "Create scrambled aliases that forward to your real inbox."
    },
    {
      "name": "webhook",
      "description": "Register webhook callbacks for real-time email notifications."
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "healthCheck",
        "summary": "Service health check",
        "tags": [
          "inbox"
        ],
        "security": [],
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "service": {
                      "type": "string"
                    },
                    "timestamp": {
                      "type": "string",
                      "format": "date-time"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/": {
      "get": {
        "operationId": "getEmails",
        "summary": "Get inbox — read all emails",
        "description": "Retrieves all emails for the specified inbox. This also serves as a way to check if an inbox exists — an empty array means the inbox has no emails (or hasn't been created yet).",
        "tags": [
          "inbox"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "domain",
            "in": "query",
            "required": false,
            "description": "Inbox domain",
            "schema": {
              "type": "string",
              "default": "m1.kwikmail.uk"
            }
          },
          {
            "name": "from",
            "in": "query",
            "required": false,
            "description": "Filter by sender address (substring match)",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "subject",
            "in": "query",
            "required": false,
            "description": "Filter by subject (substring match)",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "after",
            "in": "query",
            "required": false,
            "description": "Filter by date >= ISO 8601 string",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "before",
            "in": "query",
            "required": false,
            "description": "Filter by date <= ISO 8601 string",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "unread",
            "in": "query",
            "required": false,
            "description": "Filter by read status. true = unread only, false = read only.",
            "schema": {
              "type": "string",
              "enum": [
                "true",
                "false"
              ]
            }
          },
          {
            "name": "direction",
            "in": "query",
            "required": false,
            "description": "Filter by email direction. Defaults to 'received' (excludes sent copies). Use 'sent' for sent only, or 'all' for both.",
            "schema": {
              "type": "string",
              "enum": [
                "received",
                "sent",
                "all"
              ],
              "default": "received"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "Max number of emails to return",
            "schema": {
              "type": "integer",
              "minimum": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Array of emails",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Email"
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      },
      "delete": {
        "operationId": "deleteEmails",
        "summary": "Delete inbox or specific emails",
        "description": "Delete specific emails by ID, or omit the `id` parameter to wipe the entire inbox and all its emails. This is the equivalent of deleting an inbox.",
        "tags": [
          "inbox"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "domain",
            "in": "query",
            "required": false,
            "description": "Inbox domain",
            "schema": {
              "type": "string",
              "default": "m1.kwikmail.uk"
            }
          },
          {
            "name": "id",
            "in": "query",
            "required": false,
            "description": "Comma-separated email IDs to delete. Omit to wipe all.",
            "schema": {
              "type": "string",
              "pattern": "^[0-9]+(,[0-9]+)*$"
            }
          }
        ],
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/inbox/expiry": {
      "get": {
        "operationId": "getInboxExpiry",
        "summary": "Get inbox expiration",
        "description": "Returns when this inbox will be automatically deleted. Use this alongside `GET /` and `GET /auth/check` to get full inbox metadata (emails, expiry, and protection status).",
        "tags": [
          "inbox"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Expiry info",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "expiresAt": {
                      "type": "number",
                      "nullable": true
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/wait": {
      "get": {
        "operationId": "waitForEmail",
        "summary": "Long poll — wait for a new email to arrive",
        "description": "Returns immediately if matching email exists, otherwise blocks until a new email arrives or timeout.",
        "tags": [
          "inbox"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "domain",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "m1.kwikmail.uk"
            },
            "description": "Inbox domain"
          },
          {
            "name": "timeout",
            "in": "query",
            "required": false,
            "description": "Seconds to wait (1-30, default 15)",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 30,
              "default": 15
            }
          },
          {
            "name": "from",
            "in": "query",
            "required": false,
            "description": "Filter by sender (substring match)",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "subject",
            "in": "query",
            "required": false,
            "description": "Filter by subject (substring match)",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "after",
            "in": "query",
            "required": false,
            "description": "Filter by date >= ISO string",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "unread",
            "in": "query",
            "required": false,
            "description": "Filter by read status. true = unread only, false = read only.",
            "schema": {
              "type": "string",
              "enum": [
                "true",
                "false"
              ]
            }
          },
          {
            "name": "direction",
            "in": "query",
            "required": false,
            "description": "Filter by email direction. Defaults to 'received'.",
            "schema": {
              "type": "string",
              "enum": [
                "received",
                "sent",
                "all"
              ],
              "default": "received"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Email arrived or timeout",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "timeout": {
                      "type": "boolean"
                    },
                    "emails": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Email"
                      }
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Too many concurrent waiters (max 10 per inbox)"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/auth/check": {
      "get": {
        "operationId": "authCheck",
        "summary": "Get inbox status — check if password-protected",
        "description": "Returns whether the inbox has a password set. Use alongside `GET /` and `GET /inbox/expiry` to get full inbox metadata.",
        "tags": [
          "auth"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "domain",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "m1.kwikmail.uk"
            },
            "description": "Inbox domain"
          }
        ],
        "responses": {
          "200": {
            "description": "Protection status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "protected": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/auth/lock": {
      "post": {
        "operationId": "authLock",
        "summary": "Create inbox — set a password to reserve it",
        "description": "Sets a password on an inbox, effectively creating and reserving it. Use `type: persistent` to make the inbox non-expiring. This is the recommended way to create a dedicated agent inbox.",
        "tags": [
          "auth"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "password"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "password": {
                    "type": "string",
                    "minLength": 1
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "persistent"
                    ],
                    "description": "Set to 'persistent' for a non-expiring inbox"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Password set",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AuthLockResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "409": {
            "description": "Inbox already locked",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Lock rate limit exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/auth/unlock": {
      "post": {
        "operationId": "authUnlock",
        "summary": "Remove password from an inbox",
        "tags": [
          "auth"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "password"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "password": {
                    "type": "string",
                    "description": "Current password to verify before unlocking"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Inbox not locked"
          }
        }
      }
    },
    "/auth/change": {
      "post": {
        "operationId": "authChange",
        "summary": "Change inbox password",
        "tags": [
          "auth"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "currentPassword",
                  "newPassword"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "currentPassword": {
                    "type": "string"
                  },
                  "newPassword": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "description": "Inbox not locked"
          }
        }
      }
    },
    "/send": {
      "post": {
        "operationId": "sendEmail",
        "summary": "Compose and send an email",
        "description": "Sends an email from your agent inbox. Internal recipients (@m1.kwikmail.uk) are delivered directly; external recipients are sent via an outbound mail provider.",
        "tags": [
          "send"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "to",
                  "subject",
                  "body"
                ],
                "properties": {
                  "inbox": {
                    "type": "string",
                    "maxLength": 64
                  },
                  "domain": {
                    "type": "string",
                    "maxLength": 64,
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "senderName": {
                    "type": "string",
                    "maxLength": 128
                  },
                  "to": {
                    "type": "string",
                    "format": "email",
                    "maxLength": 320
                  },
                  "subject": {
                    "type": "string",
                    "maxLength": 998
                  },
                  "body": {
                    "type": "string",
                    "maxLength": 512000
                  },
                  "replyToId": {
                    "type": "integer",
                    "description": "Email ID to reply to (sets In-Reply-To and References headers for threading)"
                  },
                  "attachments": {
                    "type": "array",
                    "maxItems": 5,
                    "description": "Optional file attachments (max 5, 10MB each, 25MB total)",
                    "items": {
                      "type": "object",
                      "required": [
                        "filename",
                        "content"
                      ],
                      "properties": {
                        "filename": {
                          "type": "string",
                          "maxLength": 255
                        },
                        "content": {
                          "type": "string",
                          "maxLength": 13631488,
                          "description": "Base64-encoded file content (max ~10MB)"
                        },
                        "mimeType": {
                          "type": "string",
                          "maxLength": 128,
                          "description": "MIME type (default: application/octet-stream)"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Email sent",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SendEmailResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "description": "Send rate limit exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Upstream delivery failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/forward": {
      "post": {
        "operationId": "forwardEmail",
        "summary": "Forward an email to an external address",
        "tags": [
          "send"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "id",
                  "to"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "to": {
                    "type": "string",
                    "format": "email"
                  },
                  "id": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "ID of the email to forward"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Email forwarded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ForwardEmailResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Invalid email index"
          },
          "429": {
            "description": "Forward rate limit exceeded",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Upstream delivery failure",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/read": {
      "put": {
        "operationId": "markAsRead",
        "summary": "Mark email(s) as read or unread",
        "description": "Mark one or more emails as read/unread using stable email IDs.",
        "tags": [
          "email"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "id": {
                    "type": "integer",
                    "description": "ID of a single email"
                  },
                  "ids": {
                    "type": "array",
                    "items": {
                      "type": "integer"
                    },
                    "description": "Array of email IDs (for batch operations)"
                  },
                  "read": {
                    "type": "boolean",
                    "default": true,
                    "description": "true = mark read, false = mark unread"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Emails updated",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "updated": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "404": {
            "description": "No valid indices found"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/burn": {
      "put": {
        "operationId": "toggleBurnOnRead",
        "summary": "Toggle burn-on-read flag for an email",
        "tags": [
          "email"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "id"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "burn": {
                    "type": "boolean"
                  },
                  "id": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "ID of the email"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "Invalid index"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    },
    "/alias": {
      "post": {
        "operationId": "createAlias",
        "summary": "Register a scrambled alias for an inbox",
        "tags": [
          "alias"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "alias",
                  "inbox"
                ],
                "properties": {
                  "alias": {
                    "type": "string"
                  },
                  "inbox": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Alias created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean"
                    },
                    "alias": {
                      "type": "string"
                    },
                    "inbox": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/webhook": {
      "post": {
        "operationId": "registerWebhook",
        "summary": "Register a webhook callback for new email notifications",
        "tags": [
          "webhook"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "url",
                  "secret"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "description": "Inbox domain",
                    "default": "m1.kwikmail.uk"
                  },
                  "url": {
                    "type": "string",
                    "format": "uri",
                    "description": "HTTPS callback URL"
                  },
                  "secret": {
                    "type": "string",
                    "description": "Shared secret for HMAC-SHA256 signature verification"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "429": {
            "description": "Max 5 webhooks per inbox"
          }
        }
      },
      "delete": {
        "operationId": "deleteWebhook",
        "summary": "Unregister a webhook",
        "tags": [
          "webhook"
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "inbox",
                  "url"
                ],
                "properties": {
                  "inbox": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string",
                    "default": "m1.kwikmail.uk",
                    "description": "Inbox domain"
                  },
                  "url": {
                    "type": "string",
                    "format": "uri"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "$ref": "#/components/responses/Success"
          }
        }
      },
      "get": {
        "operationId": "listWebhooks",
        "summary": "List registered webhooks for an inbox",
        "tags": [
          "webhook"
        ],
        "parameters": [
          {
            "name": "inbox",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "domain",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "default": "m1.kwikmail.uk"
            },
            "description": "Inbox domain"
          }
        ],
        "responses": {
          "200": {
            "description": "Array of webhook registrations",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "url": {
                        "type": "string"
                      },
                      "created_at": {
                        "type": "integer"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/attachment": {
      "get": {
        "operationId": "getAttachment",
        "summary": "Download an email attachment",
        "tags": [
          "email"
        ],
        "parameters": [
          {
            "name": "key",
            "in": "query",
            "required": true,
            "description": "Object key from the email attachment's url field",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Attachment file content",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "404": {
            "description": "Attachment not found"
          }
        },
        "security": [
          {
            "BearerAuth": [],
            "InboxPassword": []
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key with km_* prefix. Obtain via admin API or dashboard."
      },
      "InboxPassword": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Inbox-Password",
        "description": "Required for password-protected inboxes. Pass the inbox password in this header."
      }
    },
    "schemas": {
      "Email": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "from": {
            "type": "string",
            "nullable": true
          },
          "to": {
            "type": "string",
            "nullable": true,
            "description": "Recipient address (present on sent emails)"
          },
          "subject": {
            "type": "string",
            "nullable": true
          },
          "text": {
            "type": "string",
            "nullable": true
          },
          "html": {
            "type": "string",
            "nullable": true
          },
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "read": {
            "type": "boolean"
          },
          "burnOnRead": {
            "type": "boolean"
          },
          "burnRead": {
            "type": "boolean"
          },
          "direction": {
            "type": "string",
            "enum": [
              "sent",
              "received"
            ],
            "description": "Whether this email was sent or received by the inbox"
          },
          "threadId": {
            "type": "string",
            "nullable": true,
            "description": "Thread identifier for grouping related emails"
          },
          "messageId": {
            "type": "string",
            "nullable": true,
            "description": "RFC 2822 Message-ID"
          },
          "attachments": {
            "type": "array",
            "nullable": true,
            "items": {
              "type": "object",
              "properties": {
                "filename": {
                  "type": "string"
                },
                "mimeType": {
                  "type": "string"
                },
                "size": {
                  "type": "integer"
                },
                "url": {
                  "type": "string",
                  "description": "Download URL (GET /attachment?key=...). Present when the attachment is stored server-side."
                },
                "content": {
                  "type": "string",
                  "description": "Base64-encoded content (legacy inline format, deprecated)"
                }
              }
            }
          }
        }
      },
      "SendEmailResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean"
          },
          "routed": {
            "type": "string",
            "enum": [
              "internal",
              "external",
              "cross-env"
            ],
            "description": "How the email was delivered: internal (same domain), external (outbound provider), cross-env (to staging)"
          },
          "emailId": {
            "type": "integer",
            "description": "ID of the stored sent copy"
          },
          "messageId": {
            "type": "string",
            "description": "RFC 2822 Message-ID of the sent email"
          },
          "threadId": {
            "type": "string",
            "description": "Thread ID for the conversation"
          },
          "attachments": {
            "type": "integer",
            "description": "Number of attachments sent (only present when > 0)"
          }
        }
      },
      "ForwardEmailResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean"
          },
          "routed": {
            "type": "string",
            "enum": [
              "internal"
            ],
            "description": "Present when forwarded to an internal recipient"
          },
          "attachments": {
            "type": "integer",
            "description": "Number of attachments forwarded (only present when > 0)"
          }
        }
      },
      "AuthLockResponse": {
        "type": "object",
        "properties": {
          "success": {
            "type": "boolean"
          },
          "expiresAt": {
            "type": "number",
            "nullable": true,
            "description": "Unix timestamp when the inbox expires. null for persistent inboxes."
          },
          "type": {
            "type": "string",
            "nullable": true,
            "description": "Set to 'persistent' for non-expiring inboxes. Absent for ephemeral."
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "code": {
            "type": "string",
            "description": "Machine-readable error code (e.g. upstream_auth, upstream_rate_limit, upstream_transient)"
          }
        }
      }
    },
    "responses": {
      "Success": {
        "description": "Operation succeeded",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "success": {
                  "type": "boolean"
                }
              }
            }
          }
        }
      },
      "BadRequest": {
        "description": "Missing or invalid parameters",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "Authentication required — either missing/invalid Bearer token, or inbox is password-protected and X-Inbox-Password header is missing (error: password_required)",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Wrong password",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}
