Local flag evaluation in the Node SDK
Evaluate feature flags inside your Node server from downloaded definitions: the key it needs, how definitions refresh, what happens when a fetch fails, and how exposures are recorded.
By default every flag read from a server is one POST /v1/flags call. With local evaluation the Node SDK downloads your flag definitions once, keeps them in memory and decides each assignment in-process, so a read costs no network call. It needs @lucida-analytics/node 0.1.1 or later.
Turn it on
Pass a secret key (lc_sk_...) next to the project key. Without one the SDK has no access to definitions and every read is remote.
import { Lucida } from "@lucida-analytics/node";
const lucida = new Lucida("lc_pk_...", {
host: "https://capture.lucidace.com",
secretKey: process.env.LUCIDA_SECRET_KEY, // lc_sk_..., server only
});
await lucida.ready; // resolves after the first definitions fetch
const variant = await lucida.getFeatureFlag("onboarding-post-login", "user_123");personalApiKey is an alias of secretKey. Keep the key on the server; the definitions include targeting rules.
How definitions refresh
The SDK fetches
GET /v1/flags/definitionswith the secret key, sending the lastETagasIf-None-Match, so an unchanged set costs a304.It also opens
GET /v1/flags/stream(server-sent events) and reloads on every change, so an edit or a kill switch reaches your servers in seconds. Turn the stream off withstreamFlags: false.It polls every
featureFlagsPollingIntervalMs(default 30 000 ms), but only while the stream is down.lucida.reloadFeatureFlags()fetches now.Several workers can share one fetch with
flagDefinitionCacheProvider(an in-memory, Redis or other key-value store).
What happens on failure
A failed refresh keeps the definitions you already have. It is logged only when
debug: true.Before the first successful fetch there are no definitions, so reads go to
POST /v1/flags.Flags that cannot be decided locally (a cohort filter) also go to
POST /v1/flags, unless you passonlyEvaluateLocally: trueon the call.If that call fails too, the SDK serves
fallbacks[key]when you set one (reason"fallback", no exposure recorded), else the call'sdefaultValue, elseundefined.
Exposures
getFeatureFlag, isFeatureEnabled and getFeatureFlagDetails record $feature_flag_called and $exposure with locally_evaluated: true, once per distinct id, flag and value in that process (the last 10 000 combinations are remembered, so a restart records them again). Experiment results read these events. Pass sendExposure: false on a call to skip it. getFeatureFlagPayload, getAllFlags, getAllFlagsAndPayloads and evaluateFlags never record one; attach a snapshot to an event with capture({ flags }) instead. A served fallback is never an exposure.
Tests
new Lucida("lc_pk_test", { testMode: { fixture: "flags.json" } }) runs offline against a file written by lucida flags fixture --out flags.json. Events land in lucida.captured.