Set up JavaScript SDK

Install the package

$yarn add @launchdarkly/js-client-sdk

Initialize the SDK for feature management

JavaScript SDK initialization
1// A "context" is a data object representing users, devices, organizations, and
2// other entities. You'll need this later, but you can ignore it for now.
3const context = {
4 kind: 'user',
5 key: 'EXAMPLE_CONTEXT_KEY'
6};
7// This is your client-side ID.
8const client = createClient('YOUR_CLIENT_SIDE_ID', context);
9client.start();
10
11const { status } = await client.waitForInitialization();
12
13if (status === 'complete') {
14 console.log('SDK successfully initialized!');
15} else {
16 console.error('Initialization failed');
17}

Initialize the SDK for feature management and Experimentation

If you want to use the LaunchDarkly Experimentation feature, include a track call in your initialization code:

JavaScript SDK initialization for Experimentation
1import { createClient } from '@launchdarkly/js-client-sdk';
2
3// A "context" is a data object representing users, devices, organizations, and
4// other entities. You'll need this later, but you can ignore it for now.
5const context = {
6 kind: 'user',
7 key: 'EXAMPLE_CONTEXT_KEY',
8};
9
10// This is your client-side ID.
11const ldClient = createClient('YOUR_CLIENT_SIDE_ID', context);
12
13// await start — you only need waitForInitialization() if you wait somewhere
14// other than where you start the client.
15await ldClient.start();
16console.log('SDK successfully initialized!');
17
18// Call trackMetric when a metric action occurs in your app —
19// a click, a form submit, a page view, a custom event, whatever your metric measures.
20function trackMetric(metricKey, data) {
21 ldClient.track(metricKey, data);
22}

You can find your server-side SDK keys, mobile keys, and client-side ID on the SDK keys page under Settings.

To learn more, read Initialize the client in the JavaScript SDK reference guide.