Documentation / Integrations · Python

Python: an async relay with a timeout.

FastAPI gives you the request headers and a clean async client. Keep the secret in the environment, score with a short timeout, and return a decision your policy can act on.

Last updated

The relay

main.pyFastAPI + httpx
import os
import httpx
from fastapi import APIRouter, Request

router = APIRouter()
SCORE_URL = 'https://app.volance.com/api/trace/score'
SESSION_URL = 'https://app.volance.com/api/trace/session'

@router.post('/api/human-check')
async def human_check(request: Request):
    evidence = await request.json()
    headers = request.headers
    forwarded = headers.get('x-forwarded-for', '')

    async with httpx.AsyncClient(timeout=0.8) as client:
        session = (await client.get(
            SESSION_URL,
            headers={'Authorization': 'Bearer ' + os.environ['VOLANCE_PUBLIC_KEY']},
        )).json()

        result = (await client.post(
            SCORE_URL,
            headers={'Authorization': 'Bearer ' + os.environ['VOLANCE_SECRET_KEY']},
            json={
                'publicKey': os.environ['VOLANCE_PUBLIC_KEY'],
                'sid': session['sessionId'],
                'manifest': session['manifest'],
                'page': evidence.get('page', '/'),
                'clientSignals': evidence.get('clientSignals', {}),
                'events': evidence.get('events', []),
                'requestSignals': {
                    'visitorIp': forwarded.split(',')[0].strip() or None,
                    'cookiePresent': 'cookie' in headers,
                    'visitorUserAgent': headers.get('user-agent'),
                    'acceptLanguage': headers.get('accept-language'),
                },
            },
        )).json()

    return {
        'action': result['action'],
        'score': result['score'],
        'requestId': result.get('requestId'),
    }

Environment

.envServer-only
VOLANCE_PUBLIC_KEY=pk_...
VOLANCE_SECRET_KEY=sk_...

What to pass

The relay describes the visitor; the scoring call’s own headers describe the relay.
FieldSourceNote
visitorIpThe first x-forwarded-for hopTrust it only if your proxy sets it.
cookiePresentWhether a cookie came backBoolean; no cookie value.
visitorUserAgentThe request user agentThe visitor’s, not the relay’s.
acceptLanguageThe request headerBrowsers always send one; curl often does not.
headerOrderYour edge, if it exposes itOmit rather than invent.

Failure handling

The scoring call is synchronous. A short timeout (under a second) keeps a vendor slowdown from becoming your outage. Decide per route: fail open on a read, step up or fail closed on a spend. A 4xx or 429 is not a billable score; a retry is another call, so cap it.

Verify it

  • Run the route against a consented test session and match the requestId in the portal.
  • Confirm the secret is in the process environment, not a settings module that ships to the client.
  • Check the response time in your access log before you enforce.

Related

Free to start. 10,000 scores a month, one project, no credit card.

Get started free