{
  "openapi": "3.1.0",
  "info": {
    "title": "StoryKids AI API",
    "version": "1",
    "description": "Turn a child's drawings into a personalised picture book. Send one to five photos; the app measures them, proposes the characters it can see, and the adult confirms, renames or rejects every one. The outline is built from reviewed templates and approved by the adult before anything is written; the pages are then written from the confirmed characters only, checked by 28 independent checks in code, and laid out as a collage of the child's own art. Edit or rewrite any page, approve the book, export a printable PDF and share a read-only link. StoryKids never infers feelings, personality or development from a drawing, uses no third-party characters, and has no score, grade or ranking. Authenticate with an API key (Authorization: Bearer <key> or X-API-Key). Starting a story is free; one unit is billed when the written book is delivered; everything else is free. Errors are RFC 7807 problems with a stable code."
  },
  "servers": [
    {
      "url": "https://storykids.sensifai.com",
      "description": "Production"
    }
  ],
  "security": [
    {
      "bearer": []
    },
    {
      "apiKey": []
    }
  ],
  "tags": [
    {
      "name": "Readers",
      "description": "Nickname and age band only."
    },
    {
      "name": "Stories",
      "description": "Drawings in, approved book out."
    },
    {
      "name": "Pages",
      "description": "Edit or write single pages again."
    },
    {
      "name": "Jobs",
      "description": "Reading the drawings and writing the book run in the background."
    },
    {
      "name": "Sharing",
      "description": "Read-only, expiring links."
    }
  ],
  "paths": {
    "/api/v1/children": {
      "get": {
        "operationId": "listChildren",
        "tags": [
          "Readers"
        ],
        "summary": "List readers",
        "description": "Lists the readers (children) of the account: a nickname and an age band only, there is no legal name or birth date anywhere.",
        "responses": {
          "200": {
            "description": "The readers.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "children": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Child"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createChild",
        "tags": [
          "Readers"
        ],
        "summary": "Add a reader",
        "description": "Adds a reader. The age band (4-6 or 7-9) sets how short the sentences are.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChildInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Child"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: the account has the maximum number of readers.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "INVALID_INPUT.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/children/{id}": {
      "delete": {
        "operationId": "deleteChild",
        "tags": [
          "Readers"
        ],
        "summary": "Delete a reader",
        "description": "Deletes the reader with all their stories, art, shares and history rows. This cannot be undone.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Child id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Deleted."
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories": {
      "post": {
        "operationId": "createStory",
        "tags": [
          "Stories"
        ],
        "summary": "Start a story from drawings",
        "description": "Uploads one to five photos of a child's drawings (multipart field `images`, JPEG or PNG, checked by content), reads them and proposes characters for the adult to confirm. Send `consent=true` the first time to record that you are the child's parent or guardian and agree to the drawings being processed. `language` is en, de or fa, `theme` and `length` (4, 6 or 8 pages) are closed lists, `narration` is the child's own words, kept verbatim. Answers 202 with a story and a job to poll. Send an `Idempotency-Key` header so a retry never starts a second story. Starting a story is free: nothing is charged until the book is written.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "A unique key per request; the same key returns the same job.",
            "schema": {
              "type": "string",
              "maxLength": 128
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": [
                  "images",
                  "child_id",
                  "language",
                  "theme",
                  "length"
                ],
                "properties": {
                  "images": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 5,
                    "items": {
                      "type": "string",
                      "format": "binary"
                    }
                  },
                  "child_id": {
                    "type": "string"
                  },
                  "language": {
                    "type": "string",
                    "enum": [
                      "en",
                      "de",
                      "fa"
                    ]
                  },
                  "theme": {
                    "type": "string",
                    "enum": [
                      "friendship",
                      "kindness",
                      "courage",
                      "sharing",
                      "curiosity",
                      "teamwork",
                      "bedtime",
                      "nature"
                    ]
                  },
                  "length": {
                    "type": "integer",
                    "enum": [
                      4,
                      6,
                      8
                    ]
                  },
                  "narration": {
                    "type": "string",
                    "maxLength": 1000
                  },
                  "narration_author": {
                    "type": "string",
                    "enum": [
                      "child",
                      "parent"
                    ]
                  },
                  "aliases": {
                    "type": "string",
                    "description": "A JSON list of {name, alias}: a parent-approved name the story uses instead of a character's name (at most 4)."
                  },
                  "consent": {
                    "type": "boolean"
                  },
                  "keep_art": {
                    "type": "boolean",
                    "description": "Keep the book's art beyond 24 hours; the default is the account setting."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Queued.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Accepted"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such reader.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "415": {
            "description": "UNSUPPORTED: a file is not a JPEG or PNG.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "INVALID_INPUT.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "402": {
            "description": "PAYMENT_REQUIRED: the prepaid wallet is empty (top_up_url).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "NEEDS_CONSENT, or no active subscription (subscribe_url).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      },
      "get": {
        "operationId": "listStories",
        "tags": [
          "Stories"
        ],
        "summary": "List stories",
        "description": "Lists the stories, newest first. Filter with `child_id`, `state` (analyzing, characters, outline, writing, draft, final) and `saved=1`.",
        "parameters": [
          {
            "name": "child_id",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "state",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "maximum": 100
            }
          },
          {
            "name": "offset",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The stories.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "stories": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/StorySummary"
                      }
                    },
                    "total": {
                      "type": "integer"
                    },
                    "limit": {
                      "type": "integer"
                    },
                    "offset": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}": {
      "get": {
        "operationId": "getStory",
        "tags": [
          "Stories"
        ],
        "summary": "Get a story",
        "description": "Returns the story with its latest book (or `?revision=N`): characters, outline, pages and the 28 automatic checks (`checks_passed` of `checks_total`), whether it can be approved, exported or shared, and what blocks approval.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The story.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "deleteStory",
        "tags": [
          "Stories"
        ],
        "summary": "Delete a story",
        "description": "Deletes the story with its revisions, art, shares and history row. This cannot be undone.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Deleted."
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/characters": {
      "patch": {
        "operationId": "editCharacters",
        "tags": [
          "Stories"
        ],
        "summary": "Confirm, rename, add or reject characters",
        "description": "Applies up to 20 edits (`confirm`, `rename`, `describe`, `alias`, `reject`, `reset`, `add`, `region`). Characters found by AI start as `proposed` and are never used until confirmed. At most four characters can be confirmed. Wording that fails the safety filters is not applied and a note says so. Allowed until the outline is approved.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CharacterEdits"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The story with a new draft revision.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: the outline is approved, the story is written, or more than four characters.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "INVALID_INPUT.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/outline": {
      "post": {
        "operationId": "buildOutline",
        "tags": [
          "Stories"
        ],
        "summary": "Build the story outline",
        "description": "Builds the beats from the confirmed characters, the theme and the length, with reviewed titles and goals. No model is called. An existing outline is rebuilt and no longer approved.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The story with its outline.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "NOT_CONFIRMED: confirm a character first; or CONFLICT: the story is already written.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/outline/approve": {
      "post": {
        "operationId": "approveOutline",
        "tags": [
          "Stories"
        ],
        "summary": "Approve the outline",
        "description": "Marks the outline approved. Nothing is written before the adult approves it.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The story with an approved outline.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "NOT_CONFIRMED: build the outline first.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/write": {
      "post": {
        "operationId": "writeStory",
        "tags": [
          "Stories"
        ],
        "summary": "Write the story",
        "description": "Writes the pages from the confirmed characters, the theme and the approved outline. Answers 202 with a job to poll. One unit is billed after the book is delivered, under the identifier minted here; a failed write, a refusal and a cancelled job are never charged. A story that is already written is not billed again: use the app's rewrite instead.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "A unique key per request; the same key returns the same job.",
            "schema": {
              "type": "string",
              "maxLength": 128
            }
          }
        ],
        "responses": {
          "202": {
            "description": "Queued.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Accepted"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "NOT_CONFIRMED: approve the outline first; CONFLICT: already written or being written.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "402": {
            "description": "PAYMENT_REQUIRED: the prepaid wallet is empty (top_up_url).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "NEEDS_CONSENT, or no active subscription (subscribe_url).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/pages/{n}": {
      "patch": {
        "operationId": "editPage",
        "tags": [
          "Pages"
        ],
        "summary": "Edit a page",
        "description": "Replaces a page's text with your own (at most 400 characters). Text that fails the safety filters is not applied and a note says so. Every edit makes a new draft revision, free.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "n",
            "in": "path",
            "required": true,
            "description": "Page number, from 1.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "text"
                ],
                "properties": {
                  "text": {
                    "type": "string",
                    "maxLength": 400
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The story with a new draft revision.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: the story is not written yet.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/pages/{n}/regenerate": {
      "post": {
        "operationId": "regeneratePage",
        "tags": [
          "Pages"
        ],
        "summary": "Write one page again",
        "description": "Asks the model for a new version of one page, at most 3 times per page and 12 per story; an unsafe result is replaced by a reviewed template page. Free.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "n",
            "in": "path",
            "required": true,
            "description": "Page number, from 1.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The story with a new draft revision.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: a regeneration limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "503": {
            "description": "PROVIDER_UNAVAILABLE: the writer is unavailable; nothing was changed.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/approve": {
      "post": {
        "operationId": "approveStory",
        "tags": [
          "Stories"
        ],
        "summary": "Approve the book",
        "description": "Makes the latest draft the final revision. Fails with `blockers` while a check that must pass is failing (safety, character ids, length). Exports and share links use the final revision only. Idempotent.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The story with a final revision.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/StoryView"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: a hard check fails (see `blockers`).",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/exports": {
      "post": {
        "operationId": "exportStory",
        "tags": [
          "Stories"
        ],
        "summary": "Export the printable PDF",
        "description": "Renders the latest final revision as an A4 PDF picture book: a cover, one page per story page (a crop of the child's own drawing and the text), a character sheet and an about page. Pages whose art expired show a wash of the measured colours. Persian text is not printed in the PDF yet; the page says so.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The PDF (attachment).",
            "content": {
              "application/pdf": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "NOT_CONFIRMED: approve the story first.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/stories/{id}/shares": {
      "post": {
        "operationId": "createShare",
        "tags": [
          "Sharing"
        ],
        "summary": "Create a read-only share link",
        "description": "Creates a link to the latest final revision that expires after 7, 30 or 90 days and can be revoked. The token is returned once. Needs the sharing consent (send `consent_sharing=true` the first time).",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Story id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ShareInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ShareCreated"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND: no such story for this account.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "NOT_CONFIRMED: approve the story first.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "403": {
            "description": "NEEDS_CONSENT.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "422": {
            "description": "INVALID_INPUT.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}": {
      "get": {
        "operationId": "getJob",
        "tags": [
          "Jobs"
        ],
        "summary": "Get a job",
        "description": "Polls a job. `kind` is `characters` (reading the drawings, free) or `write` (writing the book, billed on success). States: queued, running, needs_input (the drawings were readable but no character was found: add one by hand, free), completed, failed, cancelled.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Job id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/jobs/{id}/cancel": {
      "post": {
        "operationId": "cancelJob",
        "tags": [
          "Jobs"
        ],
        "summary": "Cancel a job",
        "description": "Stops a queued or running job. Nothing is charged for a cancelled job.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "Job id.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The job.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Job"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "404": {
            "description": "NOT_FOUND.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "409": {
            "description": "CONFLICT: the job has already finished.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          },
          "429": {
            "description": "QUOTA_EXCEEDED: an hourly limit was reached.",
            "content": {
              "application/problem+json": {
                "schema": {
                  "$ref": "#/components/schemas/Problem"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer"
      },
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key"
      }
    },
    "schemas": {
      "Problem": {
        "type": "object",
        "required": [
          "code",
          "status"
        ],
        "properties": {
          "type": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "code": {
            "type": "string",
            "enum": [
              "INVALID_INPUT",
              "NEEDS_CONSENT",
              "UNSUPPORTED",
              "QUOTA_EXCEEDED",
              "PROVIDER_UNAVAILABLE",
              "NOT_FOUND",
              "NOT_CONFIRMED",
              "CONFLICT",
              "GONE",
              "UNAUTHORIZED",
              "FORBIDDEN",
              "PAYMENT_REQUIRED"
            ]
          },
          "detail": {
            "type": "string"
          },
          "blockers": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "Present on a refused approval: the checks that must pass."
          },
          "errors": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "field": {
                  "type": "string"
                },
                "message": {
                  "type": "string"
                }
              }
            }
          },
          "timestamp": {
            "type": "integer"
          }
        }
      },
      "ChildInput": {
        "type": "object",
        "required": [
          "nickname",
          "age_band"
        ],
        "properties": {
          "nickname": {
            "type": "string",
            "maxLength": 40
          },
          "age_band": {
            "type": "string",
            "enum": [
              "4-6",
              "7-9"
            ]
          }
        }
      },
      "Child": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "nickname": {
            "type": "string"
          },
          "age_band": {
            "type": "string"
          },
          "age_label": {
            "type": "string"
          },
          "archived": {
            "type": "boolean"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "stories": {
            "type": "integer"
          },
          "finals": {
            "type": "integer"
          },
          "last_story_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      },
      "Accepted": {
        "type": "object",
        "properties": {
          "story_id": {
            "type": "string"
          },
          "job_id": {
            "type": "string"
          },
          "state": {
            "type": "string"
          },
          "poll": {
            "type": "string"
          }
        }
      },
      "Job": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "characters",
              "write"
            ]
          },
          "state": {
            "type": "string",
            "enum": [
              "queued",
              "running",
              "needs_input",
              "completed",
              "failed",
              "cancelled"
            ]
          },
          "message": {
            "type": "string"
          },
          "story_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "child_id": {
            "type": "string"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "finished_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "charged": {
            "type": "boolean"
          },
          "units": {
            "type": "integer"
          }
        }
      },
      "CharacterRef": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "proposed",
              "confirmed",
              "edited",
              "added"
            ]
          }
        }
      },
      "StorySummary": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "child_id": {
            "type": "string"
          },
          "child_nickname": {
            "type": "string"
          },
          "age_band": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "language": {
            "type": "string"
          },
          "theme": {
            "type": "string"
          },
          "length": {
            "type": "integer"
          },
          "state": {
            "type": "string",
            "enum": [
              "analyzing",
              "characters",
              "outline",
              "writing",
              "draft",
              "final"
            ]
          },
          "saved": {
            "type": "boolean"
          },
          "has_art": {
            "type": "boolean"
          },
          "art_expires_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "cover_url": {
            "type": [
              "string",
              "null"
            ]
          },
          "characters": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/CharacterRef"
            }
          },
          "palette": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Colour"
            }
          },
          "revision_no": {
            "type": "integer"
          },
          "final_revision_no": {
            "type": [
              "integer",
              "null"
            ]
          },
          "pages": {
            "type": "integer"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "job_id": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "Colour": {
        "type": "object",
        "properties": {
          "hex": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "family": {
            "type": "string"
          },
          "share_percent": {
            "type": "integer"
          }
        }
      },
      "StoryView": {
        "type": "object",
        "properties": {
          "story": {
            "$ref": "#/components/schemas/StorySummary"
          },
          "book": {
            "$ref": "#/components/schemas/Book"
          },
          "revisions": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "no": {
                  "type": "integer"
                },
                "status": {
                  "type": "string",
                  "enum": [
                    "draft",
                    "final"
                  ]
                },
                "created_at": {
                  "type": "string",
                  "format": "date-time"
                }
              }
            }
          },
          "final_revision_no": {
            "type": [
              "integer",
              "null"
            ]
          },
          "can_export": {
            "type": "boolean"
          },
          "can_share": {
            "type": "boolean"
          },
          "can_approve": {
            "type": "boolean"
          },
          "blockers": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "shares": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Share"
            }
          }
        }
      },
      "Character": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "alias": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "figure",
              "animal",
              "creature",
              "vehicle",
              "object",
              "plant",
              "nature",
              "other"
            ]
          },
          "description": {
            "type": "string"
          },
          "drawing_index": {
            "type": "integer"
          },
          "region": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "x": {
                "type": "number"
              },
              "y": {
                "type": "number"
              },
              "w": {
                "type": "number"
              },
              "h": {
                "type": "number"
              }
            }
          },
          "colors": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "source": {
            "type": "string",
            "enum": [
              "ai",
              "adult"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "proposed",
              "confirmed",
              "edited",
              "rejected",
              "added"
            ]
          },
          "crop_url": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "Beat": {
        "type": "object",
        "properties": {
          "n": {
            "type": "integer"
          },
          "id": {
            "type": "string",
            "enum": [
              "opening",
              "wish",
              "journey",
              "obstacle",
              "help",
              "try_again",
              "solution",
              "ending"
            ]
          },
          "title": {
            "type": "string"
          },
          "goal": {
            "type": "string"
          },
          "setting": {
            "type": "string",
            "enum": [
              "garden",
              "forest",
              "meadow",
              "beach",
              "sea",
              "mountain",
              "village",
              "city",
              "home",
              "school",
              "playground",
              "sky",
              "space",
              "cave",
              "castle",
              "farm",
              "river"
            ]
          },
          "character_ids": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "Page": {
        "type": "object",
        "properties": {
          "number": {
            "type": "integer"
          },
          "text": {
            "type": "string"
          },
          "character_ids": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "scene": {
            "type": "object",
            "properties": {
              "setting": {
                "type": "string"
              },
              "text": {
                "type": "string"
              }
            }
          },
          "art": {
            "type": "object",
            "properties": {
              "drawing_index": {
                "type": "integer"
              },
              "character_id": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "region": {
                "type": [
                  "object",
                  "null"
                ],
                "properties": {
                  "x": {
                    "type": "number"
                  },
                  "y": {
                    "type": "number"
                  },
                  "w": {
                    "type": "number"
                  },
                  "h": {
                    "type": "number"
                  }
                }
              },
              "wash": {
                "type": "boolean"
              }
            }
          },
          "template": {
            "type": "boolean"
          },
          "edited": {
            "type": "boolean"
          },
          "regenerations": {
            "type": "integer"
          },
          "art_locked": {
            "type": "boolean"
          }
        }
      },
      "Check": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "label": {
            "type": "string"
          },
          "ok": {
            "type": "boolean"
          },
          "hard": {
            "type": "boolean",
            "description": "A failing hard check blocks approval."
          },
          "detail": {
            "type": "string"
          }
        }
      },
      "Book": {
        "type": "object",
        "description": "One revision of a story. There is no score, grade or interpretation of the child anywhere in it.",
        "properties": {
          "version": {
            "type": "string"
          },
          "revision": {
            "type": "integer"
          },
          "status": {
            "type": "string",
            "enum": [
              "draft",
              "final"
            ]
          },
          "language": {
            "type": "string",
            "enum": [
              "en",
              "de",
              "fa"
            ]
          },
          "age_band": {
            "type": "string"
          },
          "theme": {
            "type": "string"
          },
          "length": {
            "type": "integer"
          },
          "title": {
            "type": "string"
          },
          "title_by_ai": {
            "type": "boolean"
          },
          "characters": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Character"
            }
          },
          "outline": {
            "type": "object",
            "properties": {
              "revision": {
                "type": "integer"
              },
              "theme": {
                "type": "string"
              },
              "length": {
                "type": "integer"
              },
              "beats": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Beat"
                }
              },
              "approved": {
                "type": "boolean"
              }
            }
          },
          "pages": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Page"
            }
          },
          "unknowns": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "narration": {
            "type": "object",
            "description": "The child's own words, kept verbatim.",
            "properties": {
              "text": {
                "type": "string"
              },
              "author": {
                "type": "string",
                "enum": [
                  "child",
                  "parent"
                ]
              },
              "sha256": {
                "type": "string"
              }
            }
          },
          "checks": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Check"
            }
          },
          "checks_passed": {
            "type": "integer"
          },
          "checks_total": {
            "type": "integer"
          },
          "reading": {
            "type": "object",
            "description": "Readability of the text, measured in Go; English only.",
            "properties": {
              "words": {
                "type": "integer"
              },
              "sentences": {
                "type": "integer"
              },
              "avg_sentence_words": {
                "type": "number"
              },
              "max_sentence_words": {
                "type": "integer"
              },
              "avg_word_len": {
                "type": "number"
              },
              "band_max_sentence": {
                "type": "integer"
              },
              "english_only": {
                "type": "boolean"
              }
            }
          },
          "notes": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "images": {
            "type": "integer"
          },
          "palette": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Colour"
            }
          },
          "provenance": {
            "type": "object",
            "properties": {
              "model": {
                "type": "string"
              },
              "prompt_version": {
                "type": "string"
              },
              "schema_version": {
                "type": "string"
              },
              "engine_version": {
                "type": "string"
              }
            }
          },
          "rewrites": {
            "type": "integer"
          },
          "identifying_text_visible": {
            "type": "boolean"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "CharacterEdits": {
        "type": "object",
        "required": [
          "edits"
        ],
        "properties": {
          "edits": {
            "type": "array",
            "maxItems": 20,
            "items": {
              "type": "object",
              "required": [
                "op"
              ],
              "properties": {
                "id": {
                  "type": "string"
                },
                "op": {
                  "type": "string",
                  "enum": [
                    "confirm",
                    "rename",
                    "describe",
                    "alias",
                    "reject",
                    "reset",
                    "add",
                    "region"
                  ]
                },
                "name": {
                  "type": "string",
                  "maxLength": 30
                },
                "alias": {
                  "type": "string",
                  "maxLength": 30
                },
                "description": {
                  "type": "string",
                  "maxLength": 120
                },
                "kind": {
                  "type": "string",
                  "enum": [
                    "figure",
                    "animal",
                    "creature",
                    "vehicle",
                    "object",
                    "plant",
                    "nature",
                    "other"
                  ]
                },
                "drawing_index": {
                  "type": "integer"
                },
                "region": {
                  "type": "object",
                  "properties": {
                    "x": {
                      "type": "number"
                    },
                    "y": {
                      "type": "number"
                    },
                    "w": {
                      "type": "number"
                    },
                    "h": {
                      "type": "number"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "ShareInput": {
        "type": "object",
        "required": [
          "days"
        ],
        "properties": {
          "days": {
            "type": "integer",
            "enum": [
              7,
              30,
              90
            ]
          },
          "include_art": {
            "type": "boolean",
            "description": "Show the child's art in the shared page; default true."
          },
          "consent_sharing": {
            "type": "boolean"
          }
        }
      },
      "Share": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "story_id": {
            "type": "string"
          },
          "revision_no": {
            "type": "integer"
          },
          "expires_at": {
            "type": "string",
            "format": "date-time"
          },
          "include_art": {
            "type": "boolean"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "revoked_at": {
            "type": "string",
            "format": "date-time"
          },
          "views": {
            "type": "integer"
          },
          "status": {
            "type": "string",
            "enum": [
              "active",
              "expired",
              "revoked"
            ]
          }
        }
      },
      "ShareCreated": {
        "type": "object",
        "properties": {
          "share": {
            "$ref": "#/components/schemas/Share"
          },
          "url": {
            "type": "string"
          },
          "token": {
            "type": "string",
            "description": "Shown once."
          },
          "expires_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    }
  }
}
