xtract.bot

POST /api/document-prepare-for-ocr2 quota units per call · cache hits free

Prepare a document scan for OCR: deskew, binarize, denoise, contrast-stretch. Improves recognition rates on noisy phone-camera captures and skewed scans.

Cleans up a document image so the OCR engine has the best possible chance. Apply this before `document-ocr` for noisy inputs — phone-camera captures, photographed receipts, faxed pages. The pipeline is: - Deskew (auto-detect rotation up to ±15° and straighten). - Convert to greyscale. - Adaptive threshold to clean black-on-white binary. - Despeckle (remove isolated stray pixels). Output is a PNG ready to feed straight into `document-ocr`.

Inputs

NameTypeDefaultDescription
image*fileInput document image.
deskewbooleantrueRotate to correct skew.
maxAnglenumber (0…90)15Cap on detected skew magnitude (degrees).
thresholdModeenum (otsu | adaptive)"otsu"Binarisation strategy.
denoisebooleantrueLight Gaussian blur before threshold.

Response

Modes: binary, stream, json. Cache: yes (24h TTL).

Large outputs stream: responses over the 4 MiB cache limit are piped through as they are produced instead of buffered. They carry x-cache-skip: size and are never cached — identical follow-up calls recompute.

Every response carries x-cache (HIT / MISS / BYPASS), x-cache-signature (stable across identical inputs), and the rate-limit headers listed below.

Quota & limits

Cost per call2 units against the 10,000-unit monthly quota. Cache hits are free — only cache misses decrement.
Burst limit200 requests per rolling minute, shared across all tools.
Max request size25 MiB (base64 inflates file payloads ~33% — the limit applies to the decoded bytes).
Max response size25 MiB
Timeout15s wall clock.
Rate-limit headersX-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Burst-Limit on every response; X-Quota-Warning once 80% of the monthly quota is spent; Retry-After on 429s.

Errors

Errors are JSON with an error string; schema failures add a details object (Zod's flattened field errors).

StatusWhenBody
400Input fails the tool's schema (wrong type, missing required field, malformed base64) or the file bytes can't be parsed as the expected format.{ "error": "invalid inputs", "details": { "fieldErrors": { … } } }
401Missing or invalid X-Api-Key / X-Account-Id headers (or no session when called from the browser).{ "error": "unauthorized" }
404Unknown tool id, or calling /api/* on the website hostname instead of api.xtract.bot.{ "error": "not found" }
413Request body over the tool's max request size, or an image header declaring more megapixels than the tool's pixel budget.{ "error": "…exceeds maxRequestBytes…" }
415Content-Type isn't application/json on the json-body transport.{ "error": "unsupported content-type; expected application/json" }
429Monthly quota or the 200/min burst limit exhausted. Check Retry-After and the X-RateLimit-* headers.{ "error": "monthly quota exceeded", "monthRemaining": 0, … }
5xxConversion engine failure. Safe to retry; if it persists, the input is hitting a bug — please report it.{ "error": "…" }

Code samples

Built from the invoice example.

# Download or substitute the example input:
#   curl -O https://xtract.bot/examples/ocr-fixtures/document-small.png
IMAGE=$(base64 -w0 < document-small.png)

curl -X POST https://api.xtract.bot/api/document-prepare-for-ocr \
  -H "Content-Type: application/json" \
  -H "Accept: application/octet-stream" \
  -H "X-Account-Id: $XTRACT_ACCOUNT_ID" \
  -H "X-Api-Key: $XTRACT_API_KEY" \
  -d '{
  "image": "'"$IMAGE"'"
}'