开发文档 · 快速开始

快速开始

完成首次上传、提交、轮询与取结果。

前置条件

  • 一个有效的 API Key(见接入与密钥),至少包含 addDocument、document.write、viewTask 三个 scope。
  • 服务地址 https://<host>,由平台方提供;示例中以占位符表示。

第一步:上传文档,取得 doc_id

curl -X POST 'https://<host>/open/v1/documents' \
  -H 'X-Aistrong-Key: <API Key>' \
  -F 'file=@./合同.pdf' \
  -F 'client_request_id=upload-0001'
{
  "success": true,
  "data": {
    "doc_id": 1001,
    "status": "created"
  },
  "error": null,
  "metering": null,
  "request_id": "req-7f3c2a"
}

相同内容再次上传会返回同一个 doc_id;可用 resolveDocument 先按内容哈希检查是否已存在。

第二步:提交能力任务

以 PDF 合并为例(其余异步能力形式相同,见各接口页面的示例):

curl -X POST 'https://<host>/open/v1/documents:merge' \
  -H 'X-Aistrong-Key: <API Key>' \
  -H 'Content-Type: application/json' \
  -d '{"doc_ids": [1001, 1002], "client_request_id": "merge-0001"}'
HTTP/1.1 202 Accepted
Location: /open/v1/job/0123456789abcdef0123456789abcdef

{
  "success": true,
  "data": {
    "job_id": "0123456789abcdef0123456789abcdef",
    "operation": "mergePdfDocuments",
    "status": "queued",
    "poll_after_ms": 1000
  },
  "error": null,
  "metering": null,
  "request_id": "req-7f3c2a"
}

第三步:轮询并读取结果

import time
import requests

BASE = "https://<host>/open/v1"
HEADERS = {"X-Aistrong-Key": "<API Key>"}

def wait_job(job_id: str) -> dict:
    while True:
        job = requests.get(f"{BASE}/job/{job_id}", headers=HEADERS, timeout=30).json()["data"]
        if job["status"] in ("succeeded", "failed", "cancelled"):
            return job
        time.sleep(job["poll_after_ms"] / 1000)

job = wait_job("0123456789abcdef0123456789abcdef")
if job["status"] != "succeeded":
    raise RuntimeError(job["error"])
result = requests.get(f"{BASE}/job/{job['job_id']}/result", headers=HEADERS, timeout=30).json()["data"]

结果结构见各接口页面的「响应」一节;任务的模型与 OCR 计量随 metering 字段返回。

下一步