{
  "openapi": "3.0.3",
  "info": {
    "title": "Clavis Sinica API",
    "version": "1.0.0",
    "description": "Structured, sourced decomposition of Chinese text — idioms, sentences, passages, and classical Chinese — plus English morpheme decomposition and script conversion. Every lookup field traces to a specific table and column; generative fields (LLM-produced) are explicitly labeled as such. Full docs: https://clavissinica.org/docs",
    "contact": {
      "url": "https://clavissinica.org"
    }
  },
  "servers": [
    {
      "url": "https://clavissinica.org"
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key prefixed ck-live_. Obtain one via POST /v1/keys or https://clavissinica.org/get-key. Distinct from chinesekey.org's pk_live_ keys — the two are not interchangeable."
      }
    },
    "schemas": {
      "RunRequest": {
        "type": "object",
        "required": [
          "service_code"
        ],
        "properties": {
          "service_code": {
            "type": "string",
            "enum": [
              "decompose.english_morph",
              "decompose.idiom",
              "decompose.sentence",
              "decompose.passage",
              "decompose.wenyan",
              "decompose.wenyan_lesson",
              "decompose.lesson",
              "ocr.image",
              "convert.script"
            ],
            "description": "\n\nAll 9 service codes (same list as GET /v1/chinese/services):\n  - decompose.english_morph — 8 ckTok, lookup, sync\n  - decompose.idiom — 10 ckTok, lookup, sync\n  - decompose.sentence — 15 ckTok, generative, sync\n  - decompose.passage — 20 ckTok, lookup, sync\n  - decompose.wenyan — 12 ckTok, lookup, sync\n  - decompose.wenyan_lesson — 100 ckTok, generative, submit_poll\n  - decompose.lesson — 100 ckTok, generative, submit_poll\n  - ocr.image — 6 ckTok, generative, sync\n  - convert.script — 2 ckTok, lookup, sync"
          },
          "input": {
            "type": "string",
            "description": "Simplified form — single primary field. Not supported for decompose.passage or convert.script (use payload instead)."
          },
          "payload": {
            "type": "object",
            "description": "Structured form — required for multi-field service_codes. See https://clavissinica.org/docs for the exact shape per service_code."
          },
          "idempotency_key": {
            "type": "string",
            "description": "Optional. Same key replays the first result without re-charging."
          }
        }
      },
      "RunResponse": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "service_code": {
            "type": "string"
          },
          "data": {
            "type": "object",
            "description": "Shape varies by service_code — see https://clavissinica.org/docs for real examples of each."
          },
          "provenance": {
            "type": "object",
            "description": "Explicit breakdown of which fields in `data` come from deterministic dictionary lookup vs. real-time LLM generation. `generated_fields` is an empty array (not an absent field) when the whole response is lookup-based.",
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "lookup",
                  "generative"
                ]
              },
              "generated_fields": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "Top-level keys within `data` that are LLM-generated for this call."
              }
            }
          },
          "cktok_charged": {
            "type": "integer"
          },
          "idempotency_key": {
            "type": "string"
          },
          "replayed": {
            "type": "boolean"
          }
        }
      },
      "AsyncSubmitResponse": {
        "type": "object",
        "description": "Returned instead of RunResponse when service_code is decompose.wenyan_lesson (async — takes 60–90s).",
        "properties": {
          "ok": {
            "type": "boolean"
          },
          "service_code": {
            "type": "string"
          },
          "task_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "pending"
            ]
          },
          "poll_url": {
            "type": "string"
          },
          "cktok_charged": {
            "type": "integer"
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "detail": {
            "type": "object",
            "properties": {
              "error": {
                "type": "string"
              },
              "msg": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  },
  "paths": {
    "/v1/chinese/run": {
      "post": {
        "summary": "Run a decomposition or conversion",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RunRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success (sync) or async task submitted",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {
                      "$ref": "#/components/schemas/RunResponse"
                    },
                    {
                      "$ref": "#/components/schemas/AsyncSubmitResponse"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request / rejected input",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "402": {
            "description": "Insufficient ckTok balance",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          },
          "404": {
            "description": "Not found (e.g. idiom/word not in dictionary) — not an error, a normal result",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/v1/chinese/services": {
      "get": {
        "summary": "List available service_codes, prices, and payload shapes",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/v1/chinese/tasks/{task_id}": {
      "get": {
        "summary": "Poll an async task (decompose.wenyan_lesson only)",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "task_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "status: pending | running | done | error; data present when done"
          },
          "404": {
            "description": "Task not found or belongs to another account"
          }
        }
      }
    },
    "/v1/demo/run": {
      "post": {
        "summary": "Free, unauthenticated demo — rate-limited per IP, excludes decompose.wenyan_lesson",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RunRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          },
          "403": {
            "description": "service_code not available in demo"
          },
          "429": {
            "description": "Daily demo limit exceeded for this IP"
          }
        }
      }
    },
    "/v1/keys": {
      "post": {
        "summary": "Request an API key (email required, no card) — two-step: this sends a confirmation link, it does not return a key directly",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "env": {
                    "type": "string",
                    "enum": [
                      "live",
                      "test"
                    ],
                    "default": "live"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Verification email sent. Same response shape whether or not the email already has an account, by design — see /v1/keys/confirm."
          }
        }
      }
    },
    "/v1/keys/confirm": {
      "get": {
        "summary": "Confirmation landing page (does not create the key — the page's button triggers the POST below)",
        "responses": {
          "200": {
            "description": "HTML landing page"
          }
        }
      },
      "post": {
        "summary": "Create the key using a token from the confirmation email — one-time use, 15-minute expiry",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "token"
                ],
                "properties": {
                  "token": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Plaintext key shown once; never stored or shown again."
          },
          "400": {
            "description": "Token invalid, expired, already used, or account already has the maximum of 5 active keys."
          }
        }
      }
    },
    "/v1/account/balance": {
      "get": {
        "summary": "Current ckTok balance",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/v1/account/keys": {
      "get": {
        "summary": "List your API keys (prefix only, never full key or hash)",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/v1/account/keys/revoke": {
      "post": {
        "summary": "Revoke a key by prefix",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "key_prefix"
                ],
                "properties": {
                  "key_prefix": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          },
          "404": {
            "description": "Key not found or not yours"
          }
        }
      }
    },
    "/v1/account/ledger": {
      "get": {
        "summary": "Transaction history (grants + debits, newest first)",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 50,
              "maximum": 200
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/v1/account/usage": {
      "get": {
        "summary": "Call counts grouped by service_code",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
    "/v1/account/topup": {
      "post": {
        "summary": "Create a Stripe Checkout session to buy ckTok ($1 = 100 ckTok)",
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "usd_amount"
                ],
                "properties": {
                  "usd_amount": {
                    "type": "number",
                    "minimum": 1,
                    "maximum": 500
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "checkout_url to redirect the user to"
          },
          "503": {
            "description": "Stripe not yet configured for this deployment"
          }
        }
      }
    }
  }
}