Create a Flag
Creating your first Feature in GrowthBook
Section titled “Creating your first Feature in GrowthBook”A Feature in GrowthBook maps directly to a feature flag in your code. Here is how to create one from scratch.
Step 1 — Open the Features list
Section titled “Step 1 — Open the Features list”- Log in to GrowthBook at
http://localhost:3000(or your Cloud URL). - In the left sidebar, click Features.
- Click the Add Feature button in the top-right corner.
Step 2 — Fill in the Feature details
Section titled “Step 2 — Fill in the Feature details”- Feature Key — enter a lowercase, hyphen-separated identifier, e.g.
my-flag. This is the string your code will use forever — choose carefully. - Value Type — select Boolean for a simple on/off flag.
- Default Value — set it to
false(off by default everywhere). - Click Create Feature.
Step 3 — Enable the flag in an environment
Section titled “Step 3 — Enable the flag in an environment”By default, your flag exists but is disabled in all environments.
- On the Feature detail page, find the Environments section. You will see rows for development, staging, and production.
- Click the toggle next to development to switch it from Disabled to Enabled.
- Because there are no override rules, every user will receive the default value of
truein development.
Tip: You can set a per-environment default that is different from the global default. This lets you ship
truein development while keepingfalsein production until you are ready.
Reading the flag in your code
Section titled “Reading the flag in your code”Once the feature exists in GrowthBook, use the SDK to evaluate it:
Boolean check with isOn
Section titled “Boolean check with isOn”import { GrowthBook } from '@growthbook/growthbook';
const gb = new GrowthBook({ apiHost: 'https://cdn.growthbook.io', clientKey: 'sdk-YOUR_DEV_KEY',});
await gb.init({ timeout: 2000 });
if (gb.isOn('my-flag')) { console.log('Flag is ON');} else { console.log('Flag is OFF');}Reading a value with getFeatureValue
Section titled “Reading a value with getFeatureValue”For boolean flags you can also use getFeatureValue, which requires an explicit fallback:
const flagValue = gb.getFeatureValue('my-flag', false);// returns true or false, never throwsimport { GrowthBook } from '@growthbook/growthbook';
const gb = new GrowthBook({
apiHost: 'https://cdn.growthbook.io',
clientKey: 'sdk-YOUR_DEV_KEY',
});
await gb.init({ timeout: 2000 });
// boolean shorthand
if (gb.isOn('my-flag')) {
console.log('Flag is ON');
}
// explicit value with fallback
const flagValue = gb.getFeatureValue('my-flag', false);