# Proxy with FastAPI

> Serve the Reveata script and forward events through two FastAPI routes, with the visitor's IP and your proxy secret.

Two routes in your FastAPI app stand in for Reveata's addresses, so the browser only talks to your domain. The code uses `httpx` (`pip install httpx`), which works with async routes.

## Before you start

1. In Reveata, open your website, then **Settings → General → Tracking code**, and choose **Create proxy secret** under **Proxy secret** (admins and the owner can). Copy it: it starts with `dt_proxy_` and is shown once.
2. Set it as the environment variable `REVEATA_PROXY_SECRET` where the app runs. Never return it to the browser.

## The routes

```python
import os

import httpx
from fastapi import FastAPI, Request, Response

app = FastAPI()

UPSTREAM = "https://reveata.com"
PASS = [
    "user-agent",
    "accept-language",
    "origin",
    "referer",
    "sec-fetch-dest",
    "sec-fetch-mode",
    "sec-fetch-site",
]
client = httpx.AsyncClient(timeout=10)


@app.get("/dt/script.js", include_in_schema=False)
async def reveata_script() -> Response:
    r = await client.get(f"{UPSTREAM}/js/script.js")
    headers = {"Cache-Control": r.headers.get("cache-control", "public, max-age=86400")}
    if "etag" in r.headers:
        headers["ETag"] = r.headers["etag"]
    return Response(r.content, status_code=r.status_code, media_type="text/javascript", headers=headers)


@app.post("/dt/event", include_in_schema=False)
async def reveata_event(request: Request) -> Response:
    body = await request.body()
    if len(body) > 16 * 1024:
        return Response(status_code=413)
    headers = {
        "content-type": "text/plain",
        "x-reveata-proxy-secret": os.environ.get("REVEATA_PROXY_SECRET", ""),
        "x-reveata-client-ip": request.client.host if request.client else "",
    }
    for name in PASS:
        value = request.headers.get(name)
        if value:
            headers[name] = value
    r = await client.post(f"{UPSTREAM}/api/collect", content=body, headers=headers)
    out = {"Cache-Control": "no-store"}
    if "x-reveata-proxy" in r.headers:
        out["x-reveata-proxy"] = r.headers["x-reveata-proxy"]
    return Response(status_code=r.status_code, headers=out)
```

The event route forwards the body unchanged, with the visitor's browser headers, and never your cookies. Then point the snippet at the two routes:

```html
<script defer data-website-id="ws_your_tracking_id" data-api="/dt/event" src="/dt/script.js"></script>
```

## Behind a load balancer or reverse proxy

`request.client.host` is the address that connected to Uvicorn. Behind Nginx, a load balancer or a platform router, start Uvicorn so it reads the visitor's address from `X-Forwarded-For`, trusting only your proxy's address:

```bash
uvicorn main:app --proxy-headers --forwarded-allow-ips="10.0.0.5"
```

Use your proxy's real address (or the range your platform documents). With `"*"`, anyone who can reach Uvicorn directly can choose the address you forward.

## Check it works

Open `/dt/script.js` on your site: you should see the script. Visit a page, then open **Realtime** in Reveata: your visit should appear within seconds, in your own city. If it shows your server's location, read the `x-reveata-proxy` response header of `/dt/event` in your browser's network tab (the route passes it on), and see [Check that it works](https://reveata.com/docs/self-hosted-proxy#check-that-it-works) for what each value means.
