Reading flags from a file
Read time: 8 minutes
Last edited: May 30, 2024
Overview
This topic explains how to run feature flags from a file when you're using a server-side SDK.
Always configure production environments to receive flag updates from LaunchDarkly. Only use file-based flags in testing and pre-production environments, or in local development. We do not support using flags from files in a production environment.
If you use flags from files in a production environment, you need to propagate flag changes to the file whenever one of your flags is updated in the LaunchDarkly UI.
If you perform automated tests or prototyping, you might want to run application code that uses feature flags without connecting to LaunchDarkly. LaunchDarkly SDKs in offline mode return the fallback value for each flag evaluation. This is not a property of the flag. It is the value that you specified as a fallback in your program code when you evaluated the flag.
However, in some server-side SDKs, you can also use files to configure the feature flag state you desire.
If you are looking for other ways to test server-side SDKs, you can also use Test data sources.
If you are looking for ways to test client-side SDKs, consider Test data sources or Unit testing with Jest instead. To learn more, read Testing code that uses feature flags.
A 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 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.
In some of the SDKs, YAML support requires an additional dependency. YAML is not available in PHP or C++.
The format of the data in 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 https://sdk.launchdarkly.com/sdk/latest-all
. Pass your SDK key in the Authorization
header.
For instance, in your terminal, you could use this command:
curl -H "Authorization: sdk-key-123abc" https://sdk.launchdarkly.com/sdk/latest-all >flagdata.json
The output looks like this, but with many more properties:
{"flags": {"flag-key-1": {"key": "flag-key-1","on": true,"variations": [ "a", "b" ]}},"segments": {"segment-key-1": {"key": "segment-key-1","includes": [ "context-key-1" ]}}}
Data in this format lets the SDK exactly duplicate all the kinds of flag behavior LaunchDarkly supports. However, in most cases you do not need this level of complexity. You may want to simply set specific flag keys to specific values.
For that, you can use a much simpler format.
Here's how:
{"flagValues": {"my-string-flag-key": "value-1","my-boolean-flag-key": true,"my-integer-flag-key": 3}}
If you want some flags to have simple values and others to have complex behavior, you can specify both 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 called file1.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
To configure the client:
using LaunchDarkly.Sdk.Server;using LaunchDarkly.Sdk.Server.Integrations;var config = Configuration.Builder("sdk key").DataSource(FileData.DataSource().FilePaths("file1.json", "file2.json").AutoUpdate(true)).Events(Components.NoEvents()).Build()var client = new LDClient(config);
If you want to use YAML instead of JSON, you must provide a YAML parser.
To learn more, read FileData
.
C++ (server-side)
Expand C++ code sample
To configure the client:
#include <launchdarkly/integrations/file_data.h>const char *filenames[2] = {"file1.json","file2.json"};LDConfigSetDataSource(config, LDFileDataInit(2, filenames));LDConfigSetSendEvents(config, LDBooleanFalse);// Call LDClientInit with config as usual.
To learn more, read LDFileDataInit
.
Erlang
Expand Erlang code sample
To configure the client:
ldclient:start_instance("sdk-key-123abc", #{file_datasource => true,send_events => false,file_paths => ["file1.json", "file2.yaml"],feature_store => ldclient_storage_map,file_auto_update => true,file_poll_interval => 1000})%% In the Erlang SDK automatic reloading uses a polling mechanism.%% The default interval is 1000ms, but you can control it with%% the file_poll_interval configuration.
Go
Expand Go code sample
import ("time"ld "github.com/launchdarkly/go-server-sdk/v6""github.com/launchdarkly/go-server-sdk/v6/ldcomponents""github.com/launchdarkly/go-server-sdk/v6/ldfiledata""github.com/launchdarkly/go-server-sdk/v6/ldfilewatch")var config ld.Configconfig.DataSource = ldfiledata.DataSource().FilePaths("file1.json", "file2.json").Reloader(ldfilewatch.WatchFiles)config.Events = ldcomponents.NoEvents()client, _ := ld.MakeCustomClient("sdk key", config, 5*time.Second)
Haskell
Expand Haskell code sample
To configure the client:
let config = LD.configSetDataSourceFactory (Just $ FileData.dataSourceFactory ["./testData/flags.json"]) $ LD.makeConfig "sdk-key-123abc"client <- LD.makeClient config
Java
Expand Java code sample
To configure the client:
import com.launchdarkly.sdk.server.*;import com.launchdarkly.sdk.server.integrations.*;LDConfig config = new LDConfig.Builder().dataSource(FileData.dataSource().filePaths("file1.json", "file2.json").autoUpdate(true)).events(Components.noEvents()).build();LDClient client = new LDClient("sdk key", config);
To learn more, read FileData
.
Node.js (server-side)
Expand Node.js (server-side) code sample
To configure the client:
const ld = require('@launchdarkly/node-server-sdk');const { FileDataSourceFactory } = require('@launchdarkly/node-server-sdk/integrations');const fileData = new FileDataSourceFactory({paths: [ 'file1.json', 'file2.json' ]});const options = {updateProcessor: fileData.getFactory()};const client = ld.init('sdk-key-123abc', options);
To learn more, read FileDataSourceFactory
.
PHP
Expand PHP code sample
To configure the client:
// Automatic reloading is not supported in PHP, because normally in PHP// the entire in-memory application state is recreated for each request.$fr = LaunchDarkly\Integrations\Files::featureRequester(['file1.json','file2.json']);$client = new LaunchDarkly\LDClient("sdk-key-123abc", ['feature_requester' => $fr,'send_events' => false]);
To learn more, read Files
.
Python
Expand Python code sample
To configure the client:
import ldclientfrom ldclient.config import Configfrom ldclient.integrations import Filesdata_source_callback = Files.new_data_source(paths=["file1.json", "file2.json"],auto_update=True)config = Config('sdk-key-123abc', update_processor_class=data_source_callback, send_events=False)ldclient.set_config(config)client = ldclient.get()
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
To configure the client:
require 'ldclient-rb'data_source = LaunchDarkly::Integrations::FileData.data_source(paths: [ "file1.json", "file2.json" ],auto_update: true)config = LaunchDarkly::Config.new(data_source: data_source,send_events: false)client = LaunchDarkly::LDClient.new("sdk key", config)
In the Ruby SDK, automatic reloading uses an inefficient file-polling mechanism unless you install the listen
gem.
To learn more, read FileData
.