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

# Netlify

> Integrate Siteline with Netlify Edge Functions

Use a [Netlify Edge Function](https://docs.netlify.com/edge-functions/overview/) to intercept requests at the edge and forward tracking data to Siteline using the `@siteline/core` SDK. The edge function proxies the request transparently, so there is zero impact on site performance.

## Prerequisites

* A [Siteline website key](/authorization)
* A Netlify site with [Edge Functions](https://docs.netlify.com/edge-functions/overview/) enabled

<Info>
  Netlify Edge Functions are available on all plans, including the **free tier**. See [Netlify Edge Functions](https://docs.netlify.com/edge-functions/overview/) for details.
</Info>

## Setup

<Steps>
  <Step title="Add the edge function">
    Create an `edge-functions` directory in the root of your Netlify project and copy [`siteline.ts`](https://github.com/siteline-ai/siteline-js-sdk/blob/main/examples/netlify/edge-functions/siteline.ts) into it:

    ```
    your-project/
    ├── edge-functions/
    │   └── siteline.ts
    └── netlify.toml
    ```

    The function imports `@siteline/core` via an ESM URL (`https://esm.sh/@siteline/core@1.0.9`) — no `npm install` is needed since Netlify Edge Functions run on Deno.
  </Step>

  <Step title="Set your website key">
    Open `edge-functions/siteline.ts` and set the `SITELINE_WEBSITE_KEY` constant at the top of the file:

    ```typescript theme={null}
    const SITELINE_WEBSITE_KEY = 'secret_key_...';
    ```

    Optionally configure `SITELINE_ENDPOINT` and `SITELINE_DEBUG`.
  </Step>

  <Step title="Configure Netlify routes">
    Create or update `netlify.toml` in your project root:

    ```toml netlify.toml theme={null}
    [build]
      edge_functions = "edge-functions"

    [[edge_functions]]
      path = "/*"
      function = "siteline"
    ```

    This routes every incoming request through the Siteline edge function.
  </Step>

  <Step title="Deploy">
    Deploy your site as usual. The edge function will begin tracking requests immediately.
  </Step>
</Steps>

## How it works

The edge function runs as middleware on every matched request:

1. A visitor (or AI bot) makes a request to your Netlify site
2. The edge function calls `context.next()` to forward the request to your origin
3. Tracking data (`url`, `method`, `status`, `duration`, `userAgent`, `ref`, `ip`, `acceptHeader`) is sent to Siteline asynchronously via fire-and-forget
4. The response is returned to the visitor unmodified

If the downstream request fails, the function still tracks the request with status `500` before re-throwing the error. The SDK initializes lazily on the first request and reuses the instance for subsequent calls.

View the full source on [GitHub](https://github.com/siteline-ai/siteline-js-sdk/blob/main/examples/netlify/edge-functions/siteline.ts).

## Configuration

<ResponseField name="SITELINE_WEBSITE_KEY" type="string" required>
  Your Siteline website key. If empty, tracking is disabled and a warning is logged to the console.
</ResponseField>

<ResponseField name="SITELINE_ENDPOINT" type="string">
  Siteline intake endpoint. Defaults to `https://api.siteline.ai/v1/intake/pageview`.
</ResponseField>

<ResponseField name="SITELINE_DEBUG" type="boolean">
  Enables debug logging. When `true`, errors in the edge function are logged to the console.
</ResponseField>

## Troubleshooting

<AccordionGroup>
  <Accordion title="No visits appearing in Siteline">
    Verify `SITELINE_WEBSITE_KEY` is set to a valid key in `edge-functions/siteline.ts`. Confirm `netlify.toml` routes `/*` to `function = "siteline"`.
  </Accordion>

  <Accordion title="Edge function does not run on requests">
    Ensure `[build] edge_functions = "edge-functions"` is present in `netlify.toml` and that the file exists at `edge-functions/siteline.ts`.
  </Accordion>

  <Accordion title="Will this slow down my site?">
    No. Tracking is fire-and-forget — the tracking call is not awaited and never blocks the response.
  </Accordion>

  <Accordion title="Do I need to install any npm packages?">
    No. The edge function imports `@siteline/core` via an ESM URL, which Netlify's Deno-based runtime resolves automatically.
  </Accordion>
</AccordionGroup>

<Tip>
  **Need help?** [Book a setup call](https://cal.com/team/siteline/tech-set-up) and we'll walk you through it.
</Tip>
