SAGEA LogoDocs & API
CookbooksOCR Cookbooks

Build a Receipt Expense Pipeline

Turn a folder of receipts into a deduped expense report with merchant, date, and total.

  • OCR every receipt to JSON with ARVA OCR
  • Parse merchant, date, and total with Python patterns
  • Dedupe repeat scans by file hash

Time to complete: ~15 minutes

Prerequisites

  • A SAGEA_API_KEY exported as an environment variable
  • Receipt images in PDF, PNG, JPG, TIFF, or WEBP format, max 100MB, ideally 300 DPI
  • Python 3.9+ with the requests package installed
  • Set up document processing
  • ARVA OCR model card

Step 1: OCR each receipt to JSON

Loop over a folder and post every file to the OCR endpoint.

curl -X POST https://api.sagea.space/v1/ocr/process \
  -H "Authorization: Bearer $SAGEA_API_KEY" \
  -F model="arva-ocr" \
  -F [email protected] \
  -F output_format="json"

Each response contains text, pages, confidence, blocks, and model_used. Accuracy is 99.2% on clean scans across 150+ languages.

Step 2: Parse merchant, date, and total

Extract the three expense fields from OCR text with regular expressions.

curl -X POST https://api.sagea.space/v1/ocr/process \
  -H "Authorization: Bearer $SAGEA_API_KEY" \
  -F model="arva-ocr" \
  -F [email protected] \
  -F output_format="text"

Step 3: Dedupe by hash and export CSV

Hash file bytes to drop duplicate scans, then write the expense report.

import csv
import hashlib
 
seen, rows = set(), []
for path in receipts:
    digest = hashlib.sha256(open(path, "rb").read()).hexdigest()
    if digest in seen:
        print(f"skip duplicate: {path}")
        continue
    seen.add(digest)
    data = ocr_receipt(path)
    parsed = parse_receipt(data["text"])
    rows.append({
        "file": path,
        "hash": digest[:12],
        "confidence": data["confidence"],
        **parsed,
    })
 
with open("expenses.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=["file", "hash", "merchant", "date", "total", "confidence"])
    writer.writeheader()
    writer.writerows(rows)
 
print(f"wrote {len(rows)} unique expenses to expenses.csv")

Standard pages cost $1.00 per 1000 pages, so a thousand receipts cost about one dollar.

Verify

ErrorCauseFix
401 UnauthorizedMissing or invalid API keyExport a valid key as SAGEA_API_KEY and retry
400 unsupported_formatFile type not in PDF, PNG, JPG, TIFF, WEBPConvert the receipt to JPG or PDF and retry
413 Payload Too LargeFile is over 100MBCompress the image and retry
429 rate_limit_exceededToo many requests at onceAdd a short sleep between receipts and retry

What's next

On this page