Cloudflare Workers
Send events from a Worker or a Pages Function: sign-ups, purchases, API calls, webhooks, anything that happens on the server. The SDK uses fetch, so it runs in Workers with no extra setup and no Node.js compatibility flag.
npm install @flarepanel/sdk1. Store the client secret
Server events authenticate with your project's client secret. Keep it in a secret, not in wrangler.jsonc:
npx wrangler secret put FLAREPANEL_CLIENT_SECRETPut the client ID, which is not secret, in vars:
// wrangler.jsonc
{
"vars": { "FLAREPANEL_CLIENT_ID": "YOUR_CLIENT_ID" }
}For a Pages project, use npx wrangler pages secret put FLAREPANEL_CLIENT_SECRET and add the client ID under Settings → Variables and Secrets.
2. Send events after the response
Wrap the call in ctx.waitUntil, so the visitor gets the response at once and the event is sent in the background:
// src/index.ts
import { FlarePanel } from '@flarepanel/sdk';
interface Env {
FLAREPANEL_CLIENT_ID: string;
FLAREPANEL_CLIENT_SECRET: string;
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const op = new FlarePanel({
clientId: env.FLAREPANEL_CLIENT_ID,
clientSecret: env.FLAREPANEL_CLIENT_SECRET,
});
const user = await signUp(request); // your code
ctx.waitUntil(
op.track('signed_up', {
profileId: user.id,
plan: user.plan,
// The visitor's address, used for location only and not stored.
__ip: request.headers.get('cf-connecting-ip') ?? undefined,
})
);
return Response.json({ ok: true });
},
};A Pages Function has the same pieces on its context:
// functions/api/signup.ts
import { FlarePanel } from '@flarepanel/sdk';
export const onRequestPost: PagesFunction<Env> = async (context) => {
const op = new FlarePanel({
clientId: context.env.FLAREPANEL_CLIENT_ID,
clientSecret: context.env.FLAREPANEL_CLIENT_SECRET,
});
context.waitUntil(op.track('signed_up', { profileId: 'user_123' }));
return Response.json({ ok: true });
};Who the event belongs to
A server has no browser, so tie each event to a person:
- Pass
profileIdfor signed-in users. Their server events then join their browser events in the same profile. - Pass
__ipwith the visitor's address fromcf-connecting-ipto place the event where the visitor is. Without it, the location comes from your Worker's own request, so it shows where the Worker ran rather than the visitor. The address is used for the lookup and is not stored.
See Events and properties and Identify users for the rest of the event format.
AI crawlers from the same Worker
If the Worker serves your website, it can also report which AI crawlers (GPTBot, ClaudeBot, PerplexityBot and others) read your pages. Crawlers do not run JavaScript, so the script tag never sees them. Wrap your handler with withCrawlCapture as shown in AI crawlers and bots.
Self-hosting
Set apiUrl to your own API when you run FlarePanel on your own Cloudflare account:
const op = new FlarePanel({
apiUrl: 'https://api.your-domain.com',
clientId: env.FLAREPANEL_CLIENT_ID,
clientSecret: env.FLAREPANEL_CLIENT_SECRET,
});All options are listed in SDK options.