FastAPI proxy
Proxy attribu.tech analytics through your FastAPI server.
This is a server-specific guide. See Proxy the tracking script for an overview of why and how proxying works.
1. Install dependencies
pip install fastapi uvicorn httpx2. Add proxy routes
Create a FastAPI app with two routes: one to serve the tracking script and one to forward events to attribu.tech.
from fastapi import FastAPI, Request
from fastapi.responses import Response
import httpx
app = FastAPI()
ATTRIBU_BASE = "https://attribu.tech"
@app.get("/js/attribu.js")
async def proxy_script():
async with httpx.AsyncClient() as client:
resp = await client.get(f"{ATTRIBU_BASE}/js/script.js")
return Response(
content=resp.content,
status_code=resp.status_code,
media_type="application/javascript",
headers={"Cache-Control": "public, max-age=86400"},
)
@app.post("/api/proxy/events")
async def proxy_events(request: Request):
body = await request.body()
headers = {
"Content-Type": request.headers.get("content-type", "application/json"),
"User-Agent": request.headers.get("user-agent", ""),
"X-Forwarded-For": request.client.host,
}
origin = request.headers.get("origin")
if origin:
headers["Origin"] = origin
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{ATTRIBU_BASE}/api/events",
content=body,
headers=headers,
)
return Response(
content=resp.content,
status_code=resp.status_code,
media_type="application/json",
)The events route is mounted at
/api/proxy/events instead of /api/events to avoid conflicts with your own API routes. The script tag uses data-api-url to point to the proxied path.3. Update your script tag
Point the script src to your proxied path and set data-api-url to your proxied API endpoint:
<script
defer
data-website-id="YOUR_SITE_ID"
data-domain="yourdomain.com"
data-api-url="https://yourdomain.com/api/proxy/events"
src="/js/attribu.js"
></script>Replace
YOUR_SITE_ID with your actual Website ID from attribu.tech. Replace yourdomain.com with your website's root domain.4. Run the server
uvicorn main:app --host 0.0.0.0 --port 8000Verify installation
- Visit your site in an incognito/private window.
- Open your browser's Developer Tools and go to the Network tab.
- Confirm the script loads from
/js/attribu.json your domain. - Confirm the pageview POST goes to
/api/proxy/eventson your domain. - Check your attribu.tech dashboard. A new pageview should appear within a few seconds.
Troubleshooting
- 502 Bad Gateway - Make sure your server can reach
attribu.techover HTTPS. Check DNS and firewall rules. - Script loads but no pageviews - Verify the
/api/proxy/eventsroute is handling POST requests. Check thatrequest.client.hostresolves correctly behind your reverse proxy. - CORS errors - The proxy should eliminate CORS issues since both the script and API are served from your domain. If you still see errors, make sure the proxy is not adding extra CORS headers that conflict.
What's next
- Back to proxy overview
- Nginx proxy setup
- Connect Stripe to attribute revenue to traffic sources.