{
  "openapi": "3.1.0",
  "info": {
    "title": "buzzAI API",
    "version": "1.0.0",
    "description": "อ่านเอกสารบัญชีไทยและสเตทเมนต์ธนาคาร แล้วรับข้อมูลที่ตรวจตัวเลขแล้วเป็น JSON คู่มือภาษาไทย: https://heybuzzai.com/developers/",
    "contact": {
      "name": "buzzAI",
      "email": "support@heybuzzai.com",
      "url": "https://heybuzzai.com/developers/"
    }
  },
  "servers": [
    {
      "url": "https://heybuzzai.com/api/v1"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "paths": {
    "/documents": {
      "post": {
        "operationId": "createDocument",
        "summary": "ส่งไฟล์ให้อ่าน (1 ไฟล์ต่อคำขอ)",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 255
            },
            "description": "ค่าเดิมกับไฟล์เดิมภายใน 24 ชั่วโมงได้คำตอบแรกกลับมา ไม่สร้างเอกสารซ้ำและไม่คิดเงินซ้ำ"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": [
                  "file"
                ],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary",
                    "description": "JPEG PNG WebP หรือ PDF ไม่มีรหัสผ่าน (สเตทเมนต์: JPEG PNG PDF)"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "accounting-document",
                      "bank-statement"
                    ],
                    "default": "accounting-document"
                  },
                  "client_id": {
                    "type": "string",
                    "description": "ลูกค้าของสำนักงาน ไม่ระบุ = ลูกค้า \"ไม่ระบุลูกค้า (ส่งผ่าน API)\""
                  },
                  "wait": {
                    "type": "string",
                    "description": "true (รอสูงสุด 60 วินาที) false หรือจำนวนวินาที 1–60",
                    "default": "false"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "อ่านเสร็จทุกเอกสารจากไฟล์นี้ (ใช้ wait)",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentBatch"
                },
                "example": {
                  "data": [
                    {
                      "id": "b1c3e1d2-4a5b-4c6d-8e9f-0a1b2c3d4e5f",
                      "object": "document",
                      "type": "accounting-document",
                      "status": "ready",
                      "client_id": "7f0e6d5c-4b3a-4291-8f7e-6d5c4b3a2918",
                      "file": {
                        "name": "IV2026-00123.pdf",
                        "mime_type": "application/pdf",
                        "size": 184220
                      },
                      "part": null,
                      "units": 1,
                      "pages": null,
                      "checks": {
                        "passed": true,
                        "issues": []
                      },
                      "corrected": false,
                      "error": null,
                      "data": {
                        "document_type": "tax_invoice",
                        "document_number": "IV2026-00123",
                        "document_date": "2026-10-05",
                        "reference_document_number": null,
                        "currency": "THB",
                        "issuer": {
                          "name": "บริษัท ตัวอย่าง จำกัด",
                          "tax_id": "9905556123450",
                          "branch": "00000",
                          "address": "99 ถนนสุขุมวิท กรุงเทพฯ 10110"
                        },
                        "recipient": {
                          "name": "บริษัท ลูกค้า จำกัด",
                          "tax_id": "9915556123450",
                          "branch": "00000",
                          "address": null
                        },
                        "vat_treatment": "separated",
                        "amounts": {
                          "subtotal": 1000,
                          "discount": null,
                          "service_charge": null,
                          "vat": 70,
                          "total": 1070,
                          "withholding_tax": null
                        },
                        "amount_in_words": "หนึ่งพันเจ็ดสิบบาทถ้วน",
                        "withholding": {
                          "form": null,
                          "lines": []
                        },
                        "items": [
                          {
                            "description": "ค่าบริการทำบัญชี เดือนกันยายน",
                            "quantity": 1,
                            "unit_price": 1000,
                            "amount": 1000
                          }
                        ]
                      },
                      "created_at": "2026-10-21T03:00:00.000Z",
                      "updated_at": "2026-10-21T03:00:12.000Z",
                      "confirmed_at": null
                    }
                  ]
                }
              }
            }
          },
          "202": {
            "description": "รับไฟล์แล้ว ยังอ่านอยู่ ดึงผลด้วย GET /documents/{id}",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentBatch"
                },
                "example": {
                  "data": [
                    {
                      "id": "b1c3e1d2-4a5b-4c6d-8e9f-0a1b2c3d4e5f",
                      "object": "document",
                      "type": "accounting-document",
                      "status": "processing",
                      "client_id": "7f0e6d5c-4b3a-4291-8f7e-6d5c4b3a2918",
                      "file": {
                        "name": "IV2026-00123.pdf",
                        "mime_type": "application/pdf",
                        "size": 184220
                      },
                      "part": null,
                      "units": null,
                      "pages": null,
                      "checks": null,
                      "corrected": false,
                      "error": null,
                      "data": null,
                      "created_at": "2026-10-21T03:00:00.000Z",
                      "updated_at": "2026-10-21T03:00:00.000Z",
                      "confirmed_at": null
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "invalid_request, invalid_parameter, invalid_type, file_required, too_many_files, file_empty, invalid_idempotency_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "quota_exceeded: API key ใช้ครบโควตาเดือนนี้",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "client_not_found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "idempotency_conflict หรือ idempotency_in_progress",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "file_too_large",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "multipart_required หรือ unsupported_file_type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "pdf_encrypted, pdf_unreadable หรือ too_many_pages",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "get": {
        "operationId": "listDocuments",
        "summary": "รายการเอกสารของสำนักงาน ใหม่สุดก่อน",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "คั่นด้วยจุลภาค: processing, in_review, ready, confirmed, failed, rejected"
          },
          {
            "name": "type",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "accounting-document",
                "bank-statement"
              ]
            }
          },
          {
            "name": "client_id",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "since",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "เอกสารที่เปลี่ยนแปลงตั้งแต่เวลานี้: YYYY-MM-DD (เวลาไทย) หรือ ISO 8601"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 50
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "next_cursor จากหน้าก่อน"
          }
        ],
        "responses": {
          "200": {
            "description": "หนึ่งหน้า",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DocumentList"
                }
              }
            }
          },
          "400": {
            "description": "invalid_parameter",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "client_not_found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/documents/{id}": {
      "get": {
        "operationId": "getDocument",
        "summary": "สถานะและข้อมูลของเอกสาร",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "เอกสาร",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Document"
                },
                "example": {
                  "id": "b1c3e1d2-4a5b-4c6d-8e9f-0a1b2c3d4e5f",
                  "object": "document",
                  "type": "accounting-document",
                  "status": "ready",
                  "client_id": "7f0e6d5c-4b3a-4291-8f7e-6d5c4b3a2918",
                  "file": {
                    "name": "IV2026-00123.pdf",
                    "mime_type": "application/pdf",
                    "size": 184220
                  },
                  "part": null,
                  "units": 1,
                  "pages": null,
                  "checks": {
                    "passed": true,
                    "issues": []
                  },
                  "corrected": false,
                  "error": null,
                  "data": {
                    "document_type": "tax_invoice",
                    "document_number": "IV2026-00123",
                    "document_date": "2026-10-05",
                    "reference_document_number": null,
                    "currency": "THB",
                    "issuer": {
                      "name": "บริษัท ตัวอย่าง จำกัด",
                      "tax_id": "9905556123450",
                      "branch": "00000",
                      "address": "99 ถนนสุขุมวิท กรุงเทพฯ 10110"
                    },
                    "recipient": {
                      "name": "บริษัท ลูกค้า จำกัด",
                      "tax_id": "9915556123450",
                      "branch": "00000",
                      "address": null
                    },
                    "vat_treatment": "separated",
                    "amounts": {
                      "subtotal": 1000,
                      "discount": null,
                      "service_charge": null,
                      "vat": 70,
                      "total": 1070,
                      "withholding_tax": null
                    },
                    "amount_in_words": "หนึ่งพันเจ็ดสิบบาทถ้วน",
                    "withholding": {
                      "form": null,
                      "lines": []
                    },
                    "items": [
                      {
                        "description": "ค่าบริการทำบัญชี เดือนกันยายน",
                        "quantity": 1,
                        "unit_price": 1000,
                        "amount": 1000
                      }
                    ]
                  },
                  "created_at": "2026-10-21T03:00:00.000Z",
                  "updated_at": "2026-10-21T03:00:12.000Z",
                  "confirmed_at": null
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "document_not_found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/clients": {
      "get": {
        "operationId": "listClients",
        "summary": "ลูกค้าของสำนักงาน",
        "responses": {
          "200": {
            "description": "ลูกค้าทั้งหมด",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ClientList"
                },
                "example": {
                  "data": [
                    {
                      "id": "7f0e6d5c-4b3a-4291-8f7e-6d5c4b3a2918",
                      "object": "client",
                      "name": "บริษัท ลูกค้า จำกัด",
                      "tax_id": "9915556123450"
                    }
                  ]
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "operationId": "createClient",
        "summary": "เพิ่มลูกค้า",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/NewClient"
              },
              "example": {
                "name": "บริษัท ลูกค้า จำกัด",
                "tax_id": "9915556123450"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "ลูกค้าที่สร้าง",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Client"
                },
                "example": {
                  "id": "7f0e6d5c-4b3a-4291-8f7e-6d5c4b3a2918",
                  "object": "client",
                  "name": "บริษัท ลูกค้า จำกัด",
                  "tax_id": "9915556123450"
                }
              }
            }
          },
          "400": {
            "description": "invalid_request หรือ invalid_parameter",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "unsupported_media_type",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/usage": {
      "get": {
        "operationId": "getUsage",
        "summary": "การใช้งานเดือนนี้",
        "responses": {
          "200": {
            "description": "การใช้งาน",
            "headers": {
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Usage"
                },
                "example": {
                  "object": "usage",
                  "month": "2026-10",
                  "period": {
                    "start": "2026-09-30T17:00:00.000Z",
                    "end": "2026-10-31T17:00:00.000Z"
                  },
                  "units": 1250,
                  "free_trial": {
                    "total": 200,
                    "used": 200,
                    "remaining": 0,
                    "started_at": "2026-09-12T02:15:00.000Z",
                    "ends_at": "2026-10-12T02:15:00.000Z"
                  },
                  "plan": {
                    "id": "pro",
                    "name": "Pro",
                    "monthly_units": 5000,
                    "used": 1180,
                    "remaining": 3820
                  },
                  "key": {
                    "id": "0c1d2e3f-4a5b-4c6d-9e8f-7a6b5c4d3e2f",
                    "name": "ระบบบัญชีของสำนักงาน",
                    "prefix": "bz_live_Xy12ab",
                    "monthly_cap": 3000,
                    "used": 940,
                    "remaining": 2060
                  }
                }
              }
            }
          },
          "401": {
            "description": "missing_api_key หรือ invalid_api_key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "วินาทีที่ต้องรอ",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Limit": {
                "description": "คำขอสูงสุดต่อนาทีของ API key นี้ (60)",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Remaining": {
                "description": "คำขอที่เหลือในนาทีนี้",
                "schema": {
                  "type": "integer"
                }
              },
              "X-RateLimit-Reset": {
                "description": "วินาทีจนเริ่มนาทีถัดไป",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "500": {
            "description": "internal_error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "Authorization: Bearer bz_live_… (ขอ API key จากทีม buzzAI)"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message",
              "message_en"
            ],
            "properties": {
              "code": {
                "type": "string",
                "enum": [
                  "missing_api_key",
                  "invalid_api_key",
                  "quota_exceeded",
                  "not_found",
                  "document_not_found",
                  "client_not_found",
                  "method_not_allowed",
                  "invalid_request",
                  "invalid_parameter",
                  "invalid_type",
                  "file_required",
                  "too_many_files",
                  "file_empty",
                  "invalid_idempotency_key",
                  "idempotency_conflict",
                  "idempotency_in_progress",
                  "file_too_large",
                  "multipart_required",
                  "unsupported_media_type",
                  "unsupported_file_type",
                  "pdf_encrypted",
                  "pdf_unreadable",
                  "too_many_pages",
                  "rate_limited",
                  "internal_error",
                  "service_unavailable"
                ]
              },
              "message": {
                "type": "string",
                "description": "ภาษาไทย"
              },
              "message_en": {
                "type": "string",
                "description": "English"
              }
            },
            "description": "code คงที่ ใช้ตัดสินใจในโค้ดได้ ส่วน message เป็นข้อความสำหรับคน"
          }
        }
      },
      "Party": {
        "type": "object",
        "required": [
          "name",
          "tax_id",
          "branch",
          "address"
        ],
        "properties": {
          "name": {
            "type": [
              "string",
              "null"
            ]
          },
          "tax_id": {
            "type": [
              "string",
              "null"
            ],
            "description": "เลขประจำตัวผู้เสียภาษี 13 หลัก ตามที่พิมพ์"
          },
          "branch": {
            "type": [
              "string",
              "null"
            ],
            "description": "รหัสสาขา 5 หลัก (00000 = สำนักงานใหญ่)"
          },
          "address": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "AccountingData": {
        "type": "object",
        "required": [
          "document_type",
          "document_number",
          "document_date",
          "reference_document_number",
          "currency",
          "issuer",
          "recipient",
          "vat_treatment",
          "amounts",
          "amount_in_words",
          "withholding",
          "items"
        ],
        "properties": {
          "document_type": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "tax_invoice",
              "abbreviated_tax_invoice",
              "invoice",
              "receipt",
              "withholding_tax_certificate",
              "credit_note",
              "debit_note",
              "other",
              null
            ]
          },
          "document_number": {
            "type": [
              "string",
              "null"
            ]
          },
          "document_date": {
            "type": [
              "string",
              "null"
            ],
            "format": "date",
            "description": "YYYY-MM-DD (ค.ศ.)"
          },
          "reference_document_number": {
            "type": [
              "string",
              "null"
            ],
            "description": "เลขที่ใบกำกับเดิม เฉพาะใบลดหนี้และใบเพิ่มหนี้"
          },
          "currency": {
            "type": [
              "string",
              "null"
            ]
          },
          "issuer": {
            "$ref": "#/components/schemas/Party"
          },
          "recipient": {
            "$ref": "#/components/schemas/Party"
          },
          "vat_treatment": {
            "type": [
              "string",
              "null"
            ],
            "enum": [
              "separated",
              "none",
              "included_not_shown",
              null
            ]
          },
          "amounts": {
            "type": "object",
            "required": [
              "subtotal",
              "discount",
              "service_charge",
              "vat",
              "total",
              "withholding_tax"
            ],
            "properties": {
              "subtotal": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "discount": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "service_charge": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "vat": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "total": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "withholding_tax": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              }
            }
          },
          "amount_in_words": {
            "type": [
              "string",
              "null"
            ]
          },
          "withholding": {
            "type": "object",
            "required": [
              "form",
              "lines"
            ],
            "properties": {
              "form": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "แบบ ภ.ง.ด. เช่น ภ.ง.ด.53"
              },
              "lines": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "income_type",
                    "date_paid",
                    "amount",
                    "tax"
                  ],
                  "properties": {
                    "income_type": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "date_paid": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "amount": {
                      "type": [
                        "number",
                        "null"
                      ],
                      "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                    },
                    "tax": {
                      "type": [
                        "number",
                        "null"
                      ],
                      "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                    }
                  }
                }
              }
            }
          },
          "items": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "description",
                "quantity",
                "unit_price",
                "amount"
              ],
              "properties": {
                "description": {
                  "type": [
                    "string",
                    "null"
                  ]
                },
                "quantity": {
                  "type": [
                    "number",
                    "null"
                  ]
                },
                "unit_price": {
                  "type": [
                    "number",
                    "null"
                  ],
                  "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                },
                "amount": {
                  "type": [
                    "number",
                    "null"
                  ],
                  "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                }
              }
            }
          }
        },
        "description": "ข้อมูลเอกสารบัญชี (ใช้ค่าที่คนตรวจทานแก้ ถ้ามี)"
      },
      "StatementData": {
        "type": "object",
        "required": [
          "account",
          "currency",
          "period",
          "balances",
          "totals",
          "transactions"
        ],
        "properties": {
          "account": {
            "type": "object",
            "required": [
              "bank",
              "name",
              "number"
            ],
            "properties": {
              "bank": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "name": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "number": {
                "type": [
                  "string",
                  "null"
                ],
                "description": "ตามที่พิมพ์ อาจถูกปิดบางหลัก"
              }
            }
          },
          "currency": {
            "type": "string",
            "enum": [
              "THB"
            ]
          },
          "period": {
            "type": "object",
            "required": [
              "start",
              "end"
            ],
            "properties": {
              "start": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date"
              },
              "end": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date"
              }
            }
          },
          "balances": {
            "type": "object",
            "required": [
              "opening",
              "closing"
            ],
            "properties": {
              "opening": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "closing": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              }
            }
          },
          "totals": {
            "type": "object",
            "required": [
              "deposits",
              "withdrawals"
            ],
            "properties": {
              "deposits": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              },
              "withdrawals": {
                "type": [
                  "number",
                  "null"
                ],
                "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
              }
            },
            "description": "ยอดรวมฝากและถอน เฉพาะเมื่อพิมพ์ไว้บนสเตทเมนต์"
          },
          "transactions": {
            "type": "array",
            "description": "เรียงจากเก่าไปใหม่",
            "items": {
              "type": "object",
              "required": [
                "date",
                "description",
                "withdrawal",
                "deposit",
                "balance"
              ],
              "properties": {
                "date": {
                  "type": [
                    "string",
                    "null"
                  ],
                  "format": "date"
                },
                "description": {
                  "type": [
                    "string",
                    "null"
                  ]
                },
                "withdrawal": {
                  "type": [
                    "number",
                    "null"
                  ],
                  "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                },
                "deposit": {
                  "type": [
                    "number",
                    "null"
                  ],
                  "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                },
                "balance": {
                  "type": [
                    "number",
                    "null"
                  ],
                  "description": "บาท ทศนิยมไม่เกิน 2 ตำแหน่ง"
                }
              }
            }
          }
        },
        "description": "ข้อมูลสเตทเมนต์ธนาคาร"
      },
      "Checks": {
        "type": "object",
        "required": [
          "passed",
          "issues"
        ],
        "properties": {
          "passed": {
            "type": "boolean",
            "description": "true เมื่อไม่มีเหตุให้ตรวจ"
          },
          "issues": {
            "type": "array",
            "items": {
              "type": "object",
              "required": [
                "code",
                "message",
                "message_en"
              ],
              "properties": {
                "code": {
                  "type": "string",
                  "enum": [
                    "total_illegible",
                    "tax_id_illegible",
                    "date_illegible",
                    "doc_number_illegible",
                    "document_illegible",
                    "multiple_documents",
                    "date_ambiguous",
                    "page_incomplete",
                    "altered_document",
                    "unsupported_document",
                    "amount_mismatch",
                    "vat_rate_mismatch",
                    "amount_words_mismatch",
                    "invalid_tax_id",
                    "wht_rate_mismatch",
                    "items_mismatch",
                    "invalid_branch",
                    "date_out_of_range",
                    "client_not_party",
                    "vat_missing",
                    "amount_unconfirmed",
                    "multiple_accounts",
                    "balance_mismatch",
                    "running_balance_mismatch",
                    "totals_mismatch",
                    "amount_side",
                    "date_unreadable",
                    "dates_not_ascending",
                    "date_outside_period",
                    "nothing_to_check",
                    "needs_attention"
                  ]
                },
                "message": {
                  "type": "string",
                  "description": "ภาษาไทย"
                },
                "message_en": {
                  "type": "string",
                  "description": "English"
                }
              },
              "description": "เหตุที่ระบบแจ้งให้ตรวจ"
            }
          },
          "results": {
            "type": "array",
            "description": "เฉพาะสเตทเมนต์: ผลการตรวจแต่ละข้อ",
            "items": {
              "type": "object",
              "required": [
                "code",
                "description",
                "description_en",
                "passed",
                "rows"
              ],
              "properties": {
                "code": {
                  "type": "string"
                },
                "description": {
                  "type": "string"
                },
                "description_en": {
                  "type": "string"
                },
                "passed": {
                  "type": [
                    "boolean",
                    "null"
                  ],
                  "description": "null เมื่อไม่มียอดให้ตรวจข้อนี้"
                },
                "rows": {
                  "type": "array",
                  "items": {
                    "type": "integer"
                  },
                  "description": "ลำดับรายการ (เริ่มที่ 1) ที่ไม่ผ่าน"
                }
              }
            }
          }
        }
      },
      "Document": {
        "type": "object",
        "required": [
          "id",
          "object",
          "type",
          "status",
          "client_id",
          "file",
          "part",
          "units",
          "pages",
          "checks",
          "corrected",
          "error",
          "data",
          "created_at",
          "updated_at",
          "confirmed_at"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "object": {
            "type": "string",
            "enum": [
              "document"
            ]
          },
          "type": {
            "type": "string",
            "enum": [
              "accounting-document",
              "bank-statement"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "processing",
              "in_review",
              "ready",
              "confirmed",
              "failed",
              "rejected"
            ]
          },
          "client_id": {
            "type": [
              "string",
              "null"
            ]
          },
          "file": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "name": {
                "type": "string"
              },
              "mime_type": {
                "type": "string"
              },
              "size": {
                "type": "integer"
              }
            }
          },
          "part": {
            "type": [
              "object",
              "null"
            ],
            "description": "เมื่อไฟล์เดียวมีหลายเอกสาร: เอกสารนี้เป็นใบที่เท่าไรจากทั้งหมด",
            "properties": {
              "index": {
                "type": "integer"
              },
              "of": {
                "type": "integer"
              },
              "source_document_id": {
                "type": "string"
              },
              "document_ids": {
                "type": "array",
                "items": {
                  "type": "string"
                },
                "description": "ทุกเอกสารจากไฟล์เดียวกัน เฉพาะ GET ของใบแรก"
              }
            }
          },
          "units": {
            "type": [
              "integer",
              "null"
            ],
            "description": "หน่วยที่คิดเงิน: null ระหว่างอ่าน 0 เมื่อ failed หรือ rejected"
          },
          "pages": {
            "type": [
              "integer",
              "null"
            ],
            "description": "จำนวนหน้า เฉพาะสเตทเมนต์"
          },
          "checks": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/Checks"
              },
              {
                "type": "null"
              }
            ]
          },
          "corrected": {
            "type": "boolean",
            "description": "คนตรวจทานแก้ข้อมูลก่อนยืนยัน"
          },
          "error": {
            "oneOf": [
              {
                "type": "object",
                "required": [
                  "code",
                  "message",
                  "message_en"
                ],
                "properties": {
                  "code": {
                    "type": "string",
                    "enum": [
                      "pdf_encrypted",
                      "pdf_unreadable",
                      "document_too_long",
                      "currency_not_supported",
                      "processing_failed"
                    ]
                  },
                  "message": {
                    "type": "string",
                    "description": "ภาษาไทย"
                  },
                  "message_en": {
                    "type": "string",
                    "description": "English"
                  }
                },
                "description": "เหตุที่อ่านไม่สำเร็จ"
              },
              {
                "type": "null"
              }
            ]
          },
          "data": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/AccountingData"
              },
              {
                "$ref": "#/components/schemas/StatementData"
              },
              {
                "type": "null"
              }
            ],
            "description": "มีเฉพาะสถานะ ready และ confirmed"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          },
          "updated_at": {
            "type": "string",
            "format": "date-time"
          },
          "confirmed_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      },
      "DocumentList": {
        "type": "object",
        "required": [
          "data",
          "next_cursor"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Document"
            }
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "DocumentBatch": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Document"
            }
          }
        }
      },
      "Client": {
        "type": "object",
        "required": [
          "id",
          "object",
          "name",
          "tax_id"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "object": {
            "type": "string",
            "enum": [
              "client"
            ]
          },
          "name": {
            "type": "string"
          },
          "tax_id": {
            "type": [
              "string",
              "null"
            ]
          }
        }
      },
      "ClientList": {
        "type": "object",
        "required": [
          "data"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Client"
            }
          }
        }
      },
      "NewClient": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200
          },
          "tax_id": {
            "type": [
              "string",
              "null"
            ],
            "description": "ตัวเลข 13 หลัก (ขีดและช่องว่างถูกตัดออก)"
          }
        }
      },
      "Usage": {
        "type": "object",
        "required": [
          "object",
          "month",
          "period",
          "units",
          "free_trial",
          "plan",
          "key"
        ],
        "properties": {
          "object": {
            "type": "string",
            "enum": [
              "usage"
            ]
          },
          "month": {
            "type": "string",
            "description": "YYYY-MM ตามเวลาไทย"
          },
          "period": {
            "type": "object",
            "required": [
              "start",
              "end"
            ],
            "properties": {
              "start": {
                "type": "string",
                "format": "date-time"
              },
              "end": {
                "type": "string",
                "format": "date-time"
              }
            }
          },
          "units": {
            "type": "integer",
            "description": "หน่วยที่คิดเงินของสำนักงานเดือนนี้ รวมทุกช่องทาง"
          },
          "free_trial": {
            "type": "object",
            "required": [
              "total",
              "used",
              "remaining",
              "started_at",
              "ends_at"
            ],
            "properties": {
              "total": {
                "type": "integer"
              },
              "used": {
                "type": "integer"
              },
              "remaining": {
                "type": "integer"
              },
              "started_at": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date-time"
              },
              "ends_at": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date-time"
              }
            }
          },
          "plan": {
            "type": [
              "object",
              "null"
            ],
            "description": "null เมื่อจ่ายตามจำนวนเอกสาร",
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "monthly_units": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "used": {
                "type": "integer"
              },
              "remaining": {
                "type": [
                  "integer",
                  "null"
                ]
              }
            }
          },
          "key": {
            "type": "object",
            "required": [
              "id",
              "name",
              "prefix",
              "monthly_cap",
              "used",
              "remaining"
            ],
            "properties": {
              "id": {
                "type": "string"
              },
              "name": {
                "type": "string"
              },
              "prefix": {
                "type": "string"
              },
              "monthly_cap": {
                "type": [
                  "integer",
                  "null"
                ]
              },
              "used": {
                "type": "integer"
              },
              "remaining": {
                "type": [
                  "integer",
                  "null"
                ]
              }
            }
          }
        }
      }
    }
  }
}
