Next.js route handler

createX3ProxyHandler from @x3growth/sdk/next/proxy implements the proxy protocol with the Fetch API only (Request in, Response out). It works in Next.js route handlers (Node and Edge runtimes) and in any framework that speaks Fetch (Astro, SvelteKit, Remix, Hono, Bun, Deno).

Setup

npm install @x3growth/sdk
.env.local
X3_API_ORIGIN={{API_ORIGIN}}
{{PROXY_KEY_ENV}}=sk_3x_YOUR_PROXY_KEY
app{{PROXY_PATH}}/[...path]/route.ts
import { createX3ProxyHandler } from "@x3growth/sdk/next/proxy";

export const { GET, POST, OPTIONS } = createX3ProxyHandler({
  apiOrigin: process.env.X3_API_ORIGIN!,
  proxyKey: process.env.{{PROXY_KEY_ENV}},
});

Point the SDK at the path – <X3Analytics host="{{PROXY_PATH}}" …/>, init({ host: "{{PROXY_PATH}}" }) or data-host="{{PROXY_PATH}}".

What the handler does:

  • forwards only /v1/e, /v1/collect, /v1/c/*, /v1/cb and /v1/s/* (everything else → 404);
  • strips cookie and authorization, forwards user-agent, origin and the body;
  • sets x-3x-client-ip by the rule of Visitor address, x-3x-client-country from x-vercel-ip-country / cf-ipcountry, and x-3x-proxy-key;
  • caches scripts (public, max-age=3600, stale-while-revalidate=86400).

Without proxyKey the handler still forwards, but the API ignores the forwarded IP – visitors behind the proxy are then merged. Always set the key in production.

If the site has middleware.ts / proxy.ts, exclude the path from its matcher, and do not name the folder with a leading underscore (private in the App Router).

Verify

curl -s -o /dev/null -w "%{http_code}\n" https://YOUR-SITE{{PROXY_PATH}}/v1/s/x3.js
curl -s -o /dev/null -w "%{http_code}\n" https://YOUR-SITE{{PROXY_PATH}}/v1/c/{{PUBLISHABLE_KEY}}

Both must print 200. Then load a page: the network tab shows POST {{PROXY_PATH}}/v1/e → 204. A 403 on that request with a correct origin means the proxy key is wrong, revoked or belongs to another project.

The handler also serves POST {{PROXY_PATH}}/v1/id: after consent the SDK calls it once per page load and the handler sets or renews the HttpOnly first-party cookie x3_vid with the project's lifetime the SDK sends as days (default and maximum 13 months, see visitor id lifetime); on revocation it deletes it. Batches with consent: true get the cookie's value injected as vid (the cookie is renewed by /v1/id, not by batches), batches without consent delete the cookie. Check it after accepting your banner:

curl -si -X POST https://YOUR-SITE{{PROXY_PATH}}/v1/id -d '{"consent":true}' | grep -i -E "set-cookie|x-3x-vid"

prints set-cookie: x3_vid=…; Path=/; Max-Age=34128000; HttpOnly; SameSite=Lax; Secure and x-3x-vid: 1. See Consent and CMPs.

Other runtimes

// Hono / Bun / Deno – mount the same handler
import { createX3ProxyHandler } from "@x3growth/sdk/next/proxy";

const proxy = createX3ProxyHandler({ apiOrigin: process.env.X3_API_ORIGIN!, proxyKey: process.env.X3_PROXY_KEY });
app.all("/n3/*", (c) => {
  const method = c.req.method as "GET" | "POST" | "OPTIONS";
  return proxy[method] ? proxy[method](c.req.raw) : new Response(null, { status: 405 });
});

Visitor address

The API trusts the address the proxy sends with the proxy key (rate limits, daily budget, visitor hash, conversion context), so the handler never takes it from a header the client can write:

  1. clientIp(headers) of the options, when you pass one (e.g. the socket address of your Node server);
  2. the header the hosting platform sets itself: Vercel x-real-ip, Netlify x-nf-client-connection-ip, Cloudflare Workers cf-connecting-ip (the platform is detected from the runtime, not from the request);
  3. x-forwarded-for read from the right: internal proxies (private, loopback and link-local addresses) are skipped, then trustedProxies - 1 public proxies (default trustedProxies: 1). Entries further left are whatever the client wrote. Behind a public CDN in front of your load balancer, set trustedProxies: 2;
  4. x-real-ip.
const proxy = createX3ProxyHandler({
  apiOrigin: process.env.X3_API_ORIGIN!,
  proxyKey: process.env.X3_PROXY_KEY,
  // only when the default rule does not fit your hosting
  trustedProxies: 2,
});

Helpers

For a proxy of your own, @x3growth/sdk/next/proxy also exports the handler's building blocks:

ExportReturns
proxiedPath(pathname, method)the /v1/… part when it is one the SDK uses (POST /v1/e, /v1/collect, /v1/cb; GET /v1/c/{pk}, /v1/s/{file}), else null
visitorIp(headers, options?)the visitor address by the rule of Visitor address
visitorCountry(headers)ISO country from x-vercel-ip-country / cf-ipcountry
VISITOR_COOKIE"x3_vid"
visitorCookie(headers)the valid x3_vid value of the Cookie header, else null
visitorSetCookie(value, url, days?)the Set-Cookie value for x3_vid (null deletes it; days 1–395, default 395; Secure except on plain-http localhost)