Skip to content

Server-side events

Both features on this page authenticate with an API key that has the write:events scope. Keep it in a server-only environment variable such as BEACON_API_KEY, and never import @tbdhdev/beacon-sdk/server into browser code.

createBeaconServer() sends events from anything with fetch: Node, Bun, Deno, edge functions, route handlers, server actions, workers and CI.

import { createBeaconServer } from "@tbdhdev/beacon-sdk/server";
const beacon = createBeaconServer({
websiteId: "your-website-uuid",
endpoint: "https://beacon.example.com/x/e",
apiKey: process.env.BEACON_API_KEY!,
});
await beacon.track("purchase", { amount: 49, currency: "USD" });
await beacon.identify("user_123", { plan: "pro" });

Pass the end user’s request details as the last argument, so the event counts toward their visit with the same browser, OS and country as their pageviews. The IP is only used for the visitor hash and the country, and is never stored.

await beacon.track(
"checkout_completed",
{ amount: 49 },
{
url: request.headers.get("referer") ?? undefined,
// Your proxy appends the address it saw; earlier entries can be forged.
ip: request.headers.get("x-forwarded-for")?.split(",").at(-1)?.trim(),
userAgent: request.headers.get("user-agent") ?? undefined,
distinctId: "user_123",
},
);
Context field What it does
url The page the event belongs to (absolute URL).
ip The end user’s IP, for the visitor hash and country.
userAgent The end user’s user agent, for browser, OS and device.
distinctId Your own user id, like identify() in the browser.
title, referrer, language Optional page details.

Events sent without these details (from a cron job or a webhook) are grouped under one anonymous visitor.

Calls reject when Beacon refuses the event: 401 for an unknown key or one without write:events, 403 for an expired key or one that doesn’t cover the website, 404 for an unknown website, 400 for an invalid event. To keep analytics out of your response time, don’t await the call:

beacon.track("signup", { plan: "pro" }).catch(console.error);

Without the SDK, POST the same JSON to the event URL with Authorization: Bearer <key>. The website’s Settings → Tracking code → Server & CI tab has ready-made fetch and curl snippets.

Ad blockers refuse requests to hosts and paths they know serve analytics. A full-stack app can send browser events to one of its own routes, which forwards them to Beacon from your server.

  1. Create an API key with the write:events scope for the website.
  2. Add a route that passes the request to proxyBeacon(). Avoid words blockers look for, such as analytics, track, collect or beacon.
  3. Set the browser SDK’s endpoint to that path.
app/api/e/route.ts
import { forwardedFor, proxyBeacon } from "@tbdhdev/beacon-sdk/server";
export function POST(request: Request) {
return proxyBeacon(request, {
endpoint: "https://beacon.example.com/x/e",
apiKey: process.env.BEACON_API_KEY!,
clientIp: forwardedFor(request),
});
}
<Beacon websiteId="your-website-uuid" endpoint="/api/e" autoPageviews={false} />

Any handler that gets a standard Request works the same way: SvelteKit +server.ts, Astro endpoints, TanStack Start server routes, Hono, Bun and Deno.

The proxy forwards the body and the browser headers Beacon reads, never cookies. Beacon treats the event as if the browser had sent it: bot filtering, Do Not Track and allowed hostnames still apply.

The API key vouches for the IP you pass as clientIp, so take it from a source your host guarantees:

Where clientIp
Vercel, or behind Caddy or Nginx forwardedFor(request)
Cloudflare request.headers.get("cf-connecting-ip")
Netlify request.headers.get("x-nf-client-connection-ip")
Fly.io request.headers.get("fly-client-ip")
TanStack Start on Node, nothing in front getRequestIP() from @tanstack/react-start/server
SvelteKit getClientAddress() from the request event

With nothing in front of your server, visitors can send their own x-forwarded-for, so don’t use forwardedFor() there.