On this page

Legacy Erlang/Elixir Server SDK

Statsig's Legacy Server SDK for Erlang and Elixir applications

Support for the Legacy Erlang SDK ends April 30, 2026. Migrate to the

new Elixir SDK

soon.

The erlang SDK repository, and this docs site, are a work in progress. If you are trying to use Statsig in erlang or elixir, reach out to the support team, your sales contact, or in the Slack community.
Github Repository

Setup the SDK

  1. Install the SDK

    Add a dependency on statsig to your:

    mix.exs:

    elixir
    {:statsig, "~> 0.0.1"}
    

    rebar.config:

    erlang
    {statsig, "0.0.1"}.
    

    erlang.mk:

    makefile
    dep_statsig = hex 0.0.1
    
  2. Initialize the SDK

    After installation, initialize the SDK using a Server Secret Key from the Statsig console.

    Do NOT embed your Server Secret Key in client-side applications, or expose it in any external-facing documents. If you accidentally expose it, you can create a new one in the Statsig console.

    statsig:initialize(<<"secret-key">>).
    
    % or with options
    Options = #{environment => #{tier => <<"staging">>}},
    statsig:initialize(<<"secret-key">>, Options).
    
    initialize performs a network request. After initialize completes, virtually all SDK operations are synchronous (refer to Evaluating Feature Gates in the Statsig SDK). The SDK fetches updates from Statsig in the background, independently of API calls.

Working with the SDK

Checking a Feature Flag/Gate

After the SDK is initialized, you can check a Feature Gate. Feature Gates create logic branches in code that can be rolled out to different users from the Statsig Console. Gates are always CLOSED or OFF (return false;) by default.All APIs require you to specify the user (refer to Statsig user) associated with the request. For example, to check a gate for a user:
User = #{<<"userID">> => <<"user-id">>},
GateValue = statsig:check_gate(User, <<"gate_name">>).

Reading a Dynamic Config

Feature Gates work well for simple on/off switches with optional user targeting. To send a different set of values (strings, numbers, and so on) to clients based on specific user attributes such as country, use Dynamic Configs. The Dynamic Config API is similar to Feature Gates, but returns a full JSON object configured on the server, from which you can fetch typed parameters.
Config = statsig:get_config(User, <<"config_name">>),
Value = statsig_config:get(Config, <<"param">>, <<"default">>).

Getting a Layer/Experiment

Use Layers/Experiments to run A/B/n experiments. Two APIs are available, but Statsig recommends layers for faster iterations with parameter reuse.
Layer = statsig:get_layer(User, <<"layer_name">>),
ParamValue = statsig_layer:get(Layer, <<"param">>, <<"default">>).

Logging an Event

To track custom events and measure how features or experiment groups affect those events, call the Log Event API. Specify the user and event name to log, and optionally provide a value and metadata object:

statsig:log_event(User, <<"event_name">>).
For more about identifying users, group analytics, and best practices, go to the logging events guide.

Statsig User

When calling APIs that require a user, pass as much information as possible to take advantage of advanced gate and config conditions (like country or OS/browser level checks), and to correctly measure the impact of your experiments on your metrics/events. At least one identifier (userID or customID) is required to provide a consistent experience for a given user. Refer to userID requirements for more detail.

In addition to userID, email, ip, userAgent, country, locale, and appVersion are available as top-level fields on StatsigUser. You can also pass any key-value pairs in an object/dictionary to the custom field to create targeting based on them.

Typing on the StatsigUser object is lenient: you can pass numbers, strings, arrays, objects, and even enums or classes. However, evaluation operators only work on primitive types, mostly strings and numbers. The SDK attempts to cast custom field types to match the operator, but evaluation results for other types are not guaranteed. For example, an array set as a custom field is only compared as a string: there is no operator to match a value within that array.

Private Attributes

To keep sensitive user PII data out of logs, use the privateAttributes field on the StatsigUser object. This field accepts an object/dictionary of private user attributes. Any attribute set in privateAttributes is used only for evaluation/targeting and is removed from all logs before Statsig sends them to its servers.

For example, if a feature gate should only pass for users with emails ending in "@statsig.com", but you don't want to log email addresses to Statsig, add the key-value pair { email: "my_user@statsig.com" } to privateAttributes on the user.

Shutdown

To gracefully shutdown the SDK and ensure all events are flushed:

statsig:shutdown().

Was this helpful?