Feature flags
Declare a flag with its default, read it with one hook, and change who sees it from the dashboard without a deploy.
DatabuddyProvider loads flags the first time a component reads one, so apps that use no flags make no flag requests.
Read a flag
import { useFlagDetails } from "@databuddy/sdk/react";
import { newCheckout } from "./flags";
export function Checkout() {
const { value, loading, status } = useFlagDetails(newCheckout);
if (loading) return <Skeleton />;
if (status === "error") return <OldCheckout />;
return value ? <NewCheckout /> : <OldCheckout />;
}The default is also served when the provider is disabled and when the flag doesn't exist (NOT_FOUND). A value whose type doesn't match the default is ignored too (TYPE_MISMATCH). A failed request is retried after 5 seconds, doubling up to 60 seconds, or after the API's Retry-After. A failed refresh keeps the last value.
Target users and accounts
Flags target the user you pass to the provider. The flag check uses it only to evaluate the request and stores none of it.
In the dashboard, choose what a rollout should Bucket by. Everyone in one bucket gets the same result.
Rules run in that order, and the first match wins. Rules and target groups support equals, contains, starts_with, ends_with, in and not_in; property rules add exists and not_exists. A flag that depends on another is blocked while that parent is paused, archived or deleted in the same environment.
Run an A/B test
Make a multivariant flag in the dashboard, then render from variant. Weights must total 100, or leave them out for an even split. Each user gets the same variant every time.
import { defineFlag } from "@databuddy/sdk";
import { useFlagDetails } from "@databuddy/sdk/react";
const exportFlow = defineFlag("export-flow", { default: "control" });
export function ExportPanel() {
const { variant, loading } = useFlagDetails(exportFlow);
if (loading) return <Skeleton />;
return variant === "guided" ? <GuidedExport /> : <DefaultExport />;
}Reading a flag sends one $flag_evaluated event per flag per page load, which counts toward your plan. In Feature flags → Experiments, pick the flag and a conversion goal to compare each variant's visitors and conversions after their first evaluation.
An evaluation isn't proof the feature was seen. For that, track() one exposure event when the flagged view appears, then the outcome, each with the flag key and variant.
Render flags on the server
Evaluate flags while rendering and pass them to the provider. The page renders with real values, and the browser starts from them without a request.
import { evaluateFlags } from "@databuddy/sdk/node";
import { DatabuddyProvider } from "@databuddy/sdk/react";
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const session = await getSession();
const user = session ? { id: session.user.id, traits: { email: session.user.email } } : null;
const clientId = process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!;
const bootstrap = await evaluateFlags({ clientId, user });
return (
<html lang="en">
<body>
<DatabuddyProvider clientId={clientId} user={user} flags={{ bootstrap }}>
{children}
</DatabuddyProvider>
</body>
</html>
);
}Configure the provider
Reads made within 10 ms share one request, and identical requests are sent once. Background refreshes pause while the tab is hidden. Changing user clears the previous user's flags and fetches the new ones; inline objects are compared by value.
The Vue plugin takes user: { userId, email, organizationId, teamId, properties } and isPending, disabled, environment, cacheTtl, staleTime, maxStaleTime, autoFetch and skipStorage directly.
Related
How is this guide?