SDKs
Node.js
Server-side decisions for many users, plus AI config helpers.
@liftcanary/node decides on your server: one client serves every request, keeps the datafile fresh in the background and sends events in batches. Decisions are in-process function calls, so they add no latency to your requests.
Install#
npm install @liftcanary/nodeRequires Node.js 18 or newer (it uses the built-in fetch).
Usage#
import { createClient } from "@liftcanary/node";
export const liftcanary = createClient({ sdkKey: process.env.LIFTCANARY_SDK_KEY! });
await liftcanary.ready(); // at startupapp.post("/checkout", async (req, res) => {
const user = liftcanary.createUser({
id: req.user.id,
attributes: { plan: req.user.plan, country: req.headers["cf-ipcountry"] as string },
});
const flow = user.getVariable("new_checkout", "flow", "classic");
const order = await placeOrder(req.body, flow);
user.track("purchase", { value: order.total });
res.json(order);
});Server defaults#
The Node.js client starts with defaults suited to one process deciding for many users:
streamingtrue | Live updates: a published change arrives in about a second. The connection never keeps your process from exiting; false turns it off. |
pollingIntervalMs30000 | Also checks for a new datafile every 30 s, in case the live connection drops (ETag, so unchanged datafiles cost almost nothing). |
eventFlushIntervalMs2000 | Sends queued events every 2 s. |
eventBatchSize200 | Events per request. |
eventMaxQueueSize20000 | Events held in memory before the oldest are dropped (counted in client.events.dropped). |
modelPricesRecord<model, { input, output }> | Price per 1M tokens, for AI configs. |
Any of them can be overridden, along with the shared options.
Shutting down#
process.on("SIGTERM", async () => {
await liftcanary.close(); // stops polling and flushes remaining events
process.exit(0);
});Pages, locations and request context#
Location conditions need the page: pass the full request URL as page, and the keys of activated triggered locations, if you track them, as activeLocations. Neither is sent with events.
At sign-in, pass the browser's anonymous id (the liftcanary_anon cookie, also exported as ANONYMOUS_ID_COOKIE) as anonymousId, so results count the landing-page visit and the signed-in user as one person. See From landing page to app.
liftcanary.createUser({ id, page: `https://${req.headers.host}${req.originalUrl}` });AI configs#
The package also has aiConfig, measureLLM, measureLLMStream (streamed answers, with the time to first token), trackLLM, trackFeedback and renderPrompt for testing models and prompts. See AI configs and LLM experiments.