openapi: 3.1.0
info:
  title: AppletHub Template Service
  version: "1.0"
  summary: Fill a Word, Markdown or text template from key-value pairs. Nothing is stored.
  description: |
    Two calls:

    - `POST /v1/inspect` reads a template and returns the keys to fill, a JSON Schema for the values (usable as
      structured output by an AI model), and any syntax errors with their locations.
    - `POST /v1/generate` fills the template and returns the document itself: a Word file, or Markdown or plain
      text. A document always comes back. Problems are shown in place, never as template markup:
      `[Key Name]` where a value is missing or null (the key with spaces: `Client_Name` → `[Client Name]`),
      `[ValueERROR!]` where a calculation cannot run, and `[SyntaxERROR!]` where a marker cannot be read
      (the two ERROR! labels in bold in Word and Markdown). A summary is in the `X-Template-Report` header.

    Options: `include_text` on inspect also returns the template's text; `output=report` on generate returns the
    full report as JSON instead of the document (no document is built; it is still one generate call).

    The template syntax (markers, lists, conditions, deletion ranges, values and their conversions) is described
    in the conversion guide, served at /guide (Markdown); this file is served at /openapi.yaml.

    Templates and values are processed in memory and never stored or logged.

    Limits: files up to 4 MB in and out; up to 1,000 items per list and 20,000 characters per value; 15 seconds per
    call. Without a key (visitors): uploaded files 5 calls a minute and 30 a day; templates sent as JSON text
    (up to 200 KB) 30 calls a minute and 600 a day. API keys are not open yet; a call with a key is answered 403.
    Every caller is also limited to 30 API calls a minute per address at the edge; that 429 comes from the
    hosting platform in its own JSON shape ({"error": {"code": "429", ...}}).
servers:
  - url: https://templates.applethub.ai
  - url: https://applethub-templates.vercel.app
security:
  - {}
  - bearer: []
paths:
  /v1/inspect:
    post:
      operationId: inspect
      summary: Read a template; list the keys to fill and any syntax errors.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              $ref: "#/components/schemas/InspectUpload"
          application/json:
            schema:
              $ref: "#/components/schemas/InspectText"
      responses:
        "200":
          description: The keys, the schema and the problems found. Returned for every template that could be read,
            with or without syntax errors (`ok` is false when there are errors).
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/InspectResult"
        "400": {$ref: "#/components/responses/BadRequest"}
        "401": {$ref: "#/components/responses/Unauthorized"}
        "403": {$ref: "#/components/responses/KeysNotOpen"}
        "413": {$ref: "#/components/responses/TooLarge"}
        "415": {$ref: "#/components/responses/WrongContentType"}
        "429": {$ref: "#/components/responses/RateLimited"}
        "500": {$ref: "#/components/responses/ServerError"}
  /v1/generate:
    post:
      operationId: generate
      summary: Fill a template from key-value pairs and return the document.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              $ref: "#/components/schemas/GenerateUpload"
          application/json:
            schema:
              $ref: "#/components/schemas/GenerateText"
      responses:
        "200":
          description: The filled document. Problems are shown in it ([Key Name], [ValueERROR!], [SyntaxERROR!]).
            With `output=report`, the full Report as JSON instead (never shortened), with `complete` and `fields`.
          headers:
            X-Template-Report:
              description: Base64url-encoded JSON, at most 8 KB. Either the full Report, or, when that would be
                longer, a ReportSummary with `truncated` true. `ok` and `complete` are always present.
              schema:
                type: string
            Content-Disposition:
              description: 'attachment; filename="<template name>-filled.<docx|md|txt>"'
              schema:
                type: string
          content:
            application/vnd.openxmlformats-officedocument.wordprocessingml.document:
              schema:
                type: string
                format: binary
            text/markdown:
              schema:
                type: string
            text/plain:
              schema:
                type: string
            application/json:
              schema:
                $ref: "#/components/schemas/Report"
        "400": {$ref: "#/components/responses/BadRequest"}
        "401": {$ref: "#/components/responses/Unauthorized"}
        "403": {$ref: "#/components/responses/KeysNotOpen"}
        "413": {$ref: "#/components/responses/TooLarge"}
        "415": {$ref: "#/components/responses/WrongContentType"}
        "422": {$ref: "#/components/responses/Refused"}
        "429": {$ref: "#/components/responses/RateLimited"}
        "500": {$ref: "#/components/responses/ServerError"}
  /health:
    get:
      operationId: health
      summary: Service status and the deployed version.
      security: []
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  status: {type: string, const: ok}
                  version: {type: string}
components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      description: An AppletHub account's API key (not open yet). Without a key, the caller is a visitor.
  schemas:
    Format:
      type: string
      enum: [docx, md, text]
      description: The template's format. Optional for uploads, where it is taken from the file name
        (.docx, .md or .markdown, .txt).
    InspectUpload:
      type: object
      required: [template]
      properties:
        template: {type: string, format: binary, description: The template file (up to 4 MB).}
        format: {$ref: "#/components/schemas/Format"}
        include_text:
          type: string
          enum: ["true", "false"]
          description: true also returns the template's text (see InspectResult `text`). Off by default.
    InspectText:
      type: object
      required: [format, template]
      description: Markdown or plain-text templates only; Word templates must be uploaded.
      properties:
        format: {type: string, enum: [md, text]}
        template: {type: string, description: The template text (up to 200 KB without a key).}
        include_text: {type: boolean, description: true also returns the template's text. Off by default.}
    GenerateUpload:
      type: object
      required: [template]
      properties:
        template: {type: string, format: binary, description: The template file (up to 4 MB).}
        values:
          type: string
          description: A JSON object of key-value pairs, sent as a form field or as a file part (up to 2 MB). A key
            not sent, or null, shows [Key Name]; "" is a blank value.
        format: {$ref: "#/components/schemas/Format"}
        strict:
          type: string
          enum: ["true", "false"]
          description: true switches value conversions off; anything not exactly the right kind is shown as given.
        output: {$ref: "#/components/schemas/Output"}
    Output:
      type: string
      enum: [document, report]
      default: document
      description: document (the default) returns the filled document. report returns the full Report as JSON
        instead, never shortened; no document is built.
    GenerateText:
      type: object
      required: [format, template]
      description: Markdown or plain-text templates only; Word templates must be uploaded.
      properties:
        format: {type: string, enum: [md, text]}
        template: {type: string, description: The template text (up to 200 KB without a key).}
        values: {$ref: "#/components/schemas/Values"}
        strict: {type: boolean}
        output: {$ref: "#/components/schemas/Output"}
    Values:
      type: object
      description: Key-value pairs. Each value is text, a number, true/false, null (not known), a date as
        YYYY-MM-DD, or a list of these. Without strict, a value is converted only when it has one possible reading
        ("$1,000" → 1000, "60%" → 0.6, "20 Nov 2026" → 2026-11-20, "yes" → true), and every conversion is reported.
      additionalProperties:
        oneOf:
          - {type: string}
          - {type: number}
          - {type: boolean}
          - {type: "null"}
          - type: array
            maxItems: 1000
            items:
              oneOf: [{type: string}, {type: number}, {type: boolean}, {type: "null"}]
    Key:
      type: object
      required: [key, kind, list]
      properties:
        key: {type: string, description: The key's name, as written in the template (case-sensitive).}
        kind: {type: string, enum: [text, number, date, boolean]}
        list: {type: boolean, description: True when the template expects a list.}
        single_ok:
          type: boolean
          description: Present (true) for a value used in a repeated row, column or paragraph without its own
            marker; it may be a list (one per copy) or a single value used in every copy.
        percent: {type: boolean, description: Present (true) when the number is shown as a percentage (0.6 = 60%).}
        description: {type: string, description: From the template's TEMPLATE comment, when it has one.}
    Issue:
      type: object
      required: [code, message]
      description: One problem or note. It names the key, label or location; it never quotes the template's text.
      properties:
        code: {type: string, examples: [syntax, range_unclosed, enddel_mismatch, merged_cells, bad_formula_value,
                                        missing_key, null_value, bad_value, condition_unknown, unequal_lists]}
        message: {type: string}
        key: {type: string}
        label: {type: string}
        location: {type: string, examples: ["paragraph 12 of the main text", "table 2, row 3, column 1 (the main text)"]}
        expected: {type: string}
        received: {description: The value that was sent, shortened.}
    Conversion:
      type: object
      properties:
        key: {type: string}
        received: {}
        result: {}
        note: {type: string}
        index: {type: integer, description: The list item, counted from 0.}
    Report:
      type: object
      required: [ok, errors, warnings, conversions]
      properties:
        ok:
          type: boolean
          description: False when the document holds a [SyntaxERROR!] or [ValueERROR!] (or, for inspect, when the
            template has a syntax error).
        complete:
          type: boolean
          description: Generate only. True when nothing is left to fill or decide - no [Key Name] placeholder, no
            clause kept because its condition was unknown, and no ERROR!.
        errors: {type: array, items: {$ref: "#/components/schemas/Issue"}}
        warnings: {type: array, items: {$ref: "#/components/schemas/Issue"}}
        conversions: {type: array, items: {$ref: "#/components/schemas/Conversion"}}
        fields:
          type: object
          description: Generate only. One status per key the template uses, in the template's order. filled - the
            value was used as sent; converted - it was read into the kind the template needs (see conversions);
            unfilled - not sent or null, shown as [Key Name]; as_given - it could not be read as the kind the
            template needs and is shown as sent (see warnings). For a list, the first of as_given, unfilled,
            converted, filled that applies to any item wins. A blank value ("") counts as filled.
          additionalProperties:
            $ref: "#/components/schemas/FieldStatus"
    FieldStatus:
      type: string
      enum: [filled, converted, unfilled, as_given]
    ReportSummary:
      type: object
      required: [ok, truncated, errors, warnings, conversions]
      properties:
        ok: {type: boolean}
        complete: {type: boolean}
        truncated: {type: boolean, const: true}
        errors: {type: integer}
        warnings: {type: integer}
        conversions: {type: integer}
        fields:
          type: object
          description: The field statuses, when they fit in the header.
          additionalProperties:
            $ref: "#/components/schemas/FieldStatus"
    InspectResult:
      allOf:
        - $ref: "#/components/schemas/Report"
        - type: object
          required: [keys, schema, metadata]
          properties:
            keys: {type: array, items: {$ref: "#/components/schemas/Key"}}
            schema:
              type: [object, "null"]
              description: A JSON Schema for the values, using only features shared by Claude, OpenAI and Gemini
                structured outputs (null through anyOf; format date).
            metadata: {type: [string, "null"], description: The template's TEMPLATE comment, as written.}
            declared_not_used: {type: array, items: {type: string}}
            used_not_declared: {type: array, items: {type: string}}
            text:
              type: string
              description: With include_text. The template's text as sent, markers and comments included. Word -
                paragraphs in order, tables as pipe tables (a cell's paragraphs joined by " <br> ", | as \|), then
                each header and footer under a line such as "--- Header 1 ---". Markdown and text - the text itself.
                At most 200 KB.
            text_truncated: {type: boolean, description: With include_text. True when the text was cut at 200 KB.}
    Problem:
      type: object
      properties:
        ok: {type: boolean, const: false}
        errors: {type: array, items: {$ref: "#/components/schemas/Issue"}}
        warnings: {type: array, items: {}}
        conversions: {type: array, items: {}}
  responses:
    BadRequest:
      description: The request could not be read (no template, unknown format, values not a JSON object).
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    Unauthorized:
      description: The key was not recognised or has been switched off.
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    KeysNotOpen:
      description: API keys are not open yet; call without a key.
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    TooLarge:
      description: The request or template is over the size limit.
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    WrongContentType:
      description: Send multipart/form-data, or JSON for Markdown and text.
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    Refused:
      description: |
        No document could be made. Only these stop a document:
        the size limits (more than 1,000 list items, a value over 20,000 characters, an output over 4 MB, a call over
        15 seconds); a file that is not a Word document, is encrypted, holds macros, is a Word template file (.dotx),
        or holds DOCTYPE/ENTITY declarations; a file with review comments, tracked changes or hidden text (they would
        pass the author's drafting on); embedded objects; and markup in places the service cannot fill (footnotes,
        endnotes, text boxes, content controls, hyperlinks, equations, charts, SmartArt, or next to a Word field).
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    RateLimited:
      description: Over the limit; try again after the number of seconds in Retry-After.
      headers:
        Retry-After: {schema: {type: integer}}
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
    ServerError:
      description: Something went wrong inside the service.
      content: {application/json: {schema: {$ref: "#/components/schemas/Problem"}}}
