Evaluate feature flags
mainThere are two ways to evaluate feature flags:
feature_flagsclaim: UsewithAuth()to retrieve flags directly from the user's session. This is best for small flag sets. Flags update on the next login or session refresh.- Runtime Client: Use
getFeatureFlagsRuntimeClient()for large flag sets or when you need server-side evaluation that stays in sync independently of the user session. This client should be created once per server process.
Note: When using the runtime client, call await featureFlags.waitUntilReady() to ensure the client is initialized.
import { getFeatureFlagsRuntimeClient, withAuth } from '@workos-inc/authkit-nextjs';
const featureFlags = getFeatureFlagsRuntimeClient();
export default async function DashboardPage() {
const { user, organizationId } = await withAuth({ ensureSignedIn: true });
try {
await featureFlags.waitUntilReady({ timeoutMs: 5000 });
} catch (error) {
console.error('Feature flags client failed to initialize:', error);
}
const enabled = featureFlags.isEnabled('advanced-analytics', {
userId: user.id,
organizationId,
});
return enabled ? <AdvancedAnalytics /> : <BasicAnalytics />;
}