{
  "openapi": "3.1.0",
  "info": {
    "title": "AIStoryHub API",
    "version": "1.0.0",
    "description": "A REST API for reading and creating stories and chapters on AIStoryHub outside the app. Authenticate with a scoped bearer token created under Settings, Developer API. Scopes are explicit, so write access does not imply read access.",
    "contact": {
      "url": "https://aistoryhub.co/developers"
    },
    "termsOfService": "https://aistoryhub.co/terms"
  },
  "servers": [
    { "url": "https://aistoryhub.co/api/v1" }
  ],
  "security": [{ "bearerAuth": [] }],
  "tags": [
    { "name": "account", "description": "The authenticated token's owner." },
    { "name": "stories", "description": "Stories owned by the token holder." },
    { "name": "chapters", "description": "Chapters and scenes within a story." },
    { "name": "world", "description": "World-building entries (characters, locations, lore, etc.)." },
    { "name": "ai", "description": "AI generation and revision. Runs on your own BYOK provider key." }
  ],
  "paths": {
    "/me": {
      "get": {
        "operationId": "getMe",
        "tags": ["account"],
        "summary": "Get the token owner's account",
        "x-required-scope": "account:read",
        "responses": {
          "200": {
            "description": "The token owner's account.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Account" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories": {
      "get": {
        "operationId": "listStories",
        "tags": ["stories"],
        "summary": "List your stories",
        "x-required-scope": "stories:read",
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Max results per page.",
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 }
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Number of results to skip.",
            "schema": { "type": "integer", "minimum": 0, "default": 0 }
          }
        ],
        "responses": {
          "200": {
            "description": "A page of stories, newest updated first.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "pagination"],
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Story" } },
                    "pagination": { "$ref": "#/components/schemas/Pagination" }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "post": {
        "operationId": "createStory",
        "tags": ["stories"],
        "summary": "Create a story",
        "x-required-scope": "stories:write",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/StoryCreate" } }
          }
        },
        "responses": {
          "201": {
            "description": "The created story.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Story" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories/{id}": {
      "get": {
        "operationId": "getStory",
        "tags": ["stories"],
        "summary": "Fetch a single story",
        "x-required-scope": "stories:read",
        "parameters": [{ "$ref": "#/components/parameters/StoryId" }],
        "responses": {
          "200": {
            "description": "The story.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Story" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories/{id}/chapters": {
      "get": {
        "operationId": "listChapters",
        "tags": ["chapters"],
        "summary": "List a story's chapters and scenes",
        "description": "Content is omitted from list results; fetch a single chapter for its `content`.",
        "x-required-scope": "stories:read",
        "parameters": [{ "$ref": "#/components/parameters/StoryId" }],
        "responses": {
          "200": {
            "description": "The story's chapters and scenes, in manuscript order.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/ChapterSummary" } }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories/{id}/chapters/{chapterId}": {
      "get": {
        "operationId": "getChapter",
        "tags": ["chapters"],
        "summary": "Fetch one chapter with its content",
        "x-required-scope": "stories:read",
        "parameters": [
          { "$ref": "#/components/parameters/StoryId" },
          {
            "name": "chapterId",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "The chapter, including its content.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ChapterDetail" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories/{id}/world": {
      "get": {
        "operationId": "listWorld",
        "tags": ["world"],
        "summary": "List world-building entries",
        "x-required-scope": "world:read",
        "parameters": [
          { "$ref": "#/components/parameters/StoryId" },
          {
            "name": "type",
            "in": "query",
            "description": "Filter by world type.",
            "schema": { "type": "string", "enum": ["character", "species", "faction", "location", "item", "lore", "timeline"] }
          }
        ],
        "responses": {
          "200": {
            "description": "World entries.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": { "data": { "type": "array", "items": { "$ref": "#/components/schemas/WorldNode" } } }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "post": {
        "operationId": "createWorld",
        "tags": ["world"],
        "summary": "Create a world-building entry",
        "x-required-scope": "world:write",
        "parameters": [{ "$ref": "#/components/parameters/StoryId" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WorldNodeCreate" } } }
        },
        "responses": {
          "201": {
            "description": "The created world entry.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WorldNode" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/stories/{id}/world/{nodeId}": {
      "get": {
        "operationId": "getWorldNode",
        "tags": ["world"],
        "summary": "Fetch a world-building entry",
        "x-required-scope": "world:read",
        "parameters": [
          { "$ref": "#/components/parameters/StoryId" },
          { "name": "nodeId", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
        ],
        "responses": {
          "200": { "description": "The world entry.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WorldNode" } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "patch": {
        "operationId": "updateWorldNode",
        "tags": ["world"],
        "summary": "Update a world-building entry",
        "x-required-scope": "world:write",
        "parameters": [
          { "$ref": "#/components/parameters/StoryId" },
          { "name": "nodeId", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": { "type": "string" },
                  "content": { "type": "string" },
                  "tags": { "type": "array", "items": { "type": "string" } },
                  "metadata": { "type": "object", "additionalProperties": true }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "The updated world entry.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WorldNode" } } } },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "delete": {
        "operationId": "deleteWorldNode",
        "tags": ["world"],
        "summary": "Delete a world-building entry",
        "x-required-scope": "world:write",
        "parameters": [
          { "$ref": "#/components/parameters/StoryId" },
          { "name": "nodeId", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
        ],
        "responses": {
          "200": { "description": "Deleted.", "content": { "application/json": { "schema": { "type": "object", "properties": { "success": { "type": "boolean" } } } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/ai/generate": {
      "post": {
        "operationId": "aiGenerate",
        "tags": ["ai"],
        "summary": "Generate prose (Generate Pro)",
        "description": "Runs on the token owner's own AI provider key (BYOK).",
        "x-required-scope": "ai:generate",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["story_id", "instruction"],
                "properties": {
                  "story_id": { "type": "string", "format": "uuid" },
                  "instruction": { "type": "string" },
                  "chapter_id": { "type": "string", "format": "uuid" },
                  "file_type": { "type": "string", "default": "chapter" },
                  "file_name": { "type": "string" },
                  "special_instructions": { "type": "string" },
                  "references": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": { "id": { "type": "string" }, "type": { "type": "string" } }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "The generated prose.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AiText" } } } },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/ai/revise": {
      "post": {
        "operationId": "aiRevise",
        "tags": ["ai"],
        "summary": "Revise text against an instruction",
        "description": "Runs on the token owner's own AI provider key (BYOK).",
        "x-required-scope": "ai:revise",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["story_id", "text", "instruction"],
                "properties": {
                  "story_id": { "type": "string", "format": "uuid" },
                  "text": { "type": "string" },
                  "instruction": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "The revised text.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AiText" } } } },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/ai/grit-pass": {
      "post": {
        "operationId": "aiGritPass",
        "tags": ["ai"],
        "summary": "Run the Human Grit Pass over text",
        "description": "Runs on the token owner's own AI provider key (BYOK).",
        "x-required-scope": "ai:revise",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["story_id", "text"],
                "properties": {
                  "story_id": { "type": "string", "format": "uuid" },
                  "text": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "The grit-passed rewrite.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/AiText" } } } },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/ai/editorial-pass": {
      "post": {
        "operationId": "aiEditorialPass",
        "tags": ["ai"],
        "summary": "Get structured editorial notes on a chapter",
        "description": "Runs on the token owner's own AI provider key (BYOK). Notes are grounded in the story bible and the author's voice style.",
        "x-required-scope": "ai:editorial",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["story_id", "paragraphs"],
                "properties": {
                  "story_id": { "type": "string", "format": "uuid" },
                  "paragraphs": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "The chapter split into text blocks; note anchors reference these by index."
                  },
                  "file_name": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Editorial notes, fence list, and scope limits.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/EditorialResult" } } }
          },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "ash_...",
        "description": "Create a token under Settings, Developer API and send it as `Authorization: Bearer ash_...`. Each endpoint requires one scope, listed per-operation via the `x-required-scope` extension; see https://aistoryhub.co/developers for the full scope list."
      }
    },
    "parameters": {
      "StoryId": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": { "type": "string", "format": "uuid" }
      }
    },
    "schemas": {
      "Account": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "email": { "type": ["string", "null"], "format": "email" },
          "plan": { "type": "string", "enum": ["free", "paid", "admin"] },
          "token_scopes": { "type": "array", "items": { "type": "string" } }
        }
      },
      "Story": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "title": { "type": "string" },
          "description": { "type": ["string", "null"] },
          "genre": { "type": ["string", "null"] },
          "tense": { "type": ["string", "null"] },
          "language": { "type": ["string", "null"] },
          "mood": { "type": ["string", "null"] },
          "time_period": { "type": ["string", "null"] },
          "tone": { "type": ["string", "null"] },
          "pov": { "type": ["string", "null"] },
          "status": { "type": ["string", "null"] },
          "is_public": { "type": "boolean" },
          "word_count": { "type": "integer" },
          "target_word_count": { "type": ["integer", "null"] },
          "cover_image_url": { "type": ["string", "null"], "format": "uri" },
          "created_at": { "type": ["string", "null"], "format": "date-time" },
          "updated_at": { "type": ["string", "null"], "format": "date-time" }
        }
      },
      "StoryCreate": {
        "type": "object",
        "required": ["title"],
        "properties": {
          "title": { "type": "string" },
          "description": { "type": "string" },
          "genre": { "type": "string" },
          "tense": { "type": "string" },
          "language": { "type": "string" },
          "mood": { "type": "string" },
          "time_period": { "type": "string" },
          "tone": { "type": "string" },
          "pov": { "type": "string" },
          "banned_words": { "type": "array", "items": { "type": "string" } },
          "target_word_count": { "type": "integer", "default": 50000 }
        }
      },
      "ChapterSummary": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "title": { "type": "string" },
          "type": { "type": "string", "enum": ["chapter", "scene"] },
          "order": { "type": "integer" },
          "word_count": { "type": "integer" },
          "updated_at": { "type": ["string", "null"], "format": "date-time" }
        }
      },
      "ChapterDetail": {
        "allOf": [
          { "$ref": "#/components/schemas/ChapterSummary" },
          {
            "type": "object",
            "properties": {
              "content": { "type": "string" }
            }
          }
        ]
      },
      "Pagination": {
        "type": "object",
        "properties": {
          "limit": { "type": "integer" },
          "offset": { "type": "integer" },
          "total": { "type": "integer" }
        }
      },
      "WorldNode": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "type": { "type": "string", "enum": ["character", "species", "faction", "location", "item", "lore", "timeline"] },
          "title": { "type": "string" },
          "content": { "type": "string" },
          "tags": { "type": "array", "items": { "type": "string" } },
          "metadata": { "type": "object", "additionalProperties": true },
          "order": { "type": "integer" },
          "created_at": { "type": ["string", "null"], "format": "date-time" },
          "updated_at": { "type": ["string", "null"], "format": "date-time" }
        }
      },
      "WorldNodeCreate": {
        "type": "object",
        "required": ["type", "title"],
        "properties": {
          "type": { "type": "string", "enum": ["character", "species", "faction", "location", "item", "lore", "timeline"] },
          "title": { "type": "string" },
          "content": { "type": "string" },
          "tags": { "type": "array", "items": { "type": "string" } },
          "metadata": { "type": "object", "additionalProperties": true }
        }
      },
      "AiText": {
        "type": "object",
        "properties": { "text": { "type": "string" } }
      },
      "EditorialNote": {
        "type": "object",
        "description": "A single editorial note anchored to the chapter text by verbatim quote.",
        "properties": {
          "category": { "type": "string" },
          "scope": { "type": "string", "enum": ["line", "structural"] },
          "confidence": { "type": "string", "enum": ["high", "medium", "low"] },
          "note": { "type": "string" },
          "suggestedInstruction": { "type": "string" },
          "anchorStart": { "type": "string" },
          "anchorEnd": { "type": "string" },
          "paragraphIndex": { "type": "integer" },
          "paragraphIndexEnd": { "type": "integer" }
        },
        "additionalProperties": true
      },
      "EditorialResult": {
        "type": "object",
        "properties": {
          "notes": { "type": "array", "items": { "$ref": "#/components/schemas/EditorialNote" } },
          "fence_list": { "type": "array", "items": { "type": "string" } },
          "scope_limits": {}
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "enum": ["unauthorized", "forbidden", "invalid_request", "not_found", "rate_limited", "internal_error"]
              },
              "message": { "type": "string" }
            }
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Missing, malformed, invalid, revoked, or expired token.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Forbidden": {
        "description": "The token lacks the scope this endpoint requires.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "InvalidRequest": {
        "description": "The request body or parameters are invalid.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NotFound": {
        "description": "The resource does not exist or you do not own it.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RateLimited": {
        "description": "Too many requests. Retry after the delay in the `Retry-After` header.",
        "headers": {
          "Retry-After": { "schema": { "type": "integer" }, "description": "Seconds to wait before retrying." }
        },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    }
  }
}
