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
| Field | Source | Note |
|---|---|---|
visitorIp | The first x-forwarded-for hop | Trust it only if your proxy sets it. |
cookiePresent | Whether a cookie came back | Boolean; no cookie value. |
visitorUserAgent | The request user agent | The visitor’s, not the relay’s. |
acceptLanguage | The request header | Browsers always send one; curl often does not. |
headerOrder | Your edge, if it exposes it | Omit 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
requestIdin 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
- Integrations — the generic relay
- Score endpoint — all request fields
- Operations — timeouts and logging
- Troubleshooting — when the relay fails