Developer Hub

FastAPI Integration

Use fastapi_dependency() to protect any endpoint in one line.

Install

bash
pip install certilayer fastapi

Setup

main.py
from fastapi import FastAPI, Depends
from certilayer import CertiLayerClient

app     = FastAPI()
client  = CertiLayerClient(api_key=os.environ["CERTILAYER_SECRET_KEY"])

# Create a reusable guard
guard    = client.fastapi_dependency(min_score=0.65)
strict   = client.fastapi_dependency(min_score=0.90)

# Protect routes
@app.post("/checkout", dependencies=[Depends(guard)])
async def checkout(cart: CartModel):
    return {"success": True}

@app.post("/transfer", dependencies=[Depends(strict)])
async def transfer(req: TransferModel):
    return process_transfer(req)

# Manual verification
@app.post("/login")
async def login(session_id: str = Header(alias="x-certilayer-session")):
    result = await client.verify_session(session_id)
    if result.verdict.value == "synthetic":
        raise HTTPException(403, "bot_detected")
    return issue_token()