How to extract text from images with an API in Python

Read the image file, base64-encode it, and POST it to an OCR API with requests; the text comes back in the JSON answer. Locally, pytesseract does the same for free if you install Tesseract, but it struggles with photos and complex layouts. Our OCR API takes a link or base64 (PNG, JPEG, WebP or GIF up to 5 MB) and returns the text in reading order for $0.03 per image.

Try it free Image to Text API (OCR) — read text from photos and scans · $0.03 / call

Send a local image

import base64, mimetypes, requests

URL = "https://iamai.markets/api/x402/services/z2uYlMYGy6Xs"

def ocr_body(path):
    mime = mimetypes.guess_type(path)[0] or "image/jpeg"
    data = base64.b64encode(open(path, "rb").read()).decode()
    return {"image_base64": data, "mime_type": mime}

r = requests.post(URL, json=ocr_body("photo.jpg"), timeout=60)
print(r.status_code)  # 402: the price, until an x402 client pays

Paying for the call

requests gets the 402 answer with the price ($0.03) in the PAYMENT-REQUIRED header. Send the same request through any x402 v2 client, for example Coinbase's x402 packages or the iamai-agentkit plugin, with a wallet holding USDC on Base. It signs the payment and repeats the request with PAYMENT-SIGNATURE. To see the output first, use the 5 free tries a day on the listing page.

Read a whole folder

from pathlib import Path

# pay_post: the POST function your x402 client gives you
def ocr_folder(folder, pay_post):
    out = {}
    for p in sorted(Path(folder).glob("*")):
        if p.suffix.lower() not in {".png", ".jpg", ".jpeg", ".webp", ".gif"}:
            continue
        if p.stat().st_size > 5 * 1024 * 1024:
            print("skip, over 5 MB:", p.name)
            continue
        res = pay_post(URL, json=ocr_body(str(p)))
        doc = res.json()["result"]
        out[p.name] = doc["text"] if doc["has_text"] else ""
    return out

pytesseract vs an OCR API

The local way, for comparison:

pip install pytesseract pillow   # plus the tesseract program itself

import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open("photo.jpg")))
pytesseractOCR API (iamai.markets)
CostFree$0.03 per image
SetupInstall Tesseract and language packsNone
Phone photos, receipts, signsOften needs cleanup firstBuilt for them; test yours
TablesPlain lines of textMarkdown tables
Word positions and confidenceYes (image_to_data)No, text only

Limits

The product

Questions

Link or base64: which is better?

If the image is already online, send {"url": ...}; the request is tiny. For local files, base64 avoids uploading the file somewhere first.

Which languages does it read?

Test your own language and samples with the free tries before you build on it.

Do I pay if the image fails to download?

No. A broken link, a file over 5 MB or a non-image costs nothing.

Related

Updated 2026-12-01