{
  "openapi": "3.1.0",
  "info": {
    "title": "bookbox.ink public API",
    "version": "1.0.0",
    "summary": "Book records, cited book chat, and the rights-aware book finder.",
    "description": "Public, unauthenticated endpoints served by bookbox.ink. Responses contain Bookbox interpretation and source locators, never copyrighted source text. Chat answers are retrieval over a derived citation corpus, not quotations.",
    "contact": { "name": "Adam Pang", "url": "https://bookbox.ink/contact" }
  },
  "servers": [{ "url": "https://bookbox.ink" }],
  "paths": {
    "/api/v1/books": {
      "get": {
        "operationId": "listBooks",
        "summary": "List every Bookbox book record.",
        "responses": {
          "200": {
            "description": "The catalog of chat-ready books.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["schemaVersion", "canonicalOwner", "data"],
                  "properties": {
                    "schemaVersion": { "type": "string", "const": "bookbox.catalog.v1" },
                    "canonicalOwner": { "type": "string", "const": "bookbox.ink" },
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Book" } }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/books/{slug}": {
      "get": {
        "operationId": "getBook",
        "summary": "Read one book record: source, rights, chapters, frameworks, exercises, and skills.",
        "parameters": [{ "$ref": "#/components/parameters/Slug" }],
        "responses": {
          "200": {
            "description": "The book record.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Book" } } }
          },
          "404": {
            "description": "No book has that slug.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    },
    "/api/v1/books/{slug}/chat": {
      "post": {
        "operationId": "chatWithBook",
        "summary": "Ask a question and receive a cited, retrieval-based answer.",
        "description": "Send `question` for a JSON answer. Sending only `messages` (the legacy Summon shape) returns the same answer as plain text with a Sources list.",
        "parameters": [{ "$ref": "#/components/parameters/Slug" }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "question": { "type": "string", "description": "The question to answer." },
                  "source": { "type": "string", "description": "Optional. Must equal the slug in the path when present." },
                  "messages": {
                    "type": "array",
                    "description": "Legacy chat history. The latest user message is used when question is absent.",
                    "items": {
                      "type": "object",
                      "properties": {
                        "role": { "type": "string" },
                        "content": { "type": "string" }
                      }
                    }
                  }
                }
              },
              "example": { "question": "How do I handle things I cannot control?" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "A cited answer.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/ChatResponse" } },
              "text/plain": { "schema": { "type": "string" } }
            }
          },
          "400": {
            "description": "Invalid JSON, wrong field types, mismatched source, or no question.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          },
          "404": {
            "description": "No book has that slug.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    },
    "/api/v1/find": {
      "post": {
        "operationId": "findBook",
        "summary": "Find lawful access routes for a book by title and author.",
        "description": "Checks the Directory of Open Access Books, Project Gutenberg, Open Library, and Google Books. When none match, Perplexity Sonar (through OpenRouter) may resolve the book identity before the catalogs are checked again. Direct downloads are limited to verified open-access files.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["query"],
                "properties": {
                  "query": { "type": "string", "minLength": 2, "maxLength": 160, "description": "Title and, if known, author." }
                }
              },
              "example": { "query": "Meditations Marcus Aurelius" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Ranked access routes with rights classifications.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/FinderResponse" } } }
          },
          "400": {
            "description": "Invalid JSON or an invalid query.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
          }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "Slug": {
        "name": "slug",
        "in": "path",
        "required": true,
        "description": "Book slug. Currently available: meditations.",
        "schema": { "type": "string", "example": "meditations" }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": { "error": { "type": "string" } }
      },
      "Chapter": {
        "type": "object",
        "required": ["number", "title", "locator", "principle", "keywords"],
        "properties": {
          "number": { "type": "integer" },
          "title": { "type": "string" },
          "locator": { "type": "string" },
          "principle": { "type": "string", "description": "Bookbox interpretation, not quotation." },
          "keywords": { "type": "array", "items": { "type": "string" } }
        }
      },
      "Book": {
        "type": "object",
        "required": ["schemaVersion", "id", "slug", "title", "author", "source", "chapters", "links"],
        "properties": {
          "schemaVersion": { "type": "string", "example": "bookbox.book.v1" },
          "id": { "type": "string", "example": "book:meditations" },
          "slug": { "type": "string" },
          "canonicalOwner": { "type": "string" },
          "title": { "type": "string" },
          "author": { "type": "string" },
          "year": { "type": "integer" },
          "status": { "type": "string", "example": "chat-ready" },
          "description": { "type": "string" },
          "source": {
            "type": "object",
            "properties": {
              "catalogUrl": { "type": "string", "format": "uri" },
              "readUrl": { "type": "string", "format": "uri" },
              "edition": { "type": "string" },
              "rightsStatus": { "type": "string" },
              "rightsEvidence": { "type": "string" },
              "verifiedAt": { "type": "string", "format": "date" }
            }
          },
          "ingestion": { "type": "object", "additionalProperties": true },
          "distillation": { "type": "object", "additionalProperties": true },
          "frameworks": { "type": "array", "items": { "type": "object", "additionalProperties": true } },
          "exercises": { "type": "array", "items": { "type": "object", "additionalProperties": true } },
          "chapters": { "type": "array", "items": { "$ref": "#/components/schemas/Chapter" } },
          "skills": { "type": "array", "items": { "type": "object", "additionalProperties": true } },
          "compatibility": { "type": "object", "additionalProperties": true },
          "links": {
            "type": "object",
            "properties": {
              "bookboxProfile": { "type": "string" },
              "api": { "type": "string" },
              "chat": { "type": "string" }
            }
          }
        }
      },
      "Citation": {
        "type": "object",
        "required": ["id", "title", "locator", "url", "interpretation"],
        "properties": {
          "id": { "type": "string" },
          "title": { "type": "string" },
          "locator": { "type": "string" },
          "url": { "type": "string", "format": "uri" },
          "interpretation": { "type": "string" }
        }
      },
      "ChatResponse": {
        "type": "object",
        "required": ["schemaVersion", "bookId", "mode", "answer", "citations", "notice"],
        "properties": {
          "schemaVersion": { "type": "string", "const": "bookbox.chat.v1" },
          "bookId": { "type": "string" },
          "mode": { "type": "string", "const": "retrieval" },
          "answer": { "type": "string" },
          "citations": { "type": "array", "items": { "$ref": "#/components/schemas/Citation" } },
          "notice": { "type": "string" }
        }
      },
      "FinderResult": {
        "type": "object",
        "required": ["id", "provider", "providerLabel", "title", "authors", "access", "rights"],
        "properties": {
          "id": { "type": "string" },
          "provider": { "type": "string", "enum": ["doab", "gutenberg", "open-library", "google-books", "perplexity"] },
          "providerLabel": { "type": "string" },
          "title": { "type": "string" },
          "authors": { "type": "array", "items": { "type": "string" } },
          "year": { "type": "integer" },
          "identifiers": {
            "type": "object",
            "properties": { "isbn": { "type": "string" }, "providerId": { "type": "string" } }
          },
          "access": {
            "type": "object",
            "required": ["kind", "label", "url", "format", "delivery"],
            "properties": {
              "kind": { "type": "string", "enum": ["download", "free", "borrow", "purchase", "preview", "catalog"] },
              "label": { "type": "string" },
              "url": { "type": "string", "format": "uri" },
              "format": { "type": "string" },
              "delivery": { "type": "string", "enum": ["direct", "provider-page"] }
            }
          },
          "rights": {
            "type": "object",
            "required": ["status", "label", "note"],
            "properties": {
              "status": {
                "type": "string",
                "enum": [
                  "verified-open-access",
                  "verified-public-domain-us",
                  "provider-public-access",
                  "provider-lending",
                  "provider-purchase",
                  "provider-preview",
                  "catalog-only"
                ]
              },
              "label": { "type": "string" },
              "note": { "type": "string" }
            }
          }
        }
      },
      "FinderResponse": {
        "type": "object",
        "required": ["schemaVersion", "query", "status", "summary", "results", "providers", "fallbacks", "notice"],
        "properties": {
          "schemaVersion": { "type": "string", "const": "bookbox.finder.v1" },
          "query": { "type": "string" },
          "resolvedQuery": { "type": "string" },
          "status": { "type": "string", "enum": ["downloads-found", "access-found", "not-found"] },
          "summary": { "type": "string" },
          "results": { "type": "array", "items": { "$ref": "#/components/schemas/FinderResult" } },
          "providers": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["provider", "label", "state", "resultCount"],
              "properties": {
                "provider": { "type": "string" },
                "label": { "type": "string" },
                "state": { "type": "string", "enum": ["ok", "unavailable"] },
                "resultCount": { "type": "integer" },
                "message": { "type": "string" }
              }
            }
          },
          "fallbacks": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["label", "url"],
              "properties": { "label": { "type": "string" }, "url": { "type": "string", "format": "uri" } }
            }
          },
          "notice": { "type": "string" }
        }
      }
    }
  }
}
