Developers

Code examples

Upload and retrieve in cURL, Python and TypeScript. Keys and data below are synthetic.

cURL

# 1. upload
curl -X POST https://api.mahadocr.com/v1/documents/upload \
  -H "X-MahadDoc-API-Key: mk_live_YOUR_KEY" \
  -F "[email protected]"

# 2. retrieve
curl https://api.mahadocr.com/v1/documents/DOC_831d6b45e154 \
  -H "X-MahadDoc-API-Key: mk_live_YOUR_KEY"

Python

import os, time, requests

API = "https://api.mahadocr.com/v1"
KEY = os.environ["MAHAD_OCR_API_KEY"]      # never hardcode the key
H   = {"X-MahadDoc-API-Key": KEY}

def read_document(path: str) -> dict:
    with open(path, "rb") as fh:
        r = requests.post(f"{API}/documents/upload", headers=H,
                          files={"file": fh}, timeout=30)
    r.raise_for_status()
    doc_id = r.json()["id"]

    for _ in range(30):                     # poll until processing finishes
        doc = requests.get(f"{API}/documents/{doc_id}", headers=H, timeout=15).json()
        if doc["status"] != "processing":
            return doc
        time.sleep(2)
    raise TimeoutError("document still processing")

doc = read_document("passport.jpg")
print(doc["status"], doc["result"]["decision"], doc["result"]["values"])

TypeScript

const API = "https://api.mahadocr.com/v1";
const KEY = process.env.MAHAD_OCR_API_KEY!;   // server-side only

export async function readDocument(file: Blob): Promise<any> {
  const form = new FormData();
  form.append("file", file);

  const up = await fetch(`${API}/documents/upload`, {
    method: "POST",
    headers: { "X-MahadDoc-API-Key": KEY },
    body: form,
  });
  if (!up.ok) throw new Error(`upload failed: ${up.status}`);
  const { id } = await up.json();

  for (let i = 0; i < 30; i++) {
    const res = await fetch(`${API}/documents/${id}`, {
      headers: { "X-MahadDoc-API-Key": KEY },
    });
    const doc = await res.json();
    if (doc.status !== "processing") return doc;
    await new Promise((r) => setTimeout(r, 2000));
  }
  throw new Error("document still processing");
}