No results for ""
EXPAND ALL
  • Home
  • API docs

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.

Do not use file-based flag values in production environments

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.

Reading flags from files is only available for server-side SDKs

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.

Newer versions of LaunchDarkly SDKs replace users with contexts

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 standard segments are supported in files

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.

YAML files have limitations

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:

The output looks like this, but with many more properties:

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:

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:

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:

To learn more, read LDFileDataInit.

Erlang

Expand Erlang code sample

To configure the client:

Go

Expand Go code sample

Haskell

Expand Haskell code sample

To configure the client:

Java

Expand Java code sample

To configure the client:

To learn more, read FileData.

Node.js (server-side)

Expand Node.js (server-side) code sample

To configure the client:

To learn more, read FileDataSourceFactory.

PHP

Expand PHP code sample

To configure the client:

To learn more, read Files.

Python

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

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.