For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • SDKs
    • SDK concepts
    • SDK features
    • Client-side SDKs
    • Server-side SDKs
      • .NET SDK reference
      • Apex SDK reference
      • C++ SDK reference
      • Erlang SDK reference
      • Go SDK reference
      • Haskell SDK reference
      • Java SDK reference
      • Lua SDK reference
        • Lua SDK 1.x to 2.0 migration guide
      • Node.js SDK reference
      • PHP SDK reference
      • Python SDK reference
      • Ruby SDK reference
      • Rust SDK reference
    • AI SDKs
    • Edge SDKs
    • OpenFeature providers
    • Observability SDKs
    • Relay Proxy
Sign inTry it free
LogoLogo
On this page
  • Overview
  • Get started
  • Install the SDK
  • Initialize the client
  • Evaluate a context
  • Supported features
SDKsServer-side SDKs

Lua SDK reference

Was this page helpful?
Previous

Lua (server-side) SDK 1.x to 2.0 migration guide

Next
Built with

Overview

This topic documents how to get started with the Lua SDK, and links to reference information on all of the supported features. We also provide documentation for running the SDK in NGINX and HAProxy.

SDK quick links

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

ResourceLocation
SDK API documentationSDK API docs
Supported SDK VersionsLua server SDK
GitHub repositorylua-server-sdk
Sample applicationsLua
Lua with HAProxy
Lua with OpenResty NGINX
Published moduleLuaRocks

Get started

After you complete the Getting Started process, follow these steps to get started using the LaunchDarkly SDK in your Lua application.

The Lua server-side SDK is implemented using a foreign function interface that calls the C++ server-side SDK. You need to install the C++ server-side SDK dynamic library somewhere accessible by the linker.

To learn more, read C++ SDK reference (server-side).

Install the SDK

First, include the library:

Lua
1local ld = require("launchdarkly_server_sdk")
The Lua SDK uses an SDK key

The Lua 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.

Initialize the client

After you install and import the SDK, create a single, shared instance of LDClient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

LDClient must be a singleton

It’s important to make LDClient a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

If you have multiple LaunchDarkly projects, you can create one LDClient for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.

Calling clientInit initiates a remote call to the LaunchDarkly service to fetch feature flags. This call blocks up for the time that you provide in milliseconds. If you request a feature flag before the client completes initialization, you will receive the default flag value.

Here is an example:

Lua
1-- This will block for 1 second
2local client = ld.clientInit("YOUR_SDK_KEY", 1000, config)

To learn more about the specific configuration properties that are available in this SDK, read clientInit.

Evaluate a context

You can use client to check which variation a particular context will receive for a given feature flag.

Here’s how:

1if client:boolVariation(context, "example-flag-key", false) then
2 print "feature is enabled"
3else
4 print "feature is disabled"
5end

Supported features

This SDK supports the following features:

  • Aliasing users
  • Anonymous contexts and users
  • Configuration, including
    • Application metadata configuration
    • Service endpoint configuration
  • Context configuration
  • Evaluating flags
  • Flag evaluation reasons
  • Flushing events
  • Getting all flags
  • Identifying and changing contexts
  • Logging
  • Offline
  • Private attributes
  • Relay Proxy configuration
    • Using proxy mode
    • Using daemon mode
  • Sending custom events
  • Storing data