File-based flags work in regulated or connectivity-restricted environmentsFile-based flags work in production for organizations with compliance-driven restrictions on outbound connections to LaunchDarkly servers. Outside of these cases, use file-based flags for testing, prototyping, or local development.If you use flags from files in production, you must propagate flag changes to the file yourself whenever you change a flag in the LaunchDarkly UI. LaunchDarkly does not sync the file for you.
Reading flags from files is only available for server-side SDKsIf you want to test client-side SDKs, you can use test data sources or our LaunchDarkly client testing plugin. To learn more, read Testing code that uses feature flags.
Data saving mode can also load flags from a fileThe newer
dataSystem configuration, also known as data saving mode, can load flags from a file by configuring a file initializer. A file initializer reads the same file format described on this page, including the flags and flagValues formats. To learn more, read Data saving mode.Newer versions of LaunchDarkly SDKs replace users with contextsA context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: “users.” To learn more, read Contexts.Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Create a flag data file
Flag data files can be either JSON or YAML. They contain up to three properties:flags: These are feature flag definitions. These can contain all the same kinds of rules and targets that you can define in a LaunchDarkly feature flag, which allows the flag to produce different values for different contexts or users.flagValues: These are simplified feature flags that specify only a value, and produce the same value for all contexts or users.segments: These are segment definitions. You only use this property if you have feature flags that use segments.
Only standard segments are supported in filesOnly rule-based segments and smaller list-based segments are supported in the file definition. Synced segments and larger list-based segments, collectively known as big segments, cannot be read from files. To learn more, read Segments.
flags and segments is defined by the LaunchDarkly application and is subject to change. Rather than trying to construct these objects yourself, it’s simpler to request existing flags directly from the LaunchDarkly server in JSON format and use this output as the starting point for your file.
Get the flags from
For instance, in your terminal, you could use this command:
https://sdk.launchdarkly.com/sdk/latest-all, or https://sdk.launchdarkly.com/sdk/poll if using data saving mode. Pass your SDK key in the Authorization header.flags and flagValues. However, it generates an error if you use the same flag key or segment key more than once, either in a single file or across multiple files.
Configure the client to use a file
You can specify either a single file or multiple files. In all of the SDKs that support this feature except Haskell, PHP, and C++, you can also specify whether the SDK should reload the file data if it detects that you have modified a file. For example, you could verify that your application behaves correctly when a flag changes. The examples below show how to configure the client to use two data files calledfile1.json and file2.json. The client assumes these two files are in the current working directory, but you can specify any relative or absolute file path. It also enables automatic reloading, if supported.
If you do not want your code to connect to LaunchDarkly at all, you must also prevent the SDK from sending analytics events. We’ve included the option to disable events in these examples.
Because there is no connection to LaunchDarkly, you do not have to use a valid SDK key. The SDK key parameter is still required, but you can use any string.
If any of the specified files is missing or invalid, the SDK does not use any of the file data and logs an error message instead.
Server-side SDKs
This feature is available in the following server-side SDKs:.NET (server-side)
Expand .NET (server-side) code sample
Expand .NET (server-side) code sample
To configure the client:If you want to use YAML instead of JSON, you must provide a YAML parser.To learn more, read
FileData.Erlang
Expand Erlang code sample
Expand Erlang code sample
To configure the client:
Go
Expand Go code sample
Expand Go code sample
Haskell
Expand Haskell code sample
Expand Haskell code sample
To configure the client:
Java
Expand Java code sample
Expand Java code sample
Node.js (server-side)
Expand Node.js (server-side) code sample
Expand Node.js (server-side) code sample
PHP
Expand PHP code sample
Expand PHP code sample
Python
Expand Python code sample
Expand Python code sample
To configure the client:In the Python SDK, if you want to use YAML files instead of JSON you must install the
pyyaml package. Also, automatic reloading uses an inefficient file-polling mechanism. We recommend installing the watchdog package.To learn more, read Files.Ruby
Expand Ruby code sample
Expand Ruby code sample
To configure the client:In the Ruby SDK, automatic reloading uses an inefficient file-polling mechanism unless you install the
listen gem.To learn more, read FileData.