Parse the receipt, auto-categorize when confidence clears your threshold, queue the rest for human approval, and fan out to the accounting system the customer chose.
expense/process_receipt.pypython
import os
import json
import hmac
import hashlib
import httpx
API = "https://eu-api.tryparsr.dev"
KEY = os.environ["PARSR_KEY"] # sk_eu_live_…
ACCOUNTING_WEBHOOK = os.environ["ACCOUNTING_WEBHOOK"]
WEBHOOK_SECRET = os.environ["WEBHOOK_SECRET"].encode()
AUTO_TAG_THRESHOLD = 0.85
async def process_receipt(image_bytes: bytes, expense_id: str) -> dict:
async with httpx.AsyncClient(timeout=30) as client:
resp = await client.post(
f"{API}/v1/parse/receipt?wait=30",
headers={
"Authorization": f"Bearer {KEY}",
"Idempotency-Key": expense_id,
},
files={"file": ("receipt.jpg", image_bytes, "image/jpeg")},
)
resp.raise_for_status()
receipt = resp.json()
# 1. Auto-tag if the parse is confident enough
confidence = min(
receipt["fields"]["merchant_name"]["confidence"],
receipt["fields"]["total"]["confidence"],
)
auto_tagged = confidence >= AUTO_TAG_THRESHOLD
payload = {
"expense_id": expense_id,
"merchant": receipt["fields"]["merchant_name"]["value"],
"total": receipt["fields"]["total"]["value"],
"currency": receipt["fields"]["currency"]["value"],
"fx_rate": receipt["fields"].get("detected_fx_rate", {}).get("value"),
"category_hint": receipt["fields"]["category_hint"]["value"],
"auto_tagged": auto_tagged,
"needs_review": not auto_tagged,
"duplicate_hint": receipt.get("duplicate_hint", False),
}
# 2. Fan out to the customer's accounting system
body = json.dumps(payload).encode()
sig = hmac.new(WEBHOOK_SECRET, body, hashlib.sha256).hexdigest()
async with httpx.AsyncClient(timeout=10) as client:
await client.post(
ACCOUNTING_WEBHOOK,
content=body,
headers={
"Content-Type": "application/json",
"X-Signature": f"sha256={sig}",
},
)
return payload