Skip to content
Constaia

FastAPI

Validate documents with AI in FastAPI

Receive the file and send it to Constaia asynchronously; the same pattern works for checking the notifications Constaia sends to your system.

Installation

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 result. If you do not have a server, use the widget with a publishable key.

Full guide
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.

  1. 01

    Send the document and what you expect

    A photo, screenshot or PDF. Say which document you expect and the rules: in date, less than 6 months old, in this person's name.

  2. 02

    We read, classify and check

    Text reading, AI classification and fixed checks: Spanish ID number check letter, machine readable zone, IBAN, dates and totals.

  3. 03

    Get the verdict and the file is deleted

    Valid, invalid or review, with the reasons in your language. By default, the file is deleted at the end unless it needs review.

And for the record…

Create your free account and validate your first document in five minutes. No card required.