Key Extraction · API v3
สูตรสำเร็จ
ตัวอย่างสั้นๆ ที่คัดลอกไปใช้ได้ — ปรับชื่อไฟล์และ API key ตามสภาพแวดล้อม
ใบแจ้งหนี้เป็น JSON + bbox
import time, requestsAPI = "https://backend.aksonocr.com/api/v3/key-extract"headers = {"X-API-Key": "<YOUR_API_KEY>"}with open("invoice.pdf", "rb") as f:file_id = requests.post(f"{API}/files", headers=headers, files={"files": f}).json()["files"][0]["id"]job_id = requests.post(f"{API}/jobs",headers={**headers, "Idempotency-Key": "invoice-md-001"},json={"inputs": [{"file_id": file_id}],"data_schema": {"type": "object","additionalProperties": False,"properties": {"invoice_no": {"type": "string", "description": "เลขที่ใบแจ้งหนี้"},"total": {"type": "number", "description": "ยอดรวม"},"line_items": {"type": "array","description": "รายการสินค้า","items": {"type": "object","additionalProperties": False,"properties": {"description": {"type": "string"},"amount": {"type": "number"},},},},},},"output": {"evidence": "bbox"},},).json()["id"]while True:status = requests.get(f"{API}/jobs/{job_id}", headers=headers).json()["status"]if status in {"completed", "failed", "cancelled", "expired"}:breaktime.sleep(2)result = requests.get(f"{API}/jobs/{job_id}/result", headers=headers).json()print(result["data"])print(result["field_metadata"]["/invoice_no"]["regions"][0]["bbox"])
วาด overlay จาก bbox
คูณค่า 0–1 ด้วยความกว้าง/สูงของหน้าจาก pages[]
function drawField(ctx, region, pageWidth, pageHeight) {const [x1, y1, x2, y2] = region.bbox;ctx.strokeStyle = "#ec4899";ctx.strokeRect(x1 * pageWidth,y1 * pageHeight,(x2 - x1) * pageWidth,(y2 - y1) * pageHeight,);}for (const [pointer, meta] of Object.entries(result.field_metadata)) {for (const region of meta.regions) {const page = result.pages.find((p) => p.page === region.page);drawField(ctx, region, page.width, page.height);}}
ดึงเฉพาะหน้า 3–5 ของ PDF
FILE_ID=$(curl -sS -X POST 'https://backend.aksonocr.com/api/v3/key-extract/files' \-H 'X-API-Key: <YOUR_API_KEY>' \-F '[email protected]' | jq -r '.files[0].id')curl -sS -X POST 'https://backend.aksonocr.com/api/v3/key-extract/jobs' \-H 'X-API-Key: <YOUR_API_KEY>' \-H 'Content-Type: application/json' \-H 'Idempotency-Key: pages-3-5' \-d "{\"inputs\": [{ \"file_id\": \"$FILE_ID\", \"pages\": [3, 4, 5] }],\"data_schema\": {\"type\": \"object\",\"properties\": { \"title\": { \"type\": \"string\" } }},\"output\": { \"evidence\": \"bbox\" }}"
ไม่ต้องการ bbox
ส่ง output.evidence: "none" เมื่อต้องการแค่ค่าใน data — regions จะเป็นอาร์เรย์ว่าง
{"inputs": [{ "file_id": "file_01J9..." }],"data_schema": {"type": "object","properties": {"invoice_no": { "type": "string" }}},"output": { "evidence": "none" }}