Skip to content
dhanu docs
Browse pages

API reference

Extract text with OCR

POST /api/pub/v1/ocr, read the text out of an image or PDF as a job.

POSThttps://api.dhanu.ai/api/pub/v1/ocr

Queues OCR for an image or PDF sent as base64 and returns the job. OCR runs on a separate service and can take a while for long PDFs, so the text arrives in the job’s result once it succeeds, wait on it or subscribe to job.succeeded. A test key answers with an already succeeded fixture job.

Request

FieldTypeRequiredNotes
datastringYesThe file bytes, base64-encoded. At least 1 characters.
mimeTypestringYesAt least 1 characters.
filenamestringNoUp to 255 characters.
languageHintstringNoThe document's language: ne, en, or auto to decide from the filename. One of ne, en, auto.
curl -X POST https://api.dhanu.ai/api/pub/v1/ocr \
  -H "Authorization: Bearer $DHANU_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{ \"data\": \"$(base64 -i receipt.png)\", \"mimeType\": \"image/png\", \"filename\": \"receipt.png\" }"
const job = await dhanu.ocr.extract({
  data: await fs.readFile("receipt.png"),
  mimeType: "image/png",
  filename: "receipt.png",
});
const done = await dhanu.jobs.wait(job.id);
console.log(done.result);
with open("receipt.png", "rb") as fh:
    job = dhanu.ocr.extract(data=fh.read(), mime_type="image/png", filename="receipt.png")
done = dhanu.jobs.wait(job.id)
print(done.result)

Response

202 Accepted

{ "job": { "id": "…", "type": "ocr.extract", "status": "queued", "result": null, "error": null, "createdAt": "…", "completedAt": null } }

Errors

CodeStatusMeaning
invalid_api_key401The bearer key is missing or unknown.
api_key_revoked401The key was revoked.
api_key_expired401The key passed its expiry date.
api_key_ip_not_allowed403The caller's IP is outside the key's allowlist.
developer_access_revoked403The key's creator lost developer access in the workspace.
api_key_project_scope403The key covers no active projects, or projectId is outside its scope.
invalid_request_body422The body did not match the schema; error.issues names each field.
project_id_required422A multi-project key called without projectId.
insufficient_credits402The workspace credit balance is zero or below.
key_spend_limit_reached402The key reached its monthly spend cap.
invalid_generation_prompt422The prompt was empty or longer than 2000 characters.