> ## 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.

# Apex SDK reference

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic documents how to get started with the Apex 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>
    <thead>
      <tr>
        <th>Resource</th>
        <th>Location</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>SDK API documentation</td>
        <td>[SDK API docs](https://github.com/launchdarkly/apex-server-sdk/blob/master/doc.md)</td>
      </tr>

      <tr>
        <td>Supported SDK Versions</td>
        <td>[Apex server SDK](/docs/sdk/concepts/supported-versions#apex-server-sdk)</td>
      </tr>

      <tr>
        <td>GitHub repository</td>
        <td>[apex-server-sdk](https://github.com/launchdarkly/apex-server-sdk)</td>
      </tr>

      <tr>
        <td>Sample application</td>
        <td>[Apex](https://github.com/launchdarkly/hello-apex-server)</td>
      </tr>

      <tr>
        <td>Published module</td>
        <td>None</td>
      </tr>
    </tbody>
  </table>
</Note>

## Get started

After you complete the [Get started process](/docs/home/getting-started), follow these instructions to start using the LaunchDarkly SDK in your Salesforce Apex application.

### Deploy the SDK to Salesforce

The first step is deploying the SDK to Salesforce:

<CodeGroup>
  ```bash title="Console" lines wrap theme={null}
  git clone https://github.com/launchdarkly/apex-server-sdk.git
  cd apex-server-sdk
  sf project deploy start --target-org 'YOUR TARGET ORG' --source-dir 'force-app'
  ```
</CodeGroup>

### Initialize the client

Initialize the client as follows:

<CodeGroup>
  ```java title="Default Config" lines wrap theme={null}
  LDClient client = new LDClient();
  ```

  ```java title="Custom Config" lines wrap theme={null}
  LDConfig config = new LDConfig.Builder()
      .setAllAttributesPrivate(true)
      .build();
  LDClient client = new LDClient(config);
  ```
</CodeGroup>

To learn more about the specific configuration options available for this SDK, read [`LDConfig.Builder`](https://github.com/launchdarkly/apex-server-sdk/blob/master/doc.md#class-ldconfigbuilder).

## Evaluate a user

You can use the `Variation` methods available from the client to check which variation a particular user will receive for a given feature flag.

Here's how:

<CodeGroup>
  ```java title="Apex" lines wrap theme={null}
  LDUser user = new LDUser.Builder('example-user-key')
      .setName('Sandy')
      .build();

  Boolean value = client.boolVariation(user, flagKey, false);
  if (value) {
      // Application code to show the feature
  } else {
      // The code to run if the feature is off
  }
  ```
</CodeGroup>

After the SDK is deployed to Salesforce, start the bridge to begin evaluating flags. The bridge must be running in order to receive flag updates and publish events to LaunchDarkly.

## Use the LaunchDarkly Salesforce bridge

The Apex server-side SDK is architected differently than our other SDKs. In most of our SDKs, the SDK downloads feature configurations and sends events by itself. The Apex SDK instead uses an [external bridging application](https://github.com/launchdarkly/apex-server-sdk/tree/main/bridge) to connect LaunchDarkly and Salesforce.

Because the SDK uses a bridge to handle state management, there is no initialization delay required to download flags. Additionally, the lack of state inside the SDK means that initializing multiple instances of the SDK is not problematic.

The Apex SDK exposes two HTTP endpoints that the bridge uses: `/store` and `/event`. The bridge pushes flag updates to Salesforce through one endpoint, and pulls events from Salesforce with the other.

### Install the bridge

The bridge is designed to be run as a [daemon](https://en.wikipedia.org/wiki/Daemon_\(computing\)) on your server infrastructure, either with a cloud provider or your own on-premise infrastructure.

The bridge is a Go application configured with environment variables. To build the bridge from source install Go 1.14 or higher. To install Go, read [Go's documentation](https://golang.org/doc/install).

The bridge needs authorization for both LaunchDarkly and Salesforce.

<Note>
  **The Apex SDK uses an SDK key**

  The Apex SDK uses an SDK key. Keys are specific to each project and environment. They are available 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).
</Note>

Build and run the bridge like this:

<CodeGroup>
  ```bash title="Console" lines wrap theme={null}
  cd bridge && go build .

  export LD_SDK_KEY='an active LaunchDarkly SDK key'
  export SALESFORCE_URL='Your Salesforce Apex REST URL'
  export OAUTH_ID='Your Salesforce OAuth Id'
  export OAUTH_SECRET='Your Salesforce OAuth secret'
  export OAUTH_USERNAME='Your Salesforce username'
  export OAUTH_PASSWORD='Your Salesforce password + security token'

  ./bridge
  ```
</CodeGroup>

The logs indicate if the bridge is running. If it fails, the logs show the errors that occurred.

To learn more about possible bridge errors and how to remediate them, read the following troubleshooting articles:

* [Error "invalid\_client\_id" Apex SDK](https://support.launchdarkly.com/hc/en-us/articles/20832735937051-Error-invalid-client-id-Apex-SDK)
* [Error "invalid\_client" Apex SDK](https://support.launchdarkly.com/hc/en-us/articles/20832763464987-Error-invalid-client-Apex-SDK)
* [Error "invalid\_grant" Apex SDK](https://support.launchdarkly.com/hc/en-us/articles/20832928003355-Error-invalid-grant-Apex-SDK)

You must deploy the SDK to Salesforce before you run the bridge. After the SDK is deployed to Salesforce, start the bridge to begin evaluating flags. The bridge must be running in order to receive flag updates and publish events to LaunchDarkly.

## Supported features

This SDK supports the following features:

* [SDK configuration](/docs/sdk/features/config#apex), including
  * [Service endpoint configuration](/docs/sdk/features/service-endpoint-configuration#apex)
* [Aliasing users](/docs/sdk/features/aliasing-users#apex)
* [Anonymous contexts and users](/docs/sdk/features/anonymous#apex)
* [Context configuration](/docs/sdk/features/context-config#apex)
* [Flag variation evaluation](/docs/sdk/features/evaluating#apex)
* [Flag variation evaluation details](/docs/sdk/features/evaluation-reasons#apex)
* [Getting all flags](/docs/sdk/features/all-flags#apex)
* [Identifying and changing contexts](/docs/sdk/features/identify#apex)
* [Private attributes](/docs/sdk/features/private-attributes#apex)
* [Tracking custom events](/docs/sdk/features/events#apex)
* [Web proxy configuration](/docs/sdk/features/web-proxy#apex)
