openapi: 3.1.0
info:
  title: Relettr API
  version: "1"
  summary: Translate manhwa chapters (Korean comics) into lettered English.
  description: |
    The Relettr public REST API, version 1.

    Every request is authenticated with an API key sent as a bearer token
    (`rl_live_…` or `rl_test_…`). Credits are integers; 100 credits = $1.

    Within v1 only additive changes are made: new endpoints, new optional
    fields, new event types and new enum values. Ignore unknown fields, and
    treat an unknown chapter `status` as non-terminal.
  contact:
    name: Relettr support
    email: support@relettr.com
servers:
  - url: https://api.relettr.com/v1
    description: Production (live and test keys)
security:
  - bearer: []
tags:
  - name: Chapters
    description: Create, upload, quote, start, poll and download a chapter.
  - name: Regions and review
    description: Read and edit the translated text, re-render, and resolve failed-mask zones.
  - name: Series
    description: Series hold the binding glossary and the context shared between chapters.
  - name: Glossary
    description: Glossary entries of a series.
  - name: Webhooks
    description: Registered webhook endpoints.
  - name: Events
    description: The events that are delivered to webhooks, as a list you can page through.
  - name: Credits
    description: Balance, usage, ledger and price estimates.
  - name: Status
    description: Service status. No authentication.
  - name: Test helpers
    description: Endpoints that exist only for `rl_test_` keys.

paths:
  /chapters:
    post:
      tags: [Chapters]
      operationId: createChapter
      summary: Create a chapter
      description: |
        Creates a chapter, declares its files in reading order, and returns one
        presigned `PUT` URL per file. Upload each file to its URL with exactly
        the `Content-Type` and `Content-Length` you declared, then call
        `POST /chapters/{id}/uploads/complete`.

        `Idempotency-Key` is **required** on this endpoint.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKeyRequired'
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/ChapterCreate'}
            example:
              source_language: ko
              series_id: ser_3hT9aB7cD1eF2gH3iJ4kL5
              number: 42
              layout: strip
              translate_sfx: false
              output: {layout: original, format: webp, quality: 90}
              auto_start: true
              max_credits: 600
              files:
                - {index: 1, filename: 001.jpg, content_type: image/jpeg, size_bytes: 812345}
                - {index: 2, filename: 002.jpg, content_type: image/jpeg, size_bytes: 790111}
              metadata: {source_ref: tower-42}
      responses:
        '201':
          description: The chapter, in `awaiting_upload`, with its upload URLs.
          headers:
            Request-Id: {$ref: '#/components/headers/Request-Id'}
            Idempotent-Replayed: {$ref: '#/components/headers/Idempotent-Replayed'}
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '413': {$ref: '#/components/responses/TooLarge'}
        '415': {$ref: '#/components/responses/UnsupportedMediaType'}
        '422': {$ref: '#/components/responses/Unprocessable'}
        '429': {$ref: '#/components/responses/RateLimited'}
    get:
      tags: [Chapters]
      operationId: listChapters
      summary: List chapters
      description: Lists chapters, newest first.
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
        - name: status
          in: query
          description: Only chapters in this status.
          schema: {$ref: '#/components/schemas/ChapterStatus'}
        - name: series_id
          in: query
          description: Only chapters of this series.
          schema: {type: string}
        - name: created[gte]
          in: query
          description: Only chapters created at or after this time.
          schema: {type: string, format: date-time}
      responses:
        '200':
          description: A page of chapters.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/Chapter'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    get:
      tags: [Chapters]
      operationId: getChapter
      summary: Retrieve a chapter
      description: |
        Returns status, progress, quote, credits, queue position, review state,
        warnings and error. The response carries an `ETag`; send it back as
        `If-None-Match` to get `304 Not Modified` with no body. A 304 counts as
        0.2 of a request against your rate limit.

        Poll every 30–60 s while `queued` and every 5–10 s while `processing`,
        or use webhooks.
      parameters:
        - name: If-None-Match
          in: header
          required: false
          schema: {type: string}
      responses:
        '200':
          description: The chapter.
          headers:
            ETag: {$ref: '#/components/headers/ETag'}
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '304': {description: Not modified since the given ETag.}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '404': {$ref: '#/components/responses/NotFound'}
        '410': {$ref: '#/components/responses/Gone'}
        '429': {$ref: '#/components/responses/RateLimited'}
    patch:
      tags: [Chapters]
      operationId: updateChapter
      summary: Update a chapter
      description: |
        Changes options or metadata. Allowed only in `awaiting_upload` and
        `ready`. Changing options on a `ready` chapter re-computes the quote.
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/ChapterUpdate'}
      responses:
        '200':
          description: The updated chapter.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}
    delete:
      tags: [Chapters]
      operationId: deleteChapter
      summary: Delete a chapter
      description: |
        Cancels the chapter if it is running (finished steps are charged, the
        rest of the hold is released), then deletes its images, text and
        results from our storage. Afterwards `GET` returns `410 gone`.
        Deleting an already deleted chapter returns `200`.

        Ledger entries are kept, because they are financial records. They
        hold credits and ids only, no content.
      responses:
        '200':
          description: Deleted.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Deleted'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/upload_urls:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Chapters]
      operationId: createUploadUrls
      summary: Issue fresh upload URLs
      description: |
        Upload URLs expire 15 minutes after they are issued. This returns new
        ones for the given file indexes, or for every file if `indexes` is
        omitted. Allowed only in `awaiting_upload`.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                indexes:
                  type: array
                  items: {type: integer, minimum: 1, maximum: 300}
      responses:
        '200':
          description: The new upload URLs.
          content:
            application/json:
              schema:
                type: object
                properties:
                  object: {const: list}
                  data:
                    type: array
                    items: {$ref: '#/components/schemas/Upload'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/uploads/complete:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Chapters]
      operationId: completeUploads
      summary: Mark uploads complete
      description: |
        Tells the server every file is uploaded. The chapter moves to
        `validating`: the server checks and decodes each image, joins and cuts
        the strip into parts, and computes the exact quote. Validation is free.

        When it finishes the chapter is `ready` (and `chapter.ready` fires), or
        `failed` with `error.code = invalid_image` (nothing is charged). With
        `auto_start` and a quote within `max_credits`, the chapter goes
        straight to `queued`.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      responses:
        '202':
          description: Accepted; the chapter is `validating`.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '422': {$ref: '#/components/responses/Unprocessable'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/start:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Chapters]
      operationId: startChapter
      summary: Start a chapter
      description: |
        Holds the quoted credits and puts the chapter in the GPU queue (first
        come, first served). You are never charged more than the quote, and
        parts that fail are refunded.

        `Idempotency-Key` is **required**. Starting a chapter that is already
        `queued` or `processing` returns `200` with the chapter. After a `402`,
        buy credits and retry with a **new** idempotency key.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKeyRequired'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                max_credits:
                  type: integer
                  minimum: 1
                  description: Refuse to start (`409 quote_exceeds_max`) if the quote is higher.
      responses:
        '200':
          description: The chapter, now `queued` (or already `queued` / `processing`).
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '402': {$ref: '#/components/responses/PaymentRequired'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}
        '503': {$ref: '#/components/responses/CapacityUnavailable'}

  /chapters/{chapter_id}/cancel:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Chapters]
      operationId: cancelChapter
      summary: Cancel a chapter
      description: |
        Stops the chapter. A `queued` chapter gets its whole hold back. A
        `processing` chapter is charged for finished steps and the rest is
        refunded. The record and any finished outputs are kept; use `DELETE`
        to erase them.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      responses:
        '200':
          description: The chapter, now `canceled`.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/result:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    get:
      tags: [Chapters]
      operationId: getChapterResult
      summary: Get the result
      description: |
        Returns signed download URLs, valid for 1 hour. Call again for fresh
        URLs. Downloads are not metered.

        Returns `409 chapter_not_ready` before the first success, and
        `409 chapter_locked` while the chapter is in `needs_review` with open
        zones.
      responses:
        '200':
          description: The result.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/ChapterResult'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '410': {$ref: '#/components/responses/Gone'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/regions:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    get:
      tags: [Regions and review]
      operationId: listRegions
      summary: List regions
      description: |
        Every text region of the chapter: Korean source, English text, type,
        box and flags. Not paginated. The response carries an `ETag`, which
        you need to edit regions.
      responses:
        '200':
          description: The regions.
          headers:
            ETag: {$ref: '#/components/headers/ETag'}
          content:
            application/json:
              schema:
                type: object
                properties:
                  object: {const: list}
                  data:
                    type: array
                    items: {$ref: '#/components/schemas/Region'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '410': {$ref: '#/components/responses/Gone'}
        '429': {$ref: '#/components/responses/RateLimited'}
    patch:
      tags: [Regions and review]
      operationId: editRegions
      summary: Edit regions
      description: |
        Batch edit `ko`, `en`, `type` and `include`. Requires `If-Match` with
        the ETag from `GET /regions`; a stale ETag returns
        `412 precondition_failed`, so two editors never overwrite each other.

        Returns the list of parts that need a re-render. Nothing is rendered
        until you call `POST /rerender`.
      parameters:
        - name: If-Match
          in: header
          required: true
          schema: {type: string}
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [edits]
              properties:
                edits:
                  type: array
                  minItems: 1
                  items: {$ref: '#/components/schemas/RegionEdit'}
            example:
              edits:
                - {id: p03r012, en: "Stay behind me."}
                - {id: p03r013, include: false}
      responses:
        '200':
          description: Saved.
          headers:
            ETag: {$ref: '#/components/headers/ETag'}
          content:
            application/json:
              schema:
                type: object
                properties:
                  dirty_parts:
                    type: array
                    items: {type: integer}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '412': {$ref: '#/components/responses/PreconditionFailed'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/retranslate:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Regions and review]
      operationId: retranslateRegions
      summary: Re-translate regions
      description: |
        Asks the translator again for the given regions (with neighbouring
        lines as context). Runs without the GPU. The changed parts become
        dirty; call `POST /rerender` to letter them.

        Pricing: translate rate (8 credits) per part touched.
      # OPEN: re-translate pricing (spec §24 Q1); recommended 8 cr per part touched.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [region_ids]
              properties:
                region_ids:
                  type: array
                  minItems: 1
                  items: {type: string}
      responses:
        '202':
          description: Accepted; the chapter is `processing`.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '402': {$ref: '#/components/responses/PaymentRequired'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /chapters/{chapter_id}/rerender:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Regions and review]
      operationId: rerenderChapter
      summary: Re-render dirty parts
      description: |
        Re-cleans and re-letters only the parts changed by edits. Joins the
        back of the GPU queue like any other job. The previous result stays
        downloadable until the new render finishes, and `result.version`
        increments.

        Pricing: clean + letter rate (10 credits) per re-rendered part.
      # OPEN: re-render pricing (spec §24 Q1); recommended 10 cr per re-rendered part.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      responses:
        '202':
          description: Accepted; the chapter is `queued` or `processing`.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '402': {$ref: '#/components/responses/PaymentRequired'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}
        '503': {$ref: '#/components/responses/CapacityUnavailable'}

  /chapters/{chapter_id}/review/accept:
    parameters:
      - $ref: '#/components/parameters/ChapterId'
    post:
      tags: [Regions and review]
      operationId: acceptReviewZones
      summary: Accept review zones
      description: |
        Accepts failed-mask zones as they are: the Korean stays in those zones
        and they get no English. Free. When no zone is left open the chapter
        moves from `needs_review` to `succeeded` and `chapter.succeeded` fires.
        Send either `zone_ids` or `all: true`.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                zone_ids:
                  type: array
                  items: {type: string}
                all:
                  type: boolean
      responses:
        '200':
          description: The chapter.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Chapter'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '409': {$ref: '#/components/responses/Conflict'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /series:
    post:
      tags: [Series]
      operationId: createSeries
      summary: Create a series
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/SeriesCreate'}
      responses:
        '201':
          description: The series.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Series'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}
    get:
      tags: [Series]
      operationId: listSeries
      summary: List series
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
      responses:
        '200':
          description: A page of series.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/Series'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /series/{series_id}:
    parameters:
      - $ref: '#/components/parameters/SeriesId'
    get:
      tags: [Series]
      operationId: getSeries
      summary: Retrieve a series
      responses:
        '200':
          description: The series.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Series'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}
    patch:
      tags: [Series]
      operationId: updateSeries
      summary: Update a series
      description: Rename the series or change its notes.
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/SeriesCreate'}
      responses:
        '200':
          description: The series.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Series'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}
    delete:
      tags: [Series]
      operationId: deleteSeries
      summary: Delete a series
      description: |
        Deletes the series **and all of its chapters**. Requires
        `?confirm=true`.
      parameters:
        - name: confirm
          in: query
          required: true
          schema: {type: boolean, const: true}
      responses:
        '200':
          description: Deleted.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Deleted'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /series/{series_id}/glossary:
    parameters:
      - $ref: '#/components/parameters/SeriesId'
    get:
      tags: [Glossary]
      operationId: listGlossary
      summary: List glossary entries
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
        - name: status
          in: query
          description: Use `proposed` to list the translator's proposals waiting for you.
          schema: {type: string, enum: [confirmed, proposed]}
      responses:
        '200':
          description: A page of entries.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/GlossaryEntry'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}
    post:
      tags: [Glossary]
      operationId: addGlossaryEntries
      summary: Add glossary entries
      description: Adds up to 500 entries in one call. Added entries are `confirmed`.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [entries]
              properties:
                entries:
                  type: array
                  minItems: 1
                  maxItems: 500
                  items: {$ref: '#/components/schemas/GlossaryTerm'}
            example:
              entries:
                - {source: 검기, target: Sword Qi, type: technique}
                - {source: 이서준, target: Lee Seojun, type: name, gender: m, since_chapter: 1}
      responses:
        '201':
          description: The created entries.
          content:
            application/json:
              schema:
                type: object
                properties:
                  object: {const: list}
                  data:
                    type: array
                    items: {$ref: '#/components/schemas/GlossaryEntry'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /series/{series_id}/glossary/{entry_id}:
    parameters:
      - $ref: '#/components/parameters/SeriesId'
      - name: entry_id
        in: path
        required: true
        schema: {type: string, examples: [gle_9pQ2rS3tU4vW5xY6zA7bC8]}
    patch:
      tags: [Glossary]
      operationId: updateGlossaryEntry
      summary: Update or confirm an entry
      description: Edit an entry. Set `status` to `confirmed` to accept a proposal.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/GlossaryTermPatch'
                - type: object
                  properties:
                    status: {type: string, enum: [confirmed]}
      responses:
        '200':
          description: The entry.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/GlossaryEntry'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}
    delete:
      tags: [Glossary]
      operationId: deleteGlossaryEntry
      summary: Delete an entry
      description: |
        Deletes the entry. A deleted term stays deleted: the translator will
        not propose it again for this series.
      responses:
        '200':
          description: Deleted.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Deleted'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /webhook_endpoints:
    post:
      tags: [Webhooks]
      operationId: createWebhookEndpoint
      summary: Register a webhook endpoint
      description: |
        Registers an HTTPS URL (port 443 or 8443) to receive events. The
        response contains the signing `secret` (`whsec_…`). **It is shown only
        once.** Live and test endpoints are separate: an endpoint created with
        a test key receives only test events.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [url]
              properties:
                url: {type: string, format: uri}
                events:
                  type: array
                  description: |
                    Event types to send. Defaults to every type except
                    `chapter.step_completed`, which is opt-in.
                  items: {$ref: '#/components/schemas/EventType'}
                description: {type: string, maxLength: 200}
      responses:
        '201':
          description: The endpoint, including its secret.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/WebhookEndpoint'
                  - type: object
                    properties:
                      secret: {type: string, examples: [whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw]}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}
    get:
      tags: [Webhooks]
      operationId: listWebhookEndpoints
      summary: List webhook endpoints
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
      responses:
        '200':
          description: A page of endpoints.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/WebhookEndpoint'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /webhook_endpoints/{endpoint_id}:
    parameters:
      - $ref: '#/components/parameters/EndpointId'
    patch:
      tags: [Webhooks]
      operationId: updateWebhookEndpoint
      summary: Update a webhook endpoint
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                url: {type: string, format: uri}
                events:
                  type: array
                  items: {$ref: '#/components/schemas/EventType'}
                enabled: {type: boolean}
                description: {type: string, maxLength: 200}
      responses:
        '200':
          description: The endpoint.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/WebhookEndpoint'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}
    delete:
      tags: [Webhooks]
      operationId: deleteWebhookEndpoint
      summary: Delete a webhook endpoint
      responses:
        '200':
          description: Deleted.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Deleted'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /webhook_endpoints/{endpoint_id}/rotate_secret:
    parameters:
      - $ref: '#/components/parameters/EndpointId'
    post:
      tags: [Webhooks]
      operationId: rotateWebhookSecret
      summary: Rotate the signing secret
      description: |
        Issues a new secret, shown once. The old secret stays valid for 24
        hours; during that time each delivery carries one signature per active
        secret.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      responses:
        '200':
          description: The endpoint with its new secret.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/WebhookEndpoint'
                  - type: object
                    properties:
                      secret: {type: string}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /webhook_endpoints/{endpoint_id}/test:
    parameters:
      - $ref: '#/components/parameters/EndpointId'
    post:
      tags: [Webhooks]
      operationId: testWebhookEndpoint
      summary: Send a test event
      description: Sends a signed `ping` event to the endpoint.
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      responses:
        '202':
          description: The `ping` event was queued for delivery.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Event'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /events:
    get:
      tags: [Events]
      operationId: listEvents
      summary: List events
      description: |
        Lists recent events, newest first. Use it to catch up after missed
        webhook deliveries.
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
        - name: type
          in: query
          schema: {$ref: '#/components/schemas/EventType'}
        - name: chapter_id
          in: query
          schema: {type: string}
      responses:
        '200':
          description: A page of events.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/Event'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /events/{event_id}:
    parameters:
      - name: event_id
        in: path
        required: true
        schema: {type: string, examples: [evt_5LwQ2rS3tU4vW5xY6zA7bC]}
    get:
      tags: [Events]
      operationId: getEvent
      summary: Retrieve an event
      responses:
        '200':
          description: The event.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Event'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /balance:
    get:
      tags: [Credits]
      operationId: getBalance
      summary: Get the balance
      description: Credits available to spend and credits held by queued or running chapters.
      responses:
        '200':
          description: The balance.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Balance'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /usage:
    get:
      tags: [Credits]
      operationId: getUsage
      summary: Get usage
      description: Credits spent in a time range, grouped by step, API key, chapter or day.
      parameters:
        - name: group_by
          in: query
          schema: {type: string, enum: [step, key, chapter, day], default: day}
        - name: from
          in: query
          schema: {type: string, format: date-time}
        - name: to
          in: query
          schema: {type: string, format: date-time}
        - name: api_key
          in: query
          description: Only usage by this API key id (`key_…`).
          schema: {type: string}
      responses:
        '200':
          description: Usage rows.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Usage'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /ledger:
    get:
      tags: [Credits]
      operationId: listLedger
      summary: List ledger entries
      description: Every credit movement on the account, newest first.
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Cursor'
        - name: type
          in: query
          schema: {$ref: '#/components/schemas/LedgerType'}
        - name: chapter_id
          in: query
          schema: {type: string}
      responses:
        '200':
          description: A page of ledger entries.
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ListEnvelope'
                  - type: object
                    properties:
                      data:
                        type: array
                        items: {$ref: '#/components/schemas/LedgerEntry'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '403': {$ref: '#/components/responses/Forbidden'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /estimate:
    post:
      tags: [Credits]
      operationId: createEstimate
      summary: Estimate a price
      description: |
        Estimates parts and credits from image sizes alone, before uploading.
        Nothing is held. This is an estimate: the binding price is the quote
        on a `ready` chapter. Any valid key can call it.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [images]
              properties:
                images:
                  type: array
                  minItems: 1
                  maxItems: 300
                  items:
                    type: object
                    required: [width, height]
                    properties:
                      width: {type: integer, minimum: 64}
                      height: {type: integer, minimum: 64}
                layout: {type: string, enum: [strip, pages], default: strip}
                translate_sfx: {type: boolean, default: false}
            example:
              images: [{width: 690, height: 12000}, {width: 690, height: 11800}]
              layout: strip
      responses:
        '200':
          description: The estimate.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Estimate'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '429': {$ref: '#/components/responses/RateLimited'}

  /status:
    get:
      tags: [Status]
      operationId: getStatus
      summary: Get service status
      description: GPU state and the estimated queue wait. No authentication needed.
      security: []
      responses:
        '200':
          description: The status.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/ServiceStatus'}

  /test_helpers/balance:
    post:
      tags: [Test helpers]
      operationId: setTestBalance
      summary: Set the test balance
      description: |
        Test mode only. Sets the test credit balance, for example to `0` to
        exercise `402 insufficient_credits`. Live keys get `404`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [credits]
              properties:
                credits: {type: integer, minimum: 0}
      responses:
        '200':
          description: The test balance.
          content:
            application/json:
              schema: {$ref: '#/components/schemas/Balance'}
        '400': {$ref: '#/components/responses/BadRequest'}
        '401': {$ref: '#/components/responses/Unauthorized'}
        '404': {$ref: '#/components/responses/NotFound'}

webhooks:
  chapterEvent:
    post:
      tags: [Webhooks]
      summary: Event delivery
      description: |
        Sent to each registered endpoint subscribed to the event type. Signed
        with the Standard Webhooks headers. Respond with any `2xx` within 10
        seconds. Anything else is retried with exponential backoff for up to 3
        days. Deliveries are at least once and unordered: deduplicate on
        `webhook-id`.
      parameters:
        - name: webhook-id
          in: header
          required: true
          schema: {type: string, examples: [evt_5LwQ2rS3tU4vW5xY6zA7bC]}
        - name: webhook-timestamp
          in: header
          required: true
          description: Unix seconds.
          schema: {type: string}
        - name: webhook-signature
          in: header
          required: true
          description: Space-separated list of `v1,<base64 HMAC-SHA256>` signatures.
          schema: {type: string}
      requestBody:
        content:
          application/json:
            schema: {$ref: '#/components/schemas/Event'}
      responses:
        '200':
          description: Any 2xx marks the delivery as done.

components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      description: |
        An API key: `rl_live_…` for real work, `rl_test_…` for test mode.
        Create keys on the site. Never put a key in browser code.

  headers:
    Request-Id:
      description: Unique id of this request. Quote it when you email support.
      schema: {type: string, examples: [req_8f2a1b3c4d5e6f7a8b9c0d]}
    ETag:
      description: Version tag. Send back in `If-None-Match` (polling) or `If-Match` (editing).
      schema: {type: string}
    Idempotent-Replayed:
      description: '`true` when this response is a replay of a saved response.'
      schema: {type: string, enum: ['true']}
    Retry-After:
      description: Seconds to wait before retrying.
      schema: {type: integer}

  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: A unique key for this request (a UUIDv4 is recommended). See Idempotency.
      schema: {type: string, maxLength: 255}
    IdempotencyKeyRequired:
      name: Idempotency-Key
      in: header
      required: true
      description: A unique key for this request (a UUIDv4 is recommended). Required here.
      schema: {type: string, maxLength: 255}
    ChapterId:
      name: chapter_id
      in: path
      required: true
      schema: {type: string, examples: [ch_7Kq2mXaB3cD4eF5gH6iJ7k]}
    SeriesId:
      name: series_id
      in: path
      required: true
      schema: {type: string, examples: [ser_3hT9aB7cD1eF2gH3iJ4kL5]}
    EndpointId:
      name: endpoint_id
      in: path
      required: true
      schema: {type: string, examples: [we_4mN8pQ2rS3tU4vW5xY6zA7]}
    Limit:
      name: limit
      in: query
      schema: {type: integer, minimum: 1, maximum: 100, default: 20}
    Cursor:
      name: cursor
      in: query
      description: The `next_cursor` from the previous page.
      schema: {type: string}

  responses:
    BadRequest:
      description: '`parameter_invalid`, `parameter_missing` or `json_invalid`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    Unauthorized:
      description: '`api_key_missing`, `api_key_invalid` or `api_key_revoked`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    Forbidden:
      description: '`scope_missing`, `test_key_live_object`, `live_key_test_object` or `account_suspended`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    NotFound:
      description: '`not_found`. Also returned for ids that belong to another account.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    Conflict:
      description: |
        `invalid_state`, `chapter_exists`, `idempotency_key_reused`,
        `idempotency_in_progress`, `chapter_not_ready`, `chapter_locked` or
        `quote_exceeds_max`.
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    Gone:
      description: '`gone`. The chapter was deleted.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    PreconditionFailed:
      description: '`precondition_failed`. The `If-Match` ETag is stale; fetch the regions again.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    TooLarge:
      description: '`too_many_files`, `chapter_too_large` or `too_many_parts`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    UnsupportedMediaType:
      description: '`unsupported_media_type`. Only image/png, image/jpeg and image/webp are accepted.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    Unprocessable:
      description: '`upload_incomplete`, `invalid_image`, `image_too_large`, `unsupported_language` or `nothing_to_translate`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    PaymentRequired:
      description: '`insufficient_credits` or `spend_limit_reached`.'
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
          example:
            error:
              type: payment_required
              code: insufficient_credits
              message: "This chapter needs 3,300 credits; 1,140 are available. Buy credits at https://relettr.com/billing."
              required: 3300
              available: 1140
              doc_url: "https://docs.relettr.com/errors#insufficient_credits"
              request_id: req_c02a1b3c4d5e6f7a8b9c0d
    RateLimited:
      description: '`rate_limited` or `concurrency_limit`.'
      headers:
        Retry-After: {$ref: '#/components/headers/Retry-After'}
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}
    CapacityUnavailable:
      description: '`capacity_unavailable`. The GPU queue is too long right now. Nothing was held.'
      headers:
        Retry-After: {$ref: '#/components/headers/Retry-After'}
      content:
        application/json:
          schema: {$ref: '#/components/schemas/Error'}

  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [type, code, message, request_id]
          additionalProperties: true
          description: |
            Some codes add fields: `required` and `available` (402),
            `scope`, `limit` and `used_this_month` (spend limits), `limit` and
            `active` (concurrency), `estimated_wait_s` (503), `status` and
            `allowed_from` (invalid_state), `missing_indexes`
            (upload_incomplete).
          properties:
            type:
              type: string
              enum: [invalid_request, authentication, permission, not_found, conflict,
                     payment_required, rate_limited, capacity, api_error]
            code: {type: string, examples: [parameter_invalid]}
            message: {type: string}
            param: {type: string, examples: ['files[3].content_type']}
            doc_url: {type: string, format: uri}
            request_id: {type: string}

    ChapterStatus:
      type: string
      description: May gain values in v1. Treat an unknown value as non-terminal and keep polling.
      enum: [awaiting_upload, validating, ready, queued, processing, needs_review,
             succeeded, failed, canceled, expired]

    FileDecl:
      type: object
      required: [index, content_type, size_bytes]
      properties:
        index:
          type: integer
          minimum: 1
          maximum: 300
          description: Reading order, 1..N, top to bottom.
        filename: {type: string, maxLength: 200, description: Stored as text only.}
        content_type: {type: string, enum: [image/png, image/jpeg, image/webp]}
        size_bytes: {type: integer, minimum: 1, maximum: 52428800}
        width: {type: integer, minimum: 64}
        height: {type: integer, minimum: 64}

    Upload:
      type: object
      properties:
        index: {type: integer}
        method: {const: PUT}
        url: {type: string, format: uri}
        headers:
          type: object
          description: Send exactly these headers with the PUT.
          additionalProperties: {type: string}
        expires_at: {type: string, format: date-time}

    OutputOptions:
      type: object
      properties:
        # OPEN: default output layout "original" (spec §24 Q5); recommended original.
        layout:
          type: string
          enum: [original, parts]
          default: original
          description: |
            `original` cuts the output back to your uploaded image boundaries
            (same count, same order). `parts` returns the processing parts
            (at most 1,600 px tall).
        strip:
          type: boolean
          default: false
          description: Also return the stitched chapter, cut at about 30,000 px.
        format: {type: string, enum: [png, webp, jpeg], default: png}
        quality: {type: integer, minimum: 1, maximum: 100, default: 90}

    ChapterOptions:
      type: object
      properties:
        layout:
          type: string
          enum: [strip, pages]
          default: strip
          description: |
            `strip` joins the images into one vertical strip (vertical-scroll manhwa).
            `pages` treats each image as its own page.
        translate_sfx:
          type: boolean
          default: false
          x-beta: true
          description: '**Beta.** Translate drawn sound effects. Adds 3 credits per part.'
        output: {$ref: '#/components/schemas/OutputOptions'}
        # OPEN: mask lock for API users (spec §24 Q2); recommended default hold, opt-in accept.
        on_mask_failure:
          type: string
          enum: [hold, accept]
          default: hold
          description: |
            `hold` stops in `needs_review` with exports locked when a zone
            cannot be cleaned safely. `accept` accepts those zones
            automatically: their Korean stays and they are listed in
            `warnings`.
        auto_start:
          type: boolean
          default: false
          description: Start as soon as the quote is known. Requires `max_credits`.
        max_credits:
          type: [integer, 'null']
          minimum: 1
          description: Do not start if the quote is higher than this.

    ChapterCreate:
      allOf:
        - $ref: '#/components/schemas/ChapterOptions'
        - type: object
          required: [source_language, files]
          properties:
            source_language:
              type: string
              enum: [ko]
              description: Korean only. Any other value returns `422 unsupported_language`.
            target_language: {type: string, enum: [en], default: en}
            series_id: {type: string, description: Use this series' glossary and context.}
            number:
              type: integer
              minimum: 1
              maximum: 99999
              description: Chapter number, unique within the series (`409 chapter_exists`).
            glossary:
              type: array
              maxItems: 300
              description: Inline glossary for a chapter without a series. Ignored when `series_id` is set.
              items: {$ref: '#/components/schemas/GlossaryTerm'}
            files:
              type: array
              minItems: 1
              maxItems: 300
              items: {$ref: '#/components/schemas/FileDecl'}
            metadata: {$ref: '#/components/schemas/Metadata'}
            test_scenario:
              type: string
              enum: [succeed, needs_review, fail_read, fail_render_partial, slow_queue, capacity_unavailable]
              description: Test mode only. Live keys get `400`.

    ChapterUpdate:
      allOf:
        - $ref: '#/components/schemas/ChapterOptions'
        - type: object
          properties:
            metadata: {$ref: '#/components/schemas/Metadata'}

    Metadata:
      type: object
      maxProperties: 20
      additionalProperties: {type: string, maxLength: 500}
      description: |
        Your own key/value strings, returned on the object and in events.
        Never shown in our UI and never sent to the translator.

    Quote:
      type: [object, 'null']
      description: Exact price, known from `ready`. You are never charged more than this.
      properties:
        credits: {type: integer, examples: [462]}
        parts: {type: integer, examples: [21]}
        breakdown:
          type: object
          properties:
            ocr: {type: integer}
            translate: {type: integer}
            clean_letter: {type: integer}
            sfx: {type: integer}

    Credits:
      type: object
      properties:
        held: {type: integer}
        charged: {type: integer}
        refunded: {type: integer}

    Progress:
      type: object
      properties:
        step: {type: [string, 'null'], enum: [read, translate, render, stitch, null]}
        percent: {type: integer, minimum: 0, maximum: 100}
        parts_total: {type: [integer, 'null']}
        parts_done: {type: integer}
        steps:
          type: array
          items:
            type: object
            properties:
              name: {type: string, enum: [read, translate, render, stitch]}
              status: {type: string, enum: [pending, running, succeeded, failed, skipped]}

    Queue:
      type: object
      properties:
        position:
          type: [integer, 'null']
          description: Place in the shared first-come-first-served queue while `queued`.
        estimated_start_at: {type: [string, 'null'], format: date-time}
        gpu: {type: string, enum: [online, cooling, offline]}

    Review:
      type: [object, 'null']
      properties:
        open: {type: integer}
        accepted: {type: integer}
        zones:
          type: array
          items:
            type: object
            properties:
              id: {type: string}
              page: {type: integer}
              box: {$ref: '#/components/schemas/Box'}
              reason: {type: string}

    Warning:
      type: object
      properties:
        code:
          type: string
          examples: [missing_glyphs]
          description: |
            For example `missing_glyphs`, `no_fit`, `tiny_lettering`,
            `korean_may_remain`, `mask_failed`, `heal_error`,
            `untranslated_erased`, `part_failed`.
        page: {type: integer}
        region_ids:
          type: array
          items: {type: string}

    ChapterError:
      type: [object, 'null']
      properties:
        code:
          type: string
          enum: [invalid_image, read_failed, translate_failed, render_failed,
                 gpu_unavailable_timeout, internal_error]
        message: {type: string}
        step: {type: [string, 'null']}
        file_index: {type: [integer, 'null']}

    Box:
      type: array
      description: '[x0, y0, x1, y1] in pixels of the chosen output layout.'
      items: {type: integer}
      minItems: 4
      maxItems: 4

    Chapter:
      type: object
      properties:
        id: {type: string, examples: [ch_7Kq2mXaB3cD4eF5gH6iJ7k]}
        object: {const: chapter}
        livemode: {type: boolean}
        status: {$ref: '#/components/schemas/ChapterStatus'}
        series_id: {type: [string, 'null']}
        number: {type: [integer, 'null']}
        source_language: {type: string, enum: [ko]}
        target_language: {type: string, enum: [en]}
        options: {$ref: '#/components/schemas/ChapterOptions'}
        uploads:
          type: array
          description: Only present on create and while `awaiting_upload`.
          items: {$ref: '#/components/schemas/Upload'}
        quote: {$ref: '#/components/schemas/Quote'}
        credits: {$ref: '#/components/schemas/Credits'}
        progress: {$ref: '#/components/schemas/Progress'}
        queue: {$ref: '#/components/schemas/Queue'}
        review: {$ref: '#/components/schemas/Review'}
        warnings:
          type: array
          items: {$ref: '#/components/schemas/Warning'}
        error: {$ref: '#/components/schemas/ChapterError'}
        result_version: {type: [integer, 'null']}
        metadata: {$ref: '#/components/schemas/Metadata'}
        created_at: {type: string, format: date-time}
        started_at: {type: [string, 'null'], format: date-time}
        completed_at: {type: [string, 'null'], format: date-time}

    ChapterResult:
      type: object
      properties:
        object: {const: chapter_result}
        chapter_id: {type: string}
        version: {type: integer}
        expires_at: {type: string, format: date-time, description: When these URLs stop working (1 hour).}
        layout: {type: string, enum: [original, parts]}
        format: {type: string, enum: [png, webp, jpeg]}
        pages:
          type: array
          items:
            type: object
            properties:
              index: {type: integer}
              width: {type: integer}
              height: {type: integer}
              bytes: {type: integer}
              sha256: {type: string}
              url: {type: string, format: uri}
        strip:
          type: array
          description: Only when `output.strip` is true.
          items:
            type: object
            properties:
              index: {type: integer}
              height: {type: integer}
              url: {type: string, format: uri}
        zip:
          type: object
          properties:
            bytes: {type: integer}
            url: {type: string, format: uri}
        regions_json:
          type: object
          properties:
            url: {type: string, format: uri}
        warnings:
          type: array
          items: {$ref: '#/components/schemas/Warning'}
        glossary_proposals:
          type: array
          description: Terms the translator suggests adding to the series glossary.
          items: {$ref: '#/components/schemas/GlossaryTerm'}

    Region:
      type: object
      properties:
        id: {type: string, examples: [p03r012]}
        page: {type: integer}
        box: {$ref: '#/components/schemas/Box'}
        ko: {type: string}
        en: {type: string}
        type: {type: string, examples: [speech], description: 'Bubble type, e.g. speech, thought, shout, narration, sfx.'}
        include: {type: boolean}
        render: {type: boolean}
        in_bubble: {type: boolean}
        flags:
          type: array
          items: {type: string}

    RegionEdit:
      type: object
      required: [id]
      properties:
        id: {type: string}
        ko: {type: string}
        en: {type: string}
        type: {type: string}
        include: {type: boolean}

    SeriesCreate:
      type: object
      properties:
        title: {type: string, maxLength: 200}
        title_ko: {type: string, maxLength: 200}
        notes: {type: string, maxLength: 2000, description: Style and context notes for the translator.}
        metadata: {$ref: '#/components/schemas/Metadata'}

    Series:
      allOf:
        - $ref: '#/components/schemas/SeriesCreate'
        - type: object
          properties:
            id: {type: string, examples: [ser_3hT9aB7cD1eF2gH3iJ4kL5]}
            object: {const: series}
            livemode: {type: boolean}
            chapter_count: {type: integer}
            glossary_count: {type: integer}
            created_at: {type: string, format: date-time}

    GlossaryTerm:
      type: object
      required: [source, target]
      properties:
        source: {type: string, maxLength: 60, description: Korean term.}
        target: {type: string, maxLength: 60, description: English rendering.}
        type: {type: string, enum: [name, place, title, technique, term, address, speech, sfx]}
        gender: {type: string, enum: [f, m]}
        since_chapter: {type: integer, minimum: 1, description: First chapter where this entry applies.}
        note: {type: string, maxLength: 200}

    GlossaryTermPatch:
      type: object
      properties:
        source: {type: string, maxLength: 60}
        target: {type: string, maxLength: 60}
        type: {type: string, enum: [name, place, title, technique, term, address, speech, sfx]}
        gender: {type: string, enum: [f, m]}
        since_chapter: {type: integer, minimum: 1}
        note: {type: string, maxLength: 200}

    GlossaryEntry:
      allOf:
        - $ref: '#/components/schemas/GlossaryTerm'
        - type: object
          properties:
            id: {type: string, examples: [gle_9pQ2rS3tU4vW5xY6zA7bC8]}
            object: {const: glossary_entry}
            status: {type: string, enum: [confirmed, proposed]}
            proposed_by_chapter: {type: [string, 'null']}
            created_at: {type: string, format: date-time}

    EventType:
      type: string
      enum:
        - chapter.ready
        - chapter.queued
        - chapter.processing
        - chapter.step_completed
        - chapter.needs_review
        - chapter.succeeded
        - chapter.failed
        - chapter.canceled
        - chapter.expired
        - chapter.deleted
        - balance.low
        - api_key.spend_limit_reached
        - ping

    WebhookEndpoint:
      type: object
      properties:
        id: {type: string, examples: [we_4mN8pQ2rS3tU4vW5xY6zA7]}
        object: {const: webhook_endpoint}
        livemode: {type: boolean}
        url: {type: string, format: uri}
        events:
          type: array
          items: {$ref: '#/components/schemas/EventType'}
        enabled: {type: boolean}
        description: {type: [string, 'null']}
        api_version: {const: v1}
        created_at: {type: string, format: date-time}

    Event:
      type: object
      description: |
        Thin payload: `data.object` is a snapshot of status, progress, quote,
        credits, warnings, review and metadata. It never contains region text
        or image URLs; fetch `/result` and `/regions` for those.
      properties:
        id: {type: string, examples: [evt_5LwQ2rS3tU4vW5xY6zA7bC]}
        object: {const: event}
        type: {$ref: '#/components/schemas/EventType'}
        created: {type: integer, description: Unix seconds.}
        livemode: {type: boolean}
        api_version: {const: v1}
        data:
          type: object
          properties:
            object: {type: object}

    Balance:
      type: object
      properties:
        object: {const: balance}
        livemode: {type: boolean}
        available: {type: integer, description: Credits you can spend now.}
        held: {type: integer, description: Credits held by queued or running chapters.}
        welcome_remaining: {type: integer, description: Unspent welcome credits, included in `available`.}
        usd_equivalent: {type: string, examples: ['11.40'], description: '`available` / 100, for display only.'}

    Usage:
      type: object
      properties:
        object: {const: usage}
        group_by: {type: string, enum: [step, key, chapter, day]}
        from: {type: string, format: date-time}
        to: {type: string, format: date-time}
        total_credits: {type: integer}
        data:
          type: array
          items:
            type: object
            properties:
              group: {type: string}
              credits: {type: integer}
              parts: {type: integer}

    LedgerType:
      type: string
      enum: [purchase, hold, settle, refund, adjust, grant]

    LedgerEntry:
      type: object
      properties:
        id: {type: string, examples: [led_2aB3cD4eF5gH6iJ7kL8mN9]}
        object: {const: ledger_entry}
        type: {$ref: '#/components/schemas/LedgerType'}
        credits: {type: integer, description: Signed amount.}
        chapter_id: {type: [string, 'null']}
        api_key_id: {type: [string, 'null']}
        created_at: {type: string, format: date-time}

    Estimate:
      type: object
      properties:
        object: {const: estimate}
        parts_estimate: {type: integer}
        credits_estimate: {type: integer}
        breakdown:
          type: object
          properties:
            ocr: {type: integer}
            translate: {type: integer}
            clean_letter: {type: integer}
            sfx: {type: integer}

    ServiceStatus:
      type: object
      properties:
        gpu: {type: string, enum: [online, cooling, offline]}
        estimated_wait_s: {type: [integer, 'null']}
        resume_at: {type: [string, 'null'], format: date-time}

    ListEnvelope:
      type: object
      properties:
        object: {const: list}
        has_more: {type: boolean}
        next_cursor: {type: [string, 'null']}

    Deleted:
      type: object
      properties:
        id: {type: string}
        object: {type: string}
        deleted: {const: true}
