Skip to main content
New: the Agentic AI Academy, 5 courses on running AI agents in production.Start free →

A flag is configuration with a lifecycle

This is the free lesson

Next: The default is mandatory

The rest of this course is part of Pro, together with every other course and the ones added later.

$25 a month billed yearly, or $29 month to month

Until 30 November: $210 for the first year, or $14.50 for the first month, with code LAUNCH.

A free account keeps your progress across devices. The paid lessons need Pro.

Code Snippets

flags.jsonjson
{
  "$schema": "https://flagd.dev/schema/v0/flags.json",
  "flags": {
    "v2_enabled": {
      "state": "ENABLED", "variants": { "on": true, "off": false }, "defaultVariant": "on"
    }
  }
}
Checkout.javajava
package demo;

import dev.openfeature.contrib.providers.flagd.Config;
import dev.openfeature.contrib.providers.flagd.FlagdOptions;
import dev.openfeature.contrib.providers.flagd.FlagdProvider;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.OpenFeatureAPI;

public class Checkout {
    public static void main(String[] args) {
        FlagdProvider provider = new FlagdProvider(FlagdOptions.builder()
                .resolverType(Config.Resolver.FILE)
                .offlineFlagSourcePath("flags.json")
                .build());

        OpenFeatureAPI api = OpenFeatureAPI.getInstance();
        api.setProviderAndWait(provider);

        Client client = api.getClient("checkout");
        boolean on = client.getBooleanValue("v2_enabled", false);
        System.out.println(on ? "checkout: new flow (v2)" : "checkout: old flow (v1)");
        api.shutdown();
    }
}
runbash
#!/bin/bash
# Compile, then run one class: ./run Checkout
set -e
mvn -q compile dependency:build-classpath -Dmdep.outputFile=cp.txt
java -cp "target/classes:$(cat cp.txt)" "demo.$1" "${@:2}"
Commands of this lessonbash
java -version 2>&1 | grep Runtime
mvn -v | head -1
find . -type f
grep -E '<(artifactId|version)>' pom.xml
mkdir -p src/main/java/demo
chmod +x run
./run Checkout
sed -i '' 's/"defaultVariant": "on"/"defaultVariant": "off"/' flags.json
./run Checkout
grep -rn v2_enabled src flags.json