跳到正文

快速入门

5 分钟内开始使用 OpenOCR。

1. 获取你的 API 密钥

在 open-ocr.com/signup 注册一个免费账户。新账户包含起步额度。然后在你的控制台中生成一个 API 密钥。

创建账户和 API 密钥需要真人完成(CAPTCHA 验证)。AI 代理:请向你的用户索取一个 sk-ocr- 密钥。

如何选择引擎

  • 通用文档、扫描件和表格:mistral-mistral-ocr-4-1。这是我们推荐的默认引擎,原生支持多页 PDF。
  • 质量尚可且最便宜:google-gemini-3.1-flash-lite。
  • Mistral OCR 4.1 可能会规范拼写(例如纠正明显的拼写错误);如需严格逐字转录,LLM 视觉引擎会遵循我们的逐字转录指令。
  • 在排名页面对比所有引擎。

2. 发出你的第一个请求

使用你的 API 密钥和图像 URL 向 OCR 端点发送一个 POST 请求。每个请求都需要一个 Idempotency-Key 请求头,以防止重复处理:

bash
curl -X POST https://api.open-ocr.com/v1/ocr \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "engine": "mistral-mistral-ocr-4-1",
    "input": {
      "type": "url",
      "url": "https://tesseract-ocr.github.io/tessdoc/images/eurotext.png"
    }
  }'

3. 读取响应

json
{
  "request_id": "req_abc123",
  "status": "succeeded",
  "engine": "mistral-mistral-ocr-4-1",
  "extracted_text": "INVOICE #12345\nDate: January 15, 2024\n...",
  "extracted_text_preview": "INVOICE #12345...",
  "cost_debited": 0.004,
  "remaining_balance": 0.996,
  "provider_latency_ms": 280
}

每页费用取决于你选择的引擎。当前费率请查看定价页面。remaining_balance 字段会显示你更新后的额度余额。

4. 使用 base64 输入

你也可以将图像以 base64 编码数据的形式发送——当文件位于内存中时很有用:

bash
# Encode a local file and send it
BASE64=$(base64 -w0 receipt.png)

curl -X POST https://api.open-ocr.com/v1/ocr \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "engine": "mistral-mistral-ocr-4-1",
    "input": {
      "type": "base64",
      "data_base64": "'$BASE64'",
      "mime_type": "image/png"
    }
  }'

5. 使用任意语言调用

OpenOCR 是一个纯粹的 REST + JSON API——无需安装任何 SDK。使用你已有的任意 HTTP 客户端即可。

python
import uuid
import requests

resp = requests.post(
    "https://api.open-ocr.com/v1/ocr",
    headers={
        "Authorization": "Bearer sk-ocr-your-api-key",
        "Content-Type": "application/json",
        "Idempotency-Key": str(uuid.uuid4()),  # new UUID per logical request
    },
    json={
        "engine": "mistral-mistral-ocr-4-1",
        "input": {"type": "url", "url": "https://example.com/receipt.png"},
    },
)
resp.raise_for_status()  # an error envelope has no "status" field
result = resp.json()
if result["status"] != "succeeded":
    raise RuntimeError(f"OCR ended as {result['status']}: {result.get('provider_error')}")
print(result["extracted_text"])
typescript
const resp = await fetch("https://api.open-ocr.com/v1/ocr", {
  method: "POST",
  headers: {
    Authorization: "Bearer sk-ocr-your-api-key",
    "Content-Type": "application/json",
    "Idempotency-Key": crypto.randomUUID(), // new UUID per logical request
  },
  body: JSON.stringify({
    engine: "mistral-mistral-ocr-4-1",
    input: { type: "url", url: "https://example.com/receipt.png" },
  }),
});
const result = await resp.json();
if (result.status !== "succeeded") throw new Error("OCR ended as " + (result.status ?? result.error?.code)); // error envelopes have no status
console.log(result.extracted_text);

6. 查询使用历史(/api/usage)

如需获取控制台风格的使用历史(支持分页和日期筛选),请使用 GET /api/usage。请使用 Authorization 请求头进行身份验证。 将 api_key 作为查询参数传递仅作为已弃用的后备方式受支持。

bash
# Preferred — the API key alone identifies the caller
curl "https://open-ocr.com/api/usage?page=1&per_page=50"   -H "Authorization: Bearer sk-ocr-your-api-key"

# Deprecated fallback (migration only; avoid in new clients)
# curl "https://open-ocr.com/api/usage?api_key=sk-ocr-your-api-key"

迁移说明:请将现有客户端从查询参数认证迁移到请求头认证,以避免 API 密钥泄露在 URL、日志、分析引荐来源和浏览器历史记录中。

7. 探索引擎

OpenOCR 提供多种 OCR 引擎,每种都针对不同的使用场景进行了优化。查看我们的引擎目录,或阅读对比工具以找到最适合的引擎。

bash
# List available engines
curl https://api.open-ocr.com/v1/engines \
  -H "Authorization: Bearer YOUR_API_KEY"

# Quick guide:
# mistral-mistral-ocr-4-1       — Recommended default: general documents, scans, tables
# google-gemini-3.1-flash-lite  — Cheapest acceptable quality
# local-tesseract               — OpenOCR-hosted, clean printed text
# local-easyocr                 — OpenOCR-hosted, multi-language
# local-doctr                   — OpenOCR-hosted, complex layouts
# openocr-ocrmypdf              — OpenOCR-hosted, scanned PDFs with auto-deskew
# openocr-rapidocr              — OpenOCR-hosted, lightweight

8. 速率限制

已认证的 POST OCR 响应中包含速率限制相关的请求头(结果轮询和 GET /v1/engines 不含):

text
X-RateLimit-Limit: 300       # requests per sliding 60 seconds, per API key
X-RateLimit-Remaining: 299  # requests left in the window
X-RateLimit-Reset: 60       # the window length; on a 429, seconds until a slot frees

默认限制为每个 API 密钥每分钟 300 次请求。超过限制时,你会收到 429 响应。