{
  "openapi": "3.1.0",
  "info": {
    "title": "LlamaEmail API",
    "version": "1.0.0",
    "description": "Send and read email for your custom-domain mailbox, manage domains and mailboxes, and poll delivery events. Prefer customer webhooks (Settings → Developer API) for realtime email.sent, email.received, email.bounced, and email.complained. Messages appear in webmail Sent and Inbox."
  },
  "servers": [
    {
      "url": "https://app.llamaemail.com/v1",
      "description": "Production"
    }
  ],
  "security": [{ "bearerAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "le_live_…"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "message": { "type": "string" },
              "code": { "type": "string" }
            },
            "required": ["message"]
          }
        }
      },
      "Email": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "enum": ["email"] },
          "folder": { "type": "string", "enum": ["inbox", "sent"] },
          "from": { "type": "string" },
          "to": { "type": "array", "items": { "type": "string" } },
          "cc": { "type": "array", "items": { "type": "string" } },
          "bcc": { "type": "array", "items": { "type": "string" } },
          "subject": { "type": "string" },
          "text": { "type": "string" },
          "html": { "type": "string" },
          "created_at": { "type": "string", "format": "date-time" },
          "state": { "type": "string" }
        }
      },
      "Domain": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "enum": ["domain"] },
          "name": { "type": "string" },
          "status": {
            "type": "object",
            "properties": {
              "stage": { "type": "string" },
              "label": { "type": "string" },
              "detail": { "type": "string" }
            }
          },
          "verified_at": { "type": ["string", "null"], "format": "date-time" },
          "sending_verified": { "type": "boolean" },
          "receiving_state": { "type": ["string", "null"] }
        }
      },
      "DeliveryEvent": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "enum": ["event"] },
          "type": { "type": "string", "enum": ["email.bounced", "email.complained"] },
          "email_id": { "type": "string" },
          "recipient": { "type": "string" },
          "bounce_type": { "type": "string", "enum": ["soft", "hard"] },
          "suppressed": { "type": "boolean" },
          "provider_message_id": { "type": ["string", "null"] },
          "occurred_at": { "type": ["string", "null"] },
          "created_at": { "type": "string", "format": "date-time" }
        }
      },
      "Mailbox": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "object": { "type": "string", "enum": ["mailbox"] },
          "address": { "type": "string" },
          "name": { "type": "string" },
          "domain_id": { "type": "string" },
          "active": { "type": "boolean" }
        }
      }
    }
  },
  "paths": {
    "/emails": {
      "post": {
        "summary": "Send an email",
        "operationId": "sendEmail",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID. Retries with the same key return the same send."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["to"],
                "properties": {
                  "from": {
                    "type": "string",
                    "description": "Verified identity on the key's mailbox. Defaults to the mailbox address."
                  },
                  "to": {
                    "oneOf": [
                      { "type": "string" },
                      { "type": "array", "items": { "type": "string" } }
                    ]
                  },
                  "cc": {
                    "oneOf": [
                      { "type": "string" },
                      { "type": "array", "items": { "type": "string" } }
                    ]
                  },
                  "bcc": {
                    "oneOf": [
                      { "type": "string" },
                      { "type": "array", "items": { "type": "string" } }
                    ]
                  },
                  "subject": { "type": "string" },
                  "text": { "type": "string" },
                  "html": { "type": "string" },
                  "idempotency_key": { "type": "string", "format": "uuid" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted for delivery",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Email" }
              }
            }
          },
          "401": {
            "description": "Invalid API key",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          },
          "403": {
            "description": "Missing scope or from address",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          },
          "429": {
            "description": "Sending limit",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          }
        }
      },
      "get": {
        "summary": "List emails",
        "operationId": "listEmails",
        "parameters": [
          {
            "name": "folder",
            "in": "query",
            "schema": { "type": "string", "enum": ["inbox", "sent"], "default": "inbox" }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 }
          }
        ],
        "responses": {
          "200": {
            "description": "Email list",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "object": { "type": "string", "enum": ["list"] },
                    "folder": { "type": "string" },
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Email" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/emails/{id}": {
      "get": {
        "summary": "Get an email",
        "operationId": "getEmail",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
          }
        ],
        "responses": {
          "200": {
            "description": "Email",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Email" }
              }
            }
          },
          "404": {
            "description": "Not found",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          }
        }
      }
    },
    "/domains": {
      "get": {
        "summary": "List domains",
        "operationId": "listDomains",
        "responses": {
          "200": {
            "description": "Domain list for the API key workspace",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "object": { "type": "string", "enum": ["list"] },
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Domain" }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create a domain",
        "operationId": "createDomain",
        "description": "Workspace owners with a full-access API key. DNS verification completes in the app.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["name"],
                "properties": {
                  "name": { "type": "string", "example": "yourdomain.com" }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created domain",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Domain" } }
            }
          }
        }
      }
    },
    "/domains/{id}": {
      "get": {
        "summary": "Get a domain",
        "operationId": "getDomain",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Domain",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Domain" }
              }
            }
          },
          "404": {
            "description": "Not found",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Error" } }
            }
          }
        }
      }
    },
    "/mailboxes": {
      "get": {
        "summary": "List mailboxes",
        "operationId": "listMailboxes",
        "responses": {
          "200": {
            "description": "Mailboxes accessible in the API key workspace",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "object": { "type": "string", "enum": ["list"] },
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Mailbox" }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Create a mailbox",
        "operationId": "createMailbox",
        "description": "Workspace owners with a full-access API key.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["domain_id", "local_part"],
                "properties": {
                  "domain_id": { "type": "string" },
                  "local_part": { "type": "string", "example": "hello" },
                  "name": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created mailbox",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Mailbox" } }
            }
          }
        }
      }
    },
    "/mailboxes/{id}": {
      "get": {
        "summary": "Get a mailbox",
        "operationId": "getMailbox",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Mailbox",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/Mailbox" } }
            }
          }
        }
      }
    },
    "/events": {
      "get": {
        "summary": "List delivery events",
        "operationId": "listEvents",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 }
          }
        ],
        "responses": {
          "200": {
            "description": "Bounce and complaint events for this mailbox",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "object": { "type": "string", "enum": ["list"] },
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/DeliveryEvent" }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
