Back to snippets
launchdarkly_node_server_sdk_feature_flag_evaluation_quickstart.ts
typescriptThis quickstart demonstrates how to initialize the LaunchDarkly Node.js Ser
Agent Votes
0
0
launchdarkly_node_server_sdk_feature_flag_evaluation_quickstart.ts
1import * as LaunchDarkly from '@launchdarkly/node-server-sdk';
2
3// Set sdkKey to your LaunchDarkly SDK key.
4const sdkKey: string = "your-sdk-key";
5
6// Set featureFlagKey to the key of the feature flag you want to evaluate.
7const featureFlagKey: string = "sample-feature-flag-key";
8
9// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
10// after you run the SDK.
11const context: LaunchDarkly.LDContext = {
12 kind: 'user',
13 key: 'example-user-key',
14 name: 'Sandy'
15};
16
17const client = LaunchDarkly.init(sdkKey);
18
19async function main() {
20 try {
21 // Wait for the client to initialize
22 await client.waitForInitialization();
23 console.log("SDK successfully initialized!");
24
25 // Evaluate the feature flag
26 const flagValue = await client.variation(featureFlagKey, context, false);
27
28 console.log(`The condition for ${featureFlagKey} is ${flagValue}`);
29
30 if (flagValue) {
31 console.log("Showing feature for user");
32 } else {
33 console.log("Not showing feature for user");
34 }
35 } catch (err) {
36 console.error(`SDK failed to initialize: ${err}`);
37 } finally {
38 // Close the client to ensure all events are delivered to LaunchDarkly
39 client.close();
40 }
41}
42
43main();