openapi: 3.1.0
info:
  title: "SwapTxt Image Typography API"
  version: "1.0.0"
  contact:
    name: "SwapTxt Support"
    email: "support@swaptxt.com"
    url: "https://swaptxt.com/developers/"
  description: |
    Programmatic OCR and typography-preserving text replacement for JPEG, PNG, and WebP images.

    **Workflow**
    1. Call `POST /v1/ocr` to extract text lines and normalized 0-1000 bounding boxes (optional).
    2. Call `POST /v1/edits` with replacement instructions (with or without explicit boxes).
    3. Poll `GET /v1/edits/{id}` or receive a signed `editCompleted` webhook with Result A (AI render), Result B (pixel-preserved original blend), and Result C (4K master).

    **Limits**: 30 POST requests/min, 240 GET requests/min, up to 10 concurrent running jobs and 30 queued jobs per account.
servers:
  - url: "https://swaptxt.com/v1"
security:
  - bearerAuth: []
paths:
  /ocr:
    post:
      operationId: createOcr
      summary: Detect text lines and 0-1000 bounding boxes
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                image_url: { type: string, format: uri }
                image_base64: { type: string }
                image_id: { type: string }
                language: { type: string, example: en }
          multipart/form-data:
            schema:
              type: object
              required: [image]
              properties:
                image: { type: string, format: binary }
                language: { type: string }
      responses:
        "200":
          description: Detected text lines and reusable image_id (valid 24 hours; same image deduplicated within 24h).
  /edits:
    post:
      operationId: createEdit
      summary: Queue a typography-matched image text replacement job
      parameters:
        - name: Idempotency-Key
          in: header
          required: false
          schema: { type: string, maxLength: 128 }
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [replacements]
              properties:
                image_id: { type: string }
                image_url: { type: string, format: uri }
                image_base64: { type: string }
                replacements:
                  type: array
                  minItems: 1
                  maxItems: 20
                  items:
                    type: object
                    required: [from, to]
                    properties:
                      from: { type: string, maxLength: 200 }
                      to: { type: string, maxLength: 200 }
                      box:
                        type: array
                        minItems: 4
                        maxItems: 4
                        items: { type: integer, minimum: 0, maximum: 1000 }
                match: { type: string, enum: [all, first], default: all }
                language: { type: string, example: en }
                callback_url: { type: string, format: uri, example: "https://example.com/webhooks/swaptxt" }
      responses:
        "202":
          description: Job accepted (status queued or processing).
        "422":
          description: text_not_found — no OCR line matched a boxless replacement (not charged).
  /edits/{id}:
    get:
      operationId: getEdit
      summary: Fetch edit status and Result A / B / C URLs
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        "200":
          description: Current edit status, queue position, results, and credit balance.
  /account:
    get:
      operationId: getAccount
      summary: Retrieve remaining edit/OCR balances and rate limits
      responses:
        "200":
          description: Account balances, active packs, and rate/queue limits.
webhooks:
  editCompleted:
    post:
      operationId: editCompleted
      summary: Delivered when an edit finishes (succeeded or failed)
      description: |
        Signed with HMAC-SHA256 using your account webhook secret from the Developer Console.
        Verify header `X-SwapTxt-Signature: sha256=<hex>`. Failed deliveries retry at 1, 5, and 30 minutes.
      responses:
        "200": { description: Acknowledge receipt with any 2xx response within 10 seconds. }
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: "Send `Authorization: Bearer <your_api_key>` created in https://swaptxt.com/developers/keys/."
