One firm, many clients, three document types. Each parse carries the client's org_id so audit records, billing, and retention policies stay isolated per-client without your code thinking about it.
firm/dispatcher.pypython
import os
import asyncio
import httpx
API = "https://eu-api.tryparsr.dev"
KEY = os.environ["PARSR_KEY"] # sk_eu_live_…
ENDPOINT = {
"bank_statement": f"{API}/v1/parse/bank-statement",
"invoice": f"{API}/v1/parse/invoice",
"receipt": f"{API}/v1/parse/receipt",
}
POST_THRESHOLD = 0.85 # post straight to the ledger above this
VAT_THRESHOLD = 0.95 # VAT-bearing fields get a stricter bar
async def dispatch(client_id: str, doc_type: str, file_path: str) -> dict:
"""Parse a single client document. client_id is your firm's internal id;
we pass it as parsr's org_id so audit, billing, and retention scope
stay isolated per-client."""
url = ENDPOINT[doc_type]
async with httpx.AsyncClient(timeout=60) as http:
with open(file_path, "rb") as f:
resp = await http.post(
f"{url}?wait=60",
headers={
"Authorization": f"Bearer {KEY}",
"Idempotency-Key": f"{client_id}:{file_path}",
},
files={"file": f},
data={"metadata": f'{{"org_id":"{client_id}"}}'},
)
resp.raise_for_status()
doc = resp.json()
# Bank statements: balance-chain verdict gates auto-post
if doc_type == "bank_statement":
chain = doc["validation"]["balance_chain"]
if not chain["valid"]:
return {"action": "review", "reason": "balance_chain_failed",
"client_id": client_id, "doc": doc}
# Confidence-gated routing into the bookkeeping ledger
min_conf = min(f["confidence"] for f in doc["fields"].values())
has_vat = any("vat" in name for name in doc["fields"])
bar = VAT_THRESHOLD if has_vat else POST_THRESHOLD
return {
"action": "post" if min_conf >= bar else "review",
"client_id": client_id,
"schema_version": doc["schema_version"],
"doc": doc,
}
async def run_batch(client_id: str, files: list[tuple[str, str]]) -> list[dict]:
"""Process a client's monthly drop in parallel, scoped to their org_id."""
return await asyncio.gather(*(
dispatch(client_id, doc_type, path) for doc_type, path in files
))