Skip to content

Latest commit

 

History

History
194 lines (146 loc) · 5.02 KB

File metadata and controls

194 lines (146 loc) · 5.02 KB
title Create Your First Feature Flag
description Create a boolean feature flag, add evaluation code, and toggle it to demonstrate end-to-end functionality

Create Your First Feature Flag

The SDK is connected. Now help the user create their first feature flag and see it work end-to-end.

Step 1: Create the Flag

Via MCP (Preferred)

If the LaunchDarkly MCP server is available, use create-flag:

  • Key: my-first-flag (or a name relevant to the user's project)
  • Name: "My First Flag"
  • Kind: boolean
  • Variations: true / false
  • Temporary: true

Via LaunchDarkly API

curl -s -X POST \
  "https://app.launchdarkly.com/api/v2/flags/PROJECT_KEY" \
  -H "Authorization: LAUNCHDARKLY_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "My First Flag",
    "key": "my-first-flag",
    "kind": "boolean",
    "variations": [
      {"value": true},
      {"value": false}
    ],
    "temporary": true
  }'

Via ldcli

ldcli flags create \
  --access-token YOUR_ACCESS_TOKEN \
  --project PROJECT_KEY \
  --data '{"name": "My First Flag", "key": "my-first-flag", "kind": "boolean"}'

After creation, the flag starts with targeting OFF, serving the off variation (false) to everyone.

Step 2: Add Flag Evaluation Code

Add code to evaluate the flag in the application. Place this where it makes sense for the user's feature.

Server-Side Examples

// Node.js
const context = { kind: 'user', key: 'example-user-key', name: 'Example User' };
const showFeature = await ldClient.variation('my-first-flag', context, false);

if (showFeature) {
  console.log('Feature is ON');
} else {
  console.log('Feature is OFF');
}
# Python
context = Context.builder("example-user-key").name("Example User").build()
show_feature = client.variation("my-first-flag", context, False)

if show_feature:
    print("Feature is ON")
else:
    print("Feature is OFF")
// Go
context := ldcontext.NewBuilder("example-user-key").Name("Example User").Build()
showFeature, _ := ldClient.BoolVariation("my-first-flag", context, false)

if showFeature {
    fmt.Println("Feature is ON")
} else {
    fmt.Println("Feature is OFF")
}

Client-Side Examples

// React
import { useFlags } from 'launchdarkly-react-client-sdk';

function MyComponent() {
  const { myFirstFlag } = useFlags();

  return (
    <div>
      {myFirstFlag ? <p>Feature is ON</p> : <p>Feature is OFF</p>}
    </div>
  );
}

Note: Client-side React SDK camelCases flag keys, so my-first-flag becomes myFirstFlag.

Step 3: Verify the Default Value

With targeting OFF, the flag should evaluate to false. Run the application and confirm:

Feature is OFF

Step 4: Toggle the Flag On

Via MCP

Use the toggle-flag tool to enable the flag in the target environment.

Via LaunchDarkly API

curl -s -X PATCH \
  "https://app.launchdarkly.com/api/v2/flags/PROJECT_KEY/my-first-flag" \
  -H "Authorization: LAUNCHDARKLY_ACCESS_TOKEN" \
  -H "Content-Type: application/json; domain-model=launchdarkly.semanticpatch" \
  -d '{
    "environmentKey": "ENVIRONMENT_KEY",
    "instructions": [
      {"kind": "turnFlagOn"}
    ]
  }'

Via ldcli

ldcli flags toggle-on \
  --access-token YOUR_ACCESS_TOKEN \
  --project PROJECT_KEY \
  --environment ENVIRONMENT_KEY \
  --flag my-first-flag

Step 5: Verify the Toggle

After toggling the flag on, the application should now show:

Feature is ON

For server-side SDKs using streaming (the default), the change should be reflected within seconds. For client-side SDKs, the change appears on the next page load or when the SDK polls for updates.

Congratulations

The user has successfully:

  1. Installed the LaunchDarkly SDK
  2. Connected it to LaunchDarkly
  3. Created a feature flag
  4. Evaluated it in code
  5. Toggled it and seen the result

Error Handling

If any step returns an authorization error (401 or 403):

  1. Check if the user is logged in: The access token may be expired or invalid.
  2. Prompt the user to log in: Direct them to ldcli login or https://app.launchdarkly.com to sign in.
  3. If the user doesn't have an account: Prompt them to sign up at https://launchdarkly.com.
  4. If the project or environment doesn't exist: Guide the user to create one via the dashboard or ldcli.

Do not retry authorization errors automatically — they require user action.

Next steps to suggest:

Status

[STATUS] Creating feature flag
[STATUS] Adding flag evaluation code
[STATUS] Verifying flag evaluates to default
[STATUS] Toggling flag on
[STATUS] Verifying flag toggle works