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

# Send Netlify traffic to Searchable (Edge Function)

> Forward AI-bot traffic from your Netlify-hosted site to Searchable using a small Edge Function that runs on every request. Works on every Netlify plan, including Free.

## What this does

An Edge Function runs at Netlify's edge in front of your origin. For every inbound request it:

1. Lets the request pass through to your origin unchanged (no added latency for users)
2. After the response is returned, fires a fire-and-forget POST to Searchable with the request metadata
3. Returns the original response to the visitor

Searchable classifies the user agent server-side and records it if it matches a known AI crawler.

<Info>
  **Works on every Netlify plan, including Free.** Netlify's free plan covers 1M Edge Function invocations per month — well above typical bot traffic for most sites.
</Info>

## Prerequisites

<Check>A Netlify site with deploy access</Check>
<Check>A Searchable project with your domain confirmed</Check>

## Setup

<Steps>
  <Step title="Generate an integration token in Searchable">
    1. Open your Searchable dashboard
    2. Go to **LLM Analytics → Setup**
    3. Pick **Netlify Edge Function** as your crawler source
    4. Click **Generate token**

    Copy the token now — it starts with `sa_…` and won't be shown again. You can always generate a new one if you lose it.
  </Step>

  <Step title="Add the Edge Function file to your repo">
    Create the file `netlify/edge-functions/searchable.ts` in your repository and paste the snippet below.

    ```typescript netlify/edge-functions/searchable.ts theme={null}
    import type { Context } from "https://edge.netlify.com";

    const ENDPOINT = "https://tracker.searchableanalytics.com/v1/netlify-edge";
    const TOKEN = Netlify.env.get("SEARCHABLE_TOKEN");

    export default async function (request: Request, context: Context) {
      const response = await context.next();
      context.waitUntil(forward(request, response, context));
      return response;
    }

    async function forward(request: Request, response: Response, context: Context) {
      if (!TOKEN) return;
      try {
        const url = new URL(request.url);
        await fetch(ENDPOINT, {
          method: "POST",
          headers: { "content-type": "application/json", authorization: `Bearer ${TOKEN}` },
          body: JSON.stringify({
            type: "netlify_edge_event",
            timestamp: Date.now(),
            request_id: request.headers.get("x-nf-request-id") ?? crypto.randomUUID(),
            method: request.method,
            path: url.pathname,
            url: request.url,
            status_code: response.status,
            user_agent: request.headers.get("user-agent") ?? "",
            ip_address: context.ip ?? "",
            country: context.geo?.country?.code ?? "",
            referrer: request.headers.get("referer") ?? "",
          }),
        });
      } catch {
        /* swallow */
      }
    }

    export const config = { path: "/*" };
    ```

    The function intercepts every request, forwards a lightweight event to Searchable after the response is returned, and never blocks the user.
  </Step>

  <Step title="Add the environment variable in Netlify">
    In your Netlify site:

    **Site settings → Environment variables → Add a variable**

    | Key                | Value                                |
    | ------------------ | ------------------------------------ |
    | `SEARCHABLE_TOKEN` | The `sa_…` token you generated above |

    Make sure the variable name is exactly `SEARCHABLE_TOKEN` (all caps, underscore).
  </Step>

  <Step title="Commit and push">
    Commit the new file and push to your main branch. Netlify will auto-deploy the edge function.

    ```bash theme={null}
    git add netlify/edge-functions/searchable.ts
    git commit -m "Add Searchable edge function"
    git push
    ```
  </Step>

  <Step title="Verify the function is active">
    In Netlify, open **Site settings → Edge functions** and confirm that `searchable` appears in the list and is marked active.
  </Step>
</Steps>

## Verifying the connection

Return to **LLM Analytics → Setup** in your Searchable dashboard. The Netlify Edge Function card should flip to **Connected** within about 10 seconds of the next AI crawler hitting your site.

| Status                      | What it means                                                                                                                 |
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| **Waiting for first event** | The function is deployed but no AI bot has hit your site yet. Typical wait is a few hours for sites that are already indexed. |
| **Connected**               | Events are arriving. The card shows the count from the last 24 hours.                                                         |

## Cost

Each forwarded request consumes one Edge Function invocation. Netlify's free plan covers 1M invocations per month — bot traffic is a tiny fraction of that for typical sites.

## Troubleshooting

<AccordionGroup>
  <Accordion title="Card stays 'Not connected' after deploying">
    Check these in order:

    * The environment variable is named exactly `SEARCHABLE_TOKEN` (all caps, underscore — not `SearchableToken` or `SEARCHABLE-TOKEN`)
    * The deploy succeeded — check Netlify's deploy log for errors in the edge function
    * The function is active in **Site settings → Edge functions**
    * The `config` export at the bottom of the file sets `path: "/*"` so the function runs on all requests
  </Accordion>

  <Accordion title="I only see human traffic, not bots">
    That's expected. Searchable filters non-bot user agents server-side — only AI crawlers (GPTBot, ClaudeBot, PerplexityBot, etc.) are recorded in your dashboard. Human visitors are forwarded but immediately discarded.
  </Accordion>

  <Accordion title="Netlify logs show errors in the edge function">
    If the function throws during `context.next()`, Netlify may surface it in deploy logs. The `forward()` call is wrapped in a `try/catch` and uses `context.waitUntil(...)`, so errors inside `forward` are silently swallowed and never affect your visitors.

    Confirm the function file is valid TypeScript and that the `import type { Context }` line is present — Netlify's Deno runtime requires it.
  </Accordion>
</AccordionGroup>

## Removing the integration

1. Delete `netlify/edge-functions/searchable.ts` from your repo and push
2. Netlify → **Site settings → Environment variables** → remove `SEARCHABLE_TOKEN`
3. Searchable → **LLM Analytics → Setup → Tokens** → revoke the token

## Next steps

<CardGroup cols={2}>
  <Card title="See the data" icon="chart-line" href="/using-searchable/visibility-tracking">
    Open LLM Analytics to see which assistants are crawling your site.
  </Card>

  <Card title="Add Search Console" icon="google" href="/integrations/google-search-console">
    Correlate AI crawls with search demand.
  </Card>
</CardGroup>
