Skip to content

Latest commit

 

History

History
151 lines (107 loc) · 4.07 KB

File metadata and controls

151 lines (107 loc) · 4.07 KB
title Apply Code Changes
description Install the SDK dependency and add initialization code to the application entrypoint

Apply Code Changes

Now execute the integration plan. Install the SDK and add the minimal code needed to initialize it.

Step 1: Install the SDK Dependency

Run the install command from the SDK Recipes using the project's package manager.

Examples:

# Node.js (npm)
npm install @launchdarkly/node-server-sdk --save

# Python (pip)
pip install launchdarkly-server-sdk

# Go
go get github.com/launchdarkly/go-server-sdk/v7

# Ruby
bundle add launchdarkly-server-sdk

# .NET
dotnet add package LaunchDarkly.ServerSdk

After installation, verify the dependency appears in the lock file or dependency manifest.

Step 2: Add the SDK Key to Environment Configuration

Never hardcode SDK keys in source code.

Add the appropriate environment variable:

SDK Type Variable Name Where to Get It
Server-side LAUNCHDARKLY_SDK_KEY LaunchDarkly > Project settings > Environments > SDK key
Client-side LAUNCHDARKLY_CLIENT_SIDE_ID LaunchDarkly > Project settings > Environments > Client-side ID
Mobile LAUNCHDARKLY_MOBILE_KEY LaunchDarkly > Project settings > Environments > Mobile key

Add to the project's environment configuration:

  1. If .env exists, add the variable there
  2. If .env.example or .env.sample exists, add a placeholder entry
  3. If the project uses a config module, add it there following existing patterns
# .env
LAUNCHDARKLY_SDK_KEY=your-sdk-key-here

Tell the user they need to replace the placeholder with their actual key.

Step 3: Add SDK Initialization Code

Add the import and initialization code to the application entrypoint. Place initialization early in the startup sequence, before any code that might need feature flags.

Key Principles

  1. Import at the top of the file with other imports
  2. Initialize early in the application lifecycle
  3. Wait for initialization before evaluating flags (most SDKs support this)
  4. Handle errors — log failures but don't crash the application
  5. Match existing code style — use the same patterns (async/await, callbacks, error handling) as the rest of the codebase

Server-Side Initialization Pattern

// Node.js example
const LaunchDarkly = require('@launchdarkly/node-server-sdk');

const ldClient = LaunchDarkly.init(process.env.LAUNCHDARKLY_SDK_KEY);

// Wait for initialization before serving requests
await ldClient.waitForInitialization();
console.log('LaunchDarkly SDK initialized successfully');
# Python example
import ldclient
from ldclient.config import Config

ldclient.set_config(Config(os.environ['LAUNCHDARKLY_SDK_KEY']))
client = ldclient.get()

if client.is_initialized():
    print('LaunchDarkly SDK initialized successfully')
// Go example
import ld "github.com/launchdarkly/go-server-sdk/v7"

ldClient, err := ld.MakeClient(os.Getenv("LAUNCHDARKLY_SDK_KEY"), 5*time.Second)
if err != nil {
    log.Printf("LaunchDarkly SDK failed to initialize: %v", err)
}

Client-Side Initialization Pattern

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

const LDProvider = await asyncWithLDProvider({
  clientSideID: process.env.REACT_APP_LAUNCHDARKLY_CLIENT_SIDE_ID,
  context: {
    kind: 'user',
    key: 'anonymous',
  },
});

// Wrap your app with the provider
root.render(
  <LDProvider>
    <App />
  </LDProvider>
);

Refer to the SDK Recipes for language-specific import and init snippets.

Step 4: Verify the Code Compiles

After making changes:

  1. Run the project's build or compile step
  2. Run the linter if one is configured
  3. Fix any import errors or type issues

Do not proceed to the next step if the code doesn't compile.

Status

[STATUS] Installing SDK dependency
[STATUS] Configuring SDK key
[STATUS] Adding initialization code
[STATUS] Verifying code compiles

Upon completion, continue with: Start the Application