FastAPI Webhooks
Proxy traffic to FastAPI and consume JSON webhooks securely behind your Relay gateway.
Overview
FastAPI is a high-performance Python framework perfect for building JSON APIs and handling webhooks. By placing Relay in front of your FastAPI service, Relay manages authentication, rate limiting, and observability. This allows your Python code to focus purely on business logic using standard Pydantic models.
Building the API
In a standard FastAPI() app, you can seamlessly validate incoming JSON payloads by defining Pydantic models. To ensure that incoming traffic is securely routed through your gateway (and not spoofed directly to your server), you can read the Request.headers for Relay-specific signatures, such as x-relay-signature.
Running with Uvicorn
Because Relay proxies requests to your backend, your FastAPI application runs behind a reverse proxy. To ensure FastAPI receives the correct client IP addresses and forwarded headers, you must configure Uvicorn to trust these forwarded IPs.
Use the --proxy-headers and --forwarded-allow-ips flags when starting your uvicorn server:
For production deployments, we highly recommend restricting forwarded_allow_ips to Relay's specific egress IP addresses rather than allowing all IPs (*).