Skip to Content
SDKJavaInitialization

Constructor

import com.canarygate.CanaryGateClient; import com.canarygate.Options; CanaryGateClient client = new CanaryGateClient("cg_api_key", new Options());

Parameters

ParameterTypeRequiredDescription
apiKeyStringYesEnvironment API Key. Created under Settings → API Keys.
optionsOptionsNoConfiguration options. See below.

Options

OptionTypeDefaultDescription
baseUrlStringhttp://localhost:3001API base URL (useful for self-hosted environments or proxies).
environmentStringdefault environmentEnvironment slug, sent via the X-Environment header.
streambooleanfalseOpt in to real-time SSE streaming.
reconnectDelayMslong5000Initial delay in ms before reconnecting a dropped stream.
maxReconnectDelayMslong30000Maximum backoff delay in ms for stream reconnection.
heartbeatTimeoutMslong65000Time in ms without a server heartbeat before the stream is considered dead and reconnects.

init()

client.init();

Executes two actions in sequence:

  1. Makes an HTTP fetch to load all flags for the environment (snapshot)
  2. Opens the SSE connection — only when stream is enabled. Otherwise flags come from the snapshot request only.

After a successful init(), getFlag() reads from the local cache; if the connection degrades later, isStale() reports it and getFlag() continues to return the last known values.

Examples

Spring Boot (singleton with streaming)

import com.canarygate.CanaryGateClient; import com.canarygate.Options; import org.springframework.stereotype.Service; @Service public class CanaryGateService implements AutoCloseable { private final CanaryGateClient client; public CanaryGateService() { this.client = new CanaryGateClient( System.getenv("CANARYGATE_KEY"), new Options( "https://flags.canarygate.dev", "production", true, 5000, 30000, 65000 ) ); this.client.init(); } public boolean isNewCheckoutEnabled(String userId) { var flag = client.getFlag("new-checkout", new FlagEvaluationContext(userId)); return flag != null && flag.isEnabled(); } @Override public void close() { client.disconnect(); } }

One-shot script (no stream)

import com.canarygate.CanaryGateClient; var client = new CanaryGateClient("cg_api_key"); client.init(); var flag = client.getFlag("run-migration", null); if (flag != null && flag.isEnabled()) { runMigration(); } // Disconnect (closes any active stream) so the process can exit client.disconnect();
Last updated on