This library can be used with server-side Java, Kotlin and Android applications. The source code for the client is available on Github.
The client library is available from the Central Maven Repository and can be added to your project by many tools:
Add following dependencies to your project in
The SDK is initialised against a single environment within a project on https://flagsmith.com, for example the Development or Production environment. You can find your environment key in the Environment settings page.
Sign Up and create an account at https://www.flagsmith.com/
In your application initialise the Flagsmith client with your API key:
To check if a feature flag exists and is enabled:
To get configuration value for a feature flag:
Identifying users allows you to target specific users from the Flagsmith dashboard.
To check if feature exists for given a user context:
To get the configuration value of a feature flag for a given user context:
To set user traits:
To get user traits for a given user context:
To get a user trait for a given user context and specific key:
Or get the user traits for a given user context and specific keys:
To update the value for user traits for a given user context and specific keys:
Or get flags and traits for a user in a single call:
By default, the client uses a default configuration. You can override the configuration as follows:
Override just the default API URI with your own:
Override the full configuration with your own
Logging is disabled by default. If you would like to enable it then call
.enableLogging() on the client builder:
Flagsmith uses SLF4J and we only implement its API. If your project does not already have SLF4J, then include an implementation, i.e.:
adding custom headers to all HTTP calls:
Caching was made available in version 2.6+
If you would like to use in-memory caching, you will need to enable it (it is disabled by default). The main advantage of using in-memory caching is that you can reduce the number of HTTP calls performed to fetch flags.
Flagsmith uses Caffeine, a high performance, near optimal caching library.
If you enable caching on the Flagsmith client without setting any values (as shown below), the following default values will be set for you:
- expireAfterWrite(5, TimeUnit.MINUTES)
If you would like to change the default settings, you can overwrite them by using the available builder methods:
The user identifier is used as the cache key, this provides granular control over the cache should you require it. If you would like to manipulate the cache:
The following API calls do not currently support caching:
getFeatureFlagValue(...)if the user parameter is null.