> ## Documentation Index
> Fetch the complete documentation index at: https://help.trfyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Proxy Trfyx through FastAPI

> Proxy Trfyx analytics through FastAPI.

Use FastAPI to forward the Trfyx script and event requests through your application.

<Steps>
  <Step title="Install dependencies">
    ```bash Terminal theme={"dark"}
    pip install fastapi uvicorn httpx
    ```
  </Step>

  <Step title="Add proxy routes">
    ```python main.py theme={"dark"}
    from fastapi import FastAPI, Request, Response
    import httpx

    app = FastAPI()

    @app.get("/script.js")
    async def script():
        async with httpx.AsyncClient() as client:
            r = await client.get("https://app.trfyx.com/script.js")
        return Response(r.content, media_type="application/javascript")

    @app.post("/api/events")
    async def events(request: Request):
        body = await request.body()
        headers = dict(request.headers)
        headers["x-forwarded-for"] = request.headers.get("x-forwarded-for", request.client.host)

        async with httpx.AsyncClient() as client:
            r = await client.post(
                "https://app.trfyx.com/api/events",
                content=body,
                headers=headers,
            )

        return Response(r.content, status_code=r.status_code, media_type=r.headers.get("content-type"))
    ```
  </Step>

  <Step title="Update your tracker">
    ```html index.html theme={"dark"}
    <script
      defer
      data-website-id="YOUR_TRFYX_PROJECT_ID"
      data-domain="yourdomain.com"
      src="/script.js"
    ></script>
    ```
  </Step>

  <Step title="Verify installation">
    Deploy the API, load your site, and check that `/script.js` and `/api/events` resolve on your domain. Verify incoming activity in Trfyx.

    <Warning>
      If your application is behind another load balancer, configure trusted proxy handling there too. Incorrect forwarding can make every visitor appear to come from the same server.
    </Warning>
  </Step>
</Steps>

## Next steps

<CardGroup cols={2}>
  <Card title="Goals" icon="bullseye" href="/goals" />

  <Card title="Funnels" icon="filter" href="/funnels" />

  <Card title="Managed Proxy" icon="shield-halved" href="/managed-proxy" />

  <Card title="Troubleshooting" icon="life-ring" href="/troubleshooting" />
</CardGroup>


## Related topics

- [Proxy Trfyx through your domain](/proxy-guide.md)
- [Proxy Trfyx through Nginx](/nginx-proxy.md)
- [Proxy Trfyx through PHP](/php-proxy.md)
- [Proxy Trfyx through Express](/express-proxy.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.