> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Fastly SDK reference

<View title="Developer" />

<View title="Federal docs">
  <Danger>
    **The Fastly SDK is not available in federal environments**

    To learn more, read <a href="/docs/home/infrastructure/federal">LaunchDarkly in federal environments</a>.
  </Danger>
</View>

<View title="EU docs">
  <Danger>
    **The Fastly SDK is not available in LaunchDarkly's European Union (EU) instance**

    To learn more, read <a href="/docs/home/infrastructure/eu">LaunchDarkly in the European Union (EU)</a>.
  </Danger>
</View>

<Note>
  **This SDK is in beta**

  Development work on the Fastly SDK is ongoing. The SDK is fully tested and we consider it production-ready, however, we are actively soliciting feedback on its usage. Some external APIs could change prior to the v1 release.
</Note>

This topic documents how to get started with the Fastly SDK, and links to reference information on all of the supported features.

<Note>
  **SDK quick links**

  LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

  <table className="fern-table">
    <thead>
      <tr>
        <th>Resource</th>
        <th>Location</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>SDK API documentation</td>

        <td>
          [SDK API docs](https://launchdarkly.github.io/js-core/packages/sdk/fastly/docs/)
        </td>
      </tr>

      <tr>
        <td>Supported SDK Versions</td>
        <td>[Fastly SDK](/docs/sdk/concepts/supported-versions#fastly-sdk)</td>
      </tr>

      <tr>
        <td>GitHub repository</td>

        <td>
          [LaunchDarkly Fastly SDK](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/fastly)
        </td>
      </tr>

      <tr>
        <td>Sample application</td>

        <td>
          [Example app](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/fastly/example)
        </td>
      </tr>

      <tr>
        <td>Published module</td>
        <td>[npm](https://www.npmjs.com/package/@launchdarkly/fastly-server-sdk)</td>
      </tr>
    </tbody>
  </table>
</Note>

<Note>
  **This SDK requires LaunchDarkly's Fastly integration**

  Configure the Fastly integration to use this SDK successfully. To learn more, read [Fastly](/docs/integrations/fastly).

  The Fastly integration is only available to customers on select plans. To learn more, [read about our pricing](https://launchdarkly.com/pricing/). To upgrade your plan, [contact Sales](https://launchdarkly.com/contact-sales/).
</Note>

## Get started

After you complete the [Create a Fastly integration](/docs/integrations/fastly#create-a-fastly-integration) process in an [existing project](/docs/home/getting-started), follow these instructions to start using the LaunchDarkly Fastly SDK:

* [Install the SDK](#install-the-sdk)
* [Initialize the client](#initialize-the-client)
* [Evaluate a flag](#evaluate-a-flag)

### Install the SDK

First, install the Fastly SDK as a dependency in your application using your application's dependency manager.

Here's how:

<CodeGroup>
  ```bash title="yarn" lines wrap theme={null}
  yarn add @launchdarkly/fastly-server-sdk
  ```

  ```bash title="npm" lines wrap theme={null}
  npm i @launchdarkly/fastly-server-sdk
  ```
</CodeGroup>

Next, import the LaunchDarkly client in your application code:

<CodeGroup>
  ```ts title="Import" lines wrap theme={null}
  import { KVStore } from 'fastly:kv-store';
  import { init } from '@launchdarkly/fastly-server-sdk';
  ```
</CodeGroup>

### Initialize the client

After you install and import the SDK, [configure a Fastly KV Store](/docs/integrations/fastly#configure-a-fastly-kv-store-for-launchdarkly). Then, initialize an `LDClient` using your LaunchDarkly client-side ID, the Fastly KV store name, and the Fastly events backend name. The client must be initialized when processing requests, not during build-time initialization.

Here's how:

<CodeGroup>
  ```ts title="Initialize the client" lines wrap theme={null}
  const KV_STORE_NAME = 'launchdarkly';
  const EVENTS_BACKEND_NAME = 'launchdarkly';
  const store = new KVStore(KV_STORE_NAME);

  addEventListener('fetch', (event: FetchEvent) => {
    event.respondWith(handleRequest(event));
  });

  async function handleRequest(event: FetchEvent): Promise<Response> {
    const ldClient = init('example-client-side-id', store, {
      eventsBackendName: EVENTS_BACKEND_NAME,
    });

    await ldClient.waitForInitialization({ timeout: 5 });

    // The rest of your handler code goes here
    return new Response('OK'); // Replace with your actual response logic
  }
  ```
</CodeGroup>

<Warning>
  **Fastly SDK credentials**

  The Fastly SDK requires a client-side ID. Client-side IDs are specific to each project and environment. They are not secret, and you can include them in code. Do not embed a server-side SDK key into an edge application.

  You can find client-side IDs on the **SDK keys** page under **Settings**. To learn more about key types, read [Keys](/docs/sdk/concepts/client-side-server-side#keys-and-credentials).
</Warning>

Events are enabled by default in the Fastly SDK, which means the Fastly SDK supports [Experimentation](/docs/home/experimentation) and [metrics](/docs/home/metrics) use cases by default. To learn more, read [Experimentation and metric events](/docs/home/experimentation/events).

### Evaluate a flag

After you initialize the client, wait for the `waitForInitialization` function to resolve. When `waitForInitialization` is resolved, the client can serve feature flags.

Using the client, you can check which variation a particular context will receive for a given feature flag. In your Fastly application, place the `variation` code so that it is invoked as needed.

Here is an example:

<CodeGroup>
  ```ts title="Evaluate a flag" lines wrap theme={null}
  import type { LDContext } from '@launchdarkly/fastly-server-sdk';

  // ldClient is the LDClient instance created in the previous step
  const ldContext: LDContext = {
    kind: 'org',
    key: 'example-organization-key',
    someAttribute: 'example-attribute-value',
  };
  const flagValue = await ldClient.variation('example-flag-key', ldContext, false);
  ```
</CodeGroup>

The SDK caches all KV data during initialization to reduce the number of backend requests needed to fetch KV data. This means changes to feature flags or segments will not be picked up during the lifecycle of a single request instance.

## Flush events

You must flush events before your worker exits to ensure that they are sent back to LaunchDarkly. To learn more, read [Flushing events](/docs/sdk/features/flush#fastly).

## Supported features

* [SDK configuration](/docs/sdk/features/config#fastly)
* [Anonymous contexts and users](/docs/sdk/features/anonymous#fastly)
* [Context configuration](/docs/sdk/features/context-config#fastly)
* [Flag variation evaluation](/docs/sdk/features/evaluating#fastly)
* [Flag variation evaluation details](/docs/sdk/features/evaluation-reasons#fastly)
* [Flushing events](/docs/sdk/features/flush#fastly)
* [Getting all flags](/docs/sdk/features/all-flags#fastly)
* [Logging configuration](/docs/sdk/features/logging#fastly)
* [Secure mode](/docs/sdk/features/secure-mode#fastly)
* [Tracking custom events](/docs/sdk/features/events#fastly)
