Install Lucida on server Node

Send events from a Node.js server with @lucida-analytics/node: install, init with the key, request context, consent responsibility and a first-event check.

Embed
Preview of the card
<a href="https://lucidace.com/help/install-node"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/5ff54368-89b5-4952-9616-f18be89f86ea/social-card.svg?v=3e7ae33eec8b" alt="Install Lucida on server Node" width="600" height="315"></a>

The Node SDK sends events from your server: backend actions, webhooks, jobs. It does not capture page views. Pair it with a browser install (all frameworks) for front-end behavior.

1. Install

npm install @lucida-analytics/node

Put the key in .env as LUCIDA_SECRET_KEY: new Lucida() and captureImmediate read it when you pass no key (the older LUCIDA_KEY still works as a fallback). It is a server key: never NEXT_PUBLIC_, and not the CLI's LC_API_KEY, which is an account key. lucida init --framework node --host https://capture.lucidace.com writes the env var and an init module.

2. Init with the key

import { Lucida } from "@lucida-analytics/node";

export const lucida = new Lucida(); // reads LUCIDA_SECRET_KEY

lucida.capture({ distinctId: "user_123", event: "invoice_paid", properties: { total: 12 } });
lucida.identify({ distinctId: "user_123", properties: { plan: "pro" } });
await lucida.shutdown(); // flush on exit

The default host is https://capture.lucidace.com, so host is not needed on the cloud. Set host only when you self-host or send through a proxy. A public key (lc_pk_...) sends events. Add secretKey: "lc_sk_..." only if you evaluate feature flags locally; never put it in client code.

Events are batched (20 events or 10 s), gzipped over 1 KB, retried on 408, 429 and 5xx, and the oldest are dropped if the in-memory queue is full. Call shutdown() before the process exits.

Inside a request, withContext carries the user so you can omit distinctId. Express, Fastify and NestJS helpers are in @lucida-analytics/node/middleware:

import { setupRequestContext } from "@lucida-analytics/node/middleware";
app.use(setupRequestContext(lucida)); // Express

It reads X-Lucida-Distinct-Id and X-Lucida-Session-Id from the browser SDK so server events join the browsing session. Those values come from the client: use them to stitch events, never for auth.

Route handlers and serverless: captureImmediate

In a short-lived function, a queue and shutdown() are easy to get wrong. captureImmediate (node 0.1.4) sends one event with one POST and keeps nothing running afterwards: no client, no queue, no retries, no timers.

import { captureImmediate } from "@lucida-analytics/node";

export async function POST(req: Request) {
  const r = await captureImmediate({ event: "signed_up", distinctId: "user_123", properties: { plan: "pro" }, $set: { plan: "pro" } });
  if (!r.ok) console.warn("lucida", r.error);
  return Response.json({ ok: true });
}
  • Input: event and distinctId are required. Optional: properties, $set, uuid (a random one is made if you leave it out; pass your own to make a retry deduplicate) and timestamp (a Date or string; default now).

  • Options, as a second argument: apiKey and host. Also consentFromCookies (below). Left out, the key is read from LUCIDA_SECRET_KEY (fallback LUCIDA_KEY) and the host from LUCIDA_HOST, else https://capture.lucidace.com.

  • It never throws. It returns { ok, status?, error? }: ok: true with the HTTP status; or ok: false with error (no api key ..., timeout, status <code>, or the network error message).

  • The request times out after 5 seconds. A failed call is not retried: retry yourself with the same uuid if the event matters.

  • await it, or the platform may freeze the function before the POST finishes.

3. Content Security Policy

Not applicable: CSP is enforced by browsers. A server-to-server call only needs outbound access to https://capture.lucidace.com.

4. Reverse proxy

Not needed. Ad blockers do not affect server calls. If egress goes through your own gateway, set host to it.

The server SDK does not run a consent banner: you own the decision. Send events about a person only if the person consented in your product, and respect a withdrawal by dropping the event before capture (or use beforeSend(event) => event | null to drop it centrally). For personless server events, pass processPersonProfile: false.

If the visitor used the browser SDK, its decision is in the lc_consent cookie. Its values are a stable contract: exactly granted or denied; no cookie means no decision yet. The anonymous id is the separate lc_distinct_id cookie (written on grant, removed on denial). consentFromCookie(cookieHeader) reads it, and captureImmediate does it for you:

import { cookies } from "next/headers";
await captureImmediate({ event: "signup", distinctId: id }, { consentFromCookies: await cookies() });

Granted stamps $consent_analytics: "granted" on the event and on the person ($set), so the grant persists. Denied sends nothing and resolves { ok: false, error: "consent denied" }. No cookie sends as before. A raw Cookie header string works too.

6. Check the first event

  • Terminal: lucida init --framework node --host https://capture.lucidace.com --project $LUCIDA_PROJECT_ID --verify waits for a new event (needs an account key). Run a code path that calls capture.

  • App: the Install wizard (onboarding page) waits for the first event and tells you when it arrives.

If nothing arrives, pass onError or debug: true to see dropped batches, and make sure the process called shutdown() or stayed alive for 10 s.

More

Send events over HTTP, Install an SDK, Developer guide: sending data, quickstart.

Updated