Feature Flags
Control access to features in your application.
Feature flags control access to specific features at runtime. @repo/feature-flags exists to:
- Roll out features gradually to users
- A/B test new functionality
- Enable/disable features based on user segments
- Control access to beta or experimental features
How it works
The package is built on the Flags SDK, which is largely an architectural pattern rather than a service.
createFlag is the entry point. A flag created through it carries authentication and PostHog resolution.
For FLAGS_SECRET, you can run node -e "console.log(crypto.randomBytes(32).toString('base64url'))" or openssl rand -base64 32 in your terminal to generate a random value.
Adding a new flag
To add a new flag, simply create a feature flag in PostHog and then modify the feature flags index file. Let's add a new flag called showAnalyticsFeature that will be enabled for all users:
import { createFlag } from './lib/create-flag';
export const showBetaFeature = createFlag('showBetaFeature');
export const showAnalyticsFeature = createFlag('showAnalyticsFeature');Make sure the key you're using matches the key in PostHog exactly.
Usage in your application
To use the flag in your application, simply import it from the @repo/feature-flags package and use it like a normal boolean value.
import { showAnalyticsFeature } from '@repo/feature-flags';
import { notFound } from 'next/navigation';
export default function Page() {
const isEnabled = showAnalyticsFeature();
if (!isEnabled) {
notFound();
}
return <div>Analytics feature is enabled</div>;
}Because feature flags are tied to the user, they only work if the user is signed in. Otherwise, the flag will always return false.
Overriding flags in development
Override a flag in development from the PostHog project that resolves it. The in-browser toolbar that used to provide overrides was removed with the move to Cloudflare; it was a hosting-platform feature, not part of the Flags SDK.