Skip to main content

Evaluate Flaggr feature flags from Java with the OpenFeature Java SDK and its OFREP provider, or call the REST API directly

Last updated November 21, 2026

Java SDK

Java services evaluate Flaggr flags with the OpenFeature Java SDK and its OFREP provider, which talks to Flaggr's OFREP endpoint. You can also call the REST API directly.

No native package

Flaggr doesn't publish a Java package or generated gRPC stubs (there is no dev.flaggr artifact on Maven Central). Use the OpenFeature provider below.

Installation

The provider uses java.net.http, so it needs Java 11 or later, and OpenFeature Java SDK 1.16 or later.

Maven

<dependencies>
  <dependency>
    <groupId>dev.openfeature</groupId>
    <artifactId>sdk</artifactId>
    <version>1.23.0</version>
  </dependency>
  <dependency>
    <groupId>dev.openfeature.contrib.providers</groupId>
    <artifactId>ofrep</artifactId>
    <version>0.0.2</version>
  </dependency>
</dependencies>

Gradle

dependencies {
    implementation 'dev.openfeature:sdk:1.23.0'
    implementation 'dev.openfeature.contrib.providers:ofrep:0.0.2'
}

Quick Start

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import dev.openfeature.contrib.providers.ofrep.OfrepProvider;
import dev.openfeature.contrib.providers.ofrep.OfrepProviderOptions;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;
 
public class FlaggrExample {
    public static void main(String[] args) {
        OfrepProviderOptions options = OfrepProviderOptions.builder()
                .baseUrl("https://api.flaggr.dev/api")
                .headers(ImmutableMap.of(
                        "Authorization", ImmutableList.of("Bearer " + System.getenv("FLAGGR_TOKEN")),
                        "X-Service-Id", ImmutableList.of("web-app")))
                .build();
 
        OpenFeatureAPI api = OpenFeatureAPI.getInstance();
        api.setProviderAndWait(OfrepProvider.constructProvider(options));
        Client client = api.getClient();
 
        boolean enabled = client.getBooleanValue("checkout-v2", false, new ImmutableContext("user-123"));
        System.out.printf("checkout-v2 = %s%n", enabled);
    }
}

Three settings connect the provider to Flaggr:

  • Base URL https://api.flaggr.dev/api. The provider appends /ofrep/v1/evaluate/flags/{key} itself, so a base URL ending in /ofrep or /ofrep/v1 returns 404.
  • An Authorization header with the API token as a bearer token. Read it from the environment (FLAGGR_TOKEN here). A read-only project API token is enough to evaluate flags.
  • An X-Service-Id header naming your service.

headers takes Guava's ImmutableMap of ImmutableList values; Guava comes with the provider.

Environments

Without an X-Environment header, flags evaluate in development. Add the header to choose another environment:

.headers(ImmutableMap.of(
        "Authorization", ImmutableList.of("Bearer " + System.getenv("FLAGGR_TOKEN")),
        "X-Service-Id", ImmutableList.of("web-app"),
        "X-Environment", ImmutableList.of("production")))

Evaluating Flags

import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.Value;
import java.util.Map;
 
EvaluationContext ctx = new ImmutableContext("user-456", Map.of(
        "plan", new Value("enterprise"),
        "country", new Value("AU")));
 
boolean darkMode = client.getBooleanValue("dark-mode", false, ctx);
String color = client.getStringValue("button-color", "blue", ctx);
int limit = client.getIntegerValue("rate-limit", 100, ctx);
double ratio = client.getDoubleValue("sample-rate", 0.25, ctx);
Value banner = client.getObjectValue("banner-config", new Value(), ctx);

Each evaluation is one HTTP request to Flaggr's data plane.

Full Details

The get…Details methods return the value along with the reason, the variant and any error:

import dev.openfeature.sdk.FlagEvaluationDetails;
 
FlagEvaluationDetails<Boolean> details = client.getBooleanDetails("checkout-v2", false, ctx);
System.out.printf("value=%s reason=%s variant=%s error=%s%n",
        details.getValue(), details.getReason(), details.getVariant(), details.getErrorCode());

Error Handling

When the flag doesn't exist, the token is rejected or Flaggr can't be reached, the value methods return your default value; they don't throw. The get…Details methods report what went wrong in getErrorCode() and getErrorMessage().

OfrepProviderOptions.builder() also takes requestTimeout and connectTimeout (Duration, 10 seconds each by default).

REST API Direct Usage

To avoid dependencies, call the REST API with java.net.http:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
 
String body = """
        {"flagKey": "checkout-v2", "serviceId": "web-app", "environment": "production",
         "defaultValue": false, "context": {"targetingKey": "user-123"}}
        """;
 
HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.flaggr.dev/api/flags/evaluate"))
        .header("Authorization", "Bearer " + System.getenv("FLAGGR_TOKEN"))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();
 
HttpResponse<String> response = HttpClient.newHttpClient()
        .send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body()); // {"flagKey":"checkout-v2","value":true,"reason":"TARGETING_MATCH",...}

The text block (""") needs Java 15 or later; on older versions, build the JSON string another way.

Connect-RPC and gRPC

Flaggr's data plane also serves Connect-RPC and gRPC evaluation and streaming services. Generated Java stubs for them aren't published; see Protocols for the wire formats.