> ## 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 with Astro

> Proxy Trfyx analytics through Astro server routes.

For Astro SSR or server deployments, expose same-origin routes that forward the Trfyx script and event requests.

<Steps>
  <Step title="Create the script route">
    Create `src/pages/script.js.ts`:

    ```ts src/pages/script.js.ts theme={"dark"}
    export async function GET() {
      const response = await fetch("https://app.trfyx.com/script.js");
      return new Response(await response.arrayBuffer(), {
        status: response.status,
        headers: { "Content-Type": "application/javascript" },
      });
    }
    ```
  </Step>

  <Step title="Create the event route">
    Create `src/pages/api/events.ts` and forward the request body to `https://app.trfyx.com/api/events` while preserving the visitor IP.

    ```ts src/pages/api/events.ts theme={"dark"}
    export async function POST({ request, clientAddress }) {
      const body = await request.arrayBuffer();
      const response = await fetch("https://app.trfyx.com/api/events", {
        method: "POST",
        headers: {
          "Content-Type": request.headers.get("content-type") || "application/json",
          "Origin": new URL(request.url).origin,
          "X-Forwarded-For": request.headers.get("x-forwarded-for") || clientAddress,
        },
        body,
      });

      return new Response(await response.arrayBuffer(), { status: response.status });
    }
    ```
  </Step>

  <Step title="Update the tracker">
    Replace the default Trfyx script with the proxied path:

    ```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 your Astro site and open it in a browser. Check the Network panel to confirm both `script.js` and `/api/events` requests use your domain. Then verify the visit appears in Trfyx.
  </Step>
</Steps>

<Note>
  For a fully static Astro site, use [Managed Proxy](/managed-proxy) instead of adding server routes.
</Note>

## Next steps

<CardGroup cols={2}>
  <Card title="Proxy setup" icon="signs-post" href="/proxy-guide">
    Read the full proxy setup guide
  </Card>

  <Card title="Managed Proxy" icon="shield-halved" href="/managed-proxy">
    Let Trfyx manage the proxy for you
  </Card>

  <Card title="Goals" icon="bullseye" href="/goals">
    Start tracking goals with Trfyx
  </Card>

  <Card title="Troubleshooting" icon="life-ring" href="/troubleshooting">
    Fix common proxy issues
  </Card>
</CardGroup>


## Related topics

- [Add Trfyx to your Astro project](/astro.md)
- [Proxy Trfyx through your domain](/proxy-guide.md)
- [Proxy Trfyx with Laravel](/laravel-proxy.md)
- [Proxy Trfyx with Caddy](/caddy-proxy.md)


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