Server flags
Create one flags manager per process and pass each request's user to it.
import { createServerFlagsManager, FlagsRequestError } from "@databuddy/sdk/node";
export const flags = createServerFlagsManager({
clientId: process.env.DATABUDDY_CLIENT_ID!,
});
export async function isOn(key: string, userId: string): Promise<boolean> {
try {
const result = await flags.getFlag(key, { userId });
return result.enabled;
} catch (error) {
if (error instanceof FlagsRequestError) return false;
throw error;
}
}getFlag() throws a FlagsRequestError only when the request fails and nothing is cached for that flag, so wrap first reads and pick a safe default.
Read flags
import { flags } from "@/lib/flags";
export async function POST(request: Request) {
const user = { userId: request.headers.get("x-user-id") ?? undefined };
await flags.fetchAllFlags(user);
const guided = flags.isEnabled("guided-export", user).on;
const limit = flags.getValue("export-limit", 100, user);
return Response.json({ guided, limit });
}id is the flag definition's ID, as in GET /public/v1/flags/definitions. It's absent when the flag doesn't exist (reason: "NOT_FOUND"). After a successful fetchAllFlags(), keys it didn't return resolve as NOT_FOUND without another request.
Configure the manager
Databuddy uses the user only to evaluate that request and doesn't store it. Checking flags from your server creates no analytics events unless you turn on trackEvaluations.
Choose what happens when Databuddy is down
If refreshes keep failing, the manager serves the last answer for up to maxStaleTime after it was evaluated. After that, getFlag() throws, isEnabled() returns your defaults value with status: "error", and getValue() returns its default. Retries wait 5 seconds, doubling to 60 seconds, or the API's Retry-After.
import { createServerFlagsManager } from "@databuddy/sdk/node";
export const flags = createServerFlagsManager({
clientId: process.env.DATABUDDY_CLIENT_ID!,
maxStaleTime: 60_000,
defaults: { "new-checkout": false },
});That setup fails closed: features turn off within a minute of an outage. To evaluate every flag for one user in one request without a manager, such as while rendering a page, use evaluateFlags.
Record evaluations
Flag activity and experiment results count $flag_evaluated events, which the browser sends when it reads a flag. To count your server's reads too, pass your Node client as trackEvaluations.
import { createServerFlagsManager, Databuddy } from "@databuddy/sdk/node";
const databuddy = new Databuddy({ apiKey: process.env.DATABUDDY_API_KEY! });
export const flags = createServerFlagsManager({
clientId: process.env.DATABUDDY_CLIENT_ID!,
trackEvaluations: databuddy,
});Each read sends one event per user and value while the manager runs, and remembers the latest 10,000. It's linked to the userId you pass, which can be the browser's anonymous ID from getIds() for a signed-out visitor. Unknown flags and failed evaluations aren't sent. Each event counts toward your plan, and with batching on you call flush() before a serverless function returns.
Related
How is this guide?