Skip to main content
Version: Next

JAVA SDK usage

Maven Central Version

Install dependencies

The first thing we will do is install the Open Feature SDK and the GO Feature Flag provider.


Initialize your Open Feature client

To evaluate the flag you need to have an Open Feature configured in your app. This code block shows you how you can create a client that you can use in your application.

import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProvider;
import dev.openfeature.contrib.providers.gofeatureflag.GoFeatureFlagProviderOptions;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.MutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;

// ...

GoFeatureFlagProviderOptions options = GoFeatureFlagProviderOptions.builder().endpoint("http://localhost:1031/").build();
GoFeatureFlagProvider provider = new GoFeatureFlagProvider(options);

OpenFeatureAPI api = OpenFeatureAPI.getInstance();
Client featureFlagClient = api.getClient();

Evaluate your flag

This code block explain how you can create an EvaluationContext and use it to evaluate your flag.


In this example we are evaluating a boolean flag, but other types are available.

Refer to the Open Feature documentation to know more about it.

// Context of your flag evaluation.
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
EvaluationContext userContext = new MutableContext("1d1b9238-2591-4a47-94cf-d2bc080892f1")
.add("firstname", "john")
.add("lastname", "doe")
.add("admin", true)
.add("anonymous", false);

Boolean adminFlag = featureFlagClient.getBooleanValue("flag-only-for-admin", false, userContext);
if (adminFlag) {
// flag "flag-only-for-admin" is true for the user
} else {
// flag "flag-only-for-admin" is false for the user

Contribute to the provider

You can find the source of the provider in the open-feature/java-sdk-contrib repository.

Get the latest GO Feature Flag updates