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

# Web analytics relay recipes

> Relay setups for Next.js, Express, Cloudflare Workers and Vercel, how to choose the visitor address header, and the relay's limits.

These recipes use the relay helpers in `@getlurk/web`. Hosted collection is coming soon.

The relay is a small route on your own domain. The script posts to it, and it calls Lurk with your project key. [Install web analytics](https://docs.getlurk.com/web/install.md) shows the first setup. This page shows more, and what to watch for.

Every recipe needs two things:

* the project key in your server's environment, never in the page;
* `ipHeader`, the name of a header that your host sets and overwrites. There is no default and no fallback.

## Next.js

In `app/api/lurk/[...lurk]/route.ts`:

```ts theme={null}
import { lurkRoute } from "@getlurk/web/next";

export const { GET, POST } = lurkRoute({
  projectKey: process.env.LURK_KEY ?? "",
  ipHeader: "x-forwarded-for",
  forwardedHops: 1,
});
```

## Express

Mount the relay before any body parser such as `express.json()`. The relay reads the raw body itself to hold it to its size limit. A body that something else already read is answered 502.

```ts theme={null}
import express from "express";
import { lurkMiddleware } from "@getlurk/web/express";

const app = express();
app.use(
  "/api/lurk",
  lurkMiddleware({
    projectKey: process.env.LURK_KEY ?? "",
    ipHeader: "x-forwarded-for",
    forwardedHops: 1,
  }),
);
app.use(express.json());
```

## Cloudflare Workers

```ts theme={null}
import { type RelayHandler } from "@getlurk/web/relay";
import { lurkHandler } from "@getlurk/web/workers";

interface Env {
  LURK_KEY: string;
}

let relay: RelayHandler | undefined;

export default {
  fetch(request: Request, env: Env): Promise<Response> | Response {
    if (!new URL(request.url).pathname.startsWith("/api/lurk/"))
      return new Response("Not found", { status: 404 });
    relay ??= lurkHandler({
      projectKey: env.LURK_KEY,
      ipHeader: "cf-connecting-ip",
      countryHeader: "cf-ipcountry",
    });
    return relay(request);
  },
};
```

## Any other host

`createRelay` from `@getlurk/web/relay` returns a function from a `Request` to a `Response`. Use it wherever those two exist.

## Choose the address header

The relay reads the visitor's address only from the header you name. It answers 400 when the header is missing or does not hold an address. A header a visitor can set, or a wrong `forwardedHops`, lets a visitor fake addresses, and the relay cannot tell.

| Host | `ipHeader` | Country header |
| - | - | - |
| Cloudflare | `cf-connecting-ip` | `cf-ipcountry` |
| Vercel | `x-real-ip` | `x-vercel-ip-country` |
| A proxy you run | The header it sets and overwrites | Your own |

* With `x-forwarded-for`, set `forwardedHops` to the number of proxies you run. The relay counts that many entries from the right.
* Vercel replaces `x-real-ip` and `x-forwarded-for`. This holds when Vercel is the first hop. With your own proxy in front, use the header your outermost trusted proxy sets.
* Without `countryHeader`, the country is unknown.

## What the relay does

* It serves `script.js` and `auto.js` itself, and handles any path that ends in `/script.js`, `/auto.js` or `/collect`. Mount it only under your own endpoint.
* It needs an `Origin` header that is your site. Set `allowedOrigins` when a proxy rewrites `Host`.
* A request with `Sec-GPC: 1` or `DNT: 1` is answered 204 and nothing goes to Lurk.
* Cookies, other headers and the query string are not forwarded. After consent, in mode `full`, the relay reads one cookie of its own, `lurk_vid`, and sends its value to Lurk as the visitor id.
* It never logs. Pass `onError` to get an error code for your own logs.
* Lurk's errors and your key are never passed to the browser.

## Limits

| What | Limit |
| - | - |
| A request to the relay | 48 KiB |
| Time the relay waits for Lurk | 3,000 milliseconds by default, set with `timeoutMs` |
| Calls a minute for each project key | 600 |

A request over the size limit gets 413. A wrong method gets 405, and so does `HEAD`. A missing or foreign `Origin` gets 403.

## Other options

* `mode: "off"` sends nothing for a visitor. Pass a function of the country to switch collection off for some countries.
* `sites` limits the relay to site ids you list. Any other gets 403.
* `apiUrl` must be `https`, except for a Lurk on the same machine.


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