FastAPI
Validate documents with AI in FastAPI
UploadFile and httpx.AsyncClient; same pattern to verify webhooks.
Install
pip install fastapi httpx python-multipart
The safe pattern
The browser or app never sees your secret key. The file goes to your server, your server calls Constaia and returns the verdict.
main.py
import json
import os
import httpx
from fastapi import FastAPI, UploadFile
app = FastAPI()
API = "https://api.constaia.com/v1/analyze"
@app.post("/api/verify")
async def verify(file: UploadFile):
options = {"expect": "es_dni", "checks": {"not_expired": True}}
async with httpx.AsyncClient(timeout=60) as client:
res = await client.post(
API,
headers={"Authorization": f"Bearer {os.environ['CONSTAIA_API_KEY']}"},
files={"file": (file.filename, await file.read(), file.content_type)},
data={"options": json.dumps(options)},
)
res.raise_for_status()
return {"status": res.json()["verdict"]["status"]}One call. An answer you can act on.
- 01
Send the document and what you expect
Photo, screenshot or PDF. Set the expected type and the rules: not expired, under 6 months old, in this person's name.
- 02
We read, classify and check
OCR, AI classification and deterministic validators: PDF417 barcodes, MRZ, ABA routing numbers, dates and totals.
- 03
Get the verdict and delete the file
valid, invalid or review with reasons in your language. By default the file is deleted when the analysis ends.
And for the record…
Create your account, copy the test key and validate your first document in five minutes.