Skip to main content
This topic explains how to define and add hooks to LaunchDarkly SDKs. Hooks are collections of developer-defined callbacks. They contain methods that the SDK executes at various points of interest. In LaunchDarkly SDKs, hooks provide entry points to observe or modify aspects of SDK operation. For example, when you enable OpenTelemetry in a server-side SDK, you add a tracing hook, provided by LaunchDarkly, that surfaces telemetry data. You might also write your own hook, for instance to support logging or reporting errors.

Hooks stages

LaunchDarkly SDKs provide the following series of hooks:
SeriesStagesExecution
EvaluationbeforeEvaluation, afterEvaluationWhen you include a hook in your SDK configuration, the SDK executes these two stages before and after you evaluate a feature flag.
IdentifybeforeIdentify, afterIdentifyWhen you include a hook in your SDK configuration, the SDK executes these two stages before and after you identify a context. Only available for client-side SDKs.
TrackafterTrackWhen you include a hook in your SDK configuration, the SDK executes this stage after you send a custom event.
Not all SDKs support all of these series. In particular, the identify series is only available for client-side SDKs. Details about how to add hooks to each SDK are available in the SDK-specific sections below.

Server-side SDKs

This feature is available in the following SDKs:

.NET (server-side)

To use hook functionality with the .NET (server-side) SDK, first import the LaunchDarkly.Sdk.Server.Hooks namespace. Then, define a new hook. This class must derive from the Hook class and override its methods. Finally, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
To learn more, read Hooks.

Go

To use hook functionality with the Go SDK, first import the ldhooks package. Then, define a new hook. It must implement the Hook interface. Finally, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
To learn more, read Hook.

Java

To use hook functionality with the Java SDK, first define a new hook. This class must implement the Hook abstract class. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
To learn more, read Hook.

Node.js (server-side)

To use hook functionality with the Node.js (server-side) SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.

PHP

To use hook functionality with the PHP SDK, first define a new hook. This class must extend the LaunchDarkly\Hooks\Hook abstract class and implement getMetadata. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read the SDK API documentation.

Python

To use hook functionality with the Python SDK, first define a new hook. This class must inherit from ldclient.hook.Hook. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the add_hook method:
To learn more, read ldclient.hook.If you are working with forking processes, any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any hooks after a postfork() call, unless you are certain they can survive the forking process. To learn more, read Initialize the client and ldclient.postfork.

Ruby

To use hook functionality with the Ruby SDK, first define a new hook. This class must include the Hooks mixin. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the add_hook method:
To learn more, read Hooks.If you are working with forking processes, any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any hooks after a postfork() call, unless you are certain they can survive the forking process. To learn more, read Initialize the client and postfork.

Client-side SDKs

This feature is available in the following SDKs:

Android

To use hook functionality with the Android SDK, first define a new hook. This class must extend the Hook class. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the setHooks method:
To learn more, read Hook and HooksConfigurationBuilder.

iOS

To use hook functionality with the iOS SDK, first define a new hook. This class must use the Hook protocol. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
To learn more, read Hook.

JavaScript

To use hook functionality with the JavaScript SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.

React Native

To use hook functionality with the React Native SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.

Vue

The Vue SDK relies on the JavaScript SDK for hooks. Define a hook that implements the Hook interface, then set the hooks option in ldOptions when you install the plugin or create a provider.Here’s how:
You can also add a hook later. Retrieve the client with useLDClient() and call addHook().To learn more about defining a hook, read JavaScript.