Skip to content

运行提取

POST /v1/extractions/run

对 PDF 文档运行提取。文档将使用指定的模板进行处理,提取的数据以与模板字段定义匹配的结构化 JSON 形式返回。

试一试

Swagger UI 中交互式测试此端点。

需要认证

Authorization 头中包含您的 API 密钥。

请求

请求头

必需
AuthorizationBearer <token>
Content-Typeapplication/json

查询参数

参数类型必需描述
asyncstring设置为 "true" 时立即返回 processing 状态,而不是等待完成。默认行为(省略或 "false")为同步模式。

请求体

字段类型必需描述
templateIdstring要使用的提取模板 ID。
fileNamestring文档的原始文件名。
pdfBase64stringPDF 文件的 Base64 编码内容。
mimeTypestring文件的 MIME 类型。接受的值:application/pdfapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
runIdstring可选标识符,用于将相关提取分组到一个批次中。

代码示例

bash
curl -X POST https://api.docmap.io/v1/extractions/run \
  -H "Authorization: Bearer dm_live_abc123def456ghi789jkl012mno345" \
  -H "Content-Type: application/json" \
  -d '{
    "templateId": "tmpl_8f3a2b1c4d5e6f7g",
    "fileName": "invoice-2024-001.pdf",
    "pdfBase64": "JVBERi0xLjQKMSAwIG9iago8PAovVHlwZSAvQ2F0YW...",
    "mimeType": "application/pdf"
  }'
typescript
const apiKey = process.env.DOCMAP_API_KEY

const response = await fetch('https://api.docmap.io/v1/extractions/run', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    templateId: 'tmpl_8f3a2b1c4d5e6f7g',
    fileName: 'invoice-2024-001.pdf',
    pdfBase64: pdfBuffer.toString('base64'),
    mimeType: 'application/pdf',
  }),
})

const { data } = await response.json()
console.log(data.extractedData)
python
import requests
import base64

api_key = "dm_live_abc123def456ghi789jkl012mno345"

with open("invoice-2024-001.pdf", "rb") as f:
    pdf_base64 = base64.b64encode(f.read()).decode("utf-8")

response = requests.post(
    "https://api.docmap.io/v1/extractions/run",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    },
    json={
        "templateId": "tmpl_8f3a2b1c4d5e6f7g",
        "fileName": "invoice-2024-001.pdf",
        "pdfBase64": pdf_base64,
        "mimeType": "application/pdf",
    },
)

data = response.json()["data"]
print(data["extractedData"])

响应

状态码:200 OK

响应体包含在 data 对象中。

字段

字段类型描述
idstring唯一提取 ID(以 extract_ 为前缀)。
userIdstring拥有此提取的用户 ID。
templateIdstring用于提取的模板 ID。
templateNamestring所使用模板的显示名称。
fileNamestring上传文档的原始文件名。
status"processing" | "completed" | "failed"当前提取状态。使用异步模式时为 "processing",成功时为 "completed",错误时为 "failed"
extractedDataobject | null与模板字段匹配的提取数据。如果提取失败则为 null
errorstring | null描述失败原因的错误信息。如果提取成功则为 null
variablesVariable[]提取过程中使用的模板变量定义数组。
source"dashboard" | "api"提取的触发方式。
runIdstring | null批次运行 ID(如果在请求中提供了的话)。
processingTimeMsnumber | null总处理时长(毫秒)。
createdAtstring提取创建时间的 ISO 8601 时间戳。

示例

json
{
  "data": {
    "id": "extract_9k2m4n6p8q0r1s3t",
    "userId": "uid_a1b2c3d4e5f6",
    "templateId": "tmpl_8f3a2b1c4d5e6f7g",
    "templateName": "Invoice Template",
    "fileName": "invoice-2024-001.pdf",
    "status": "completed",
    "extractedData": {
      "vendor_name": "Acme Corp",
      "invoice_number": "INV-2024-001",
      "invoice_date": "2024-11-15",
      "total_amount": 1250.00,
      "currency": "USD",
      "line_items": [
        {
          "description": "Widget A",
          "quantity": 10,
          "unit_price": 125.00
        }
      ]
    },
    "error": null,
    "variables": [
      {
        "name": "vendor_name",
        "type": "string",
        "description": "Name of the vendor or supplier"
      },
      {
        "name": "total_amount",
        "type": "number",
        "description": "Total invoice amount"
      }
    ],
    "source": "api",
    "runId": null,
    "processingTimeMs": 3842,
    "createdAt": "2024-11-20T14:30:00.000Z"
  }
}

异步模式

默认情况下,API 会等待提取完成后再响应(同步模式)。对于长时间运行的提取,您可以使用异步模式获得即时响应,然后轮询结果。

在 URL 中添加 ?async=true 以启用异步模式:

bash
curl -X POST "https://api.docmap.io/v1/extractions/run?async=true" \
  -H "Authorization: Bearer dm_live_abc123def456ghi789jkl012mno345" \
  -H "Content-Type: application/json" \
  -d '{
    "templateId": "tmpl_8f3a2b1c4d5e6f7g",
    "fileName": "invoice-2024-001.pdf",
    "pdfBase64": "JVBERi0xLjQKMSAwIG9iago8PAovVHlwZSAvQ2F0YW...",
    "mimeType": "application/pdf"
  }'

响应:202 Accepted

响应结构与同步响应相同,但 status 将为 "processing"extractedData 将为 nullprocessingTimeMs 将为 null

json
{
  "data": {
    "id": "extract_9k2m4n6p8q0r1s3t",
    "userId": "uid_a1b2c3d4e5f6",
    "templateId": "tmpl_8f3a2b1c4d5e6f7g",
    "templateName": "Invoice Template",
    "fileName": "invoice-2024-001.pdf",
    "status": "processing",
    "extractedData": null,
    "error": null,
    "variables": [...],
    "source": "api",
    "runId": null,
    "processingTimeMs": null,
    "createdAt": "2024-11-20T14:30:00.000Z"
  }
}

使用返回的 id 轮询获取提取端点,直到 status 变为 "completed""failed"

WARNING

如果提取在 "processing" 状态超过 2 分钟,应将其视为失败。系统没有自动超时机制 -- 在极少数情况下(例如服务器重启),记录可能会无限期保持 "processing" 状态。

错误

状态码错误码描述
401UNAUTHORIZED缺少、无效或已过期的 API 密钥/令牌。
404NOT_FOUND指定的模板未找到或不属于您的账户。
429USAGE_LIMIT_EXCEEDED您已达到计划的月度提取限额。
500INTERNAL_ERROR提取处理过程中发生意外错误。

DocMap API 文档