Skip to Content
SDKC# / .NETInitialization

Constructor

using CanaryGate; var 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.
StreamboolfalseOpt in to real-time SSE streaming.
ReconnectDelayTimeSpan5sInitial delay before reconnecting a dropped stream.
MaxReconnectDelayTimeSpan30sMaximum backoff delay for stream reconnection.
HeartbeatTimeoutTimeSpan65sTime without a server heartbeat before the stream is considered dead and reconnects.

InitAsync()

await client.InitAsync();

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 InitAsync(), GetFlag() reads from the local cache; if the connection degrades later, IsStale() reports it and GetFlag() continues to return the last known values.

Examples

ASP.NET Core (singleton with streaming)

// Program.cs using CanaryGate; var builder = WebApplication.CreateBuilder(args); var canary = new CanaryGateClient( builder.Configuration["CANARYGATE_KEY"]!, new Options { Environment = "production", Stream = true, } ); await canary.InitAsync(); builder.Services.AddSingleton(canary); var app = builder.Build(); app.MapGet("/", (CanaryGateClient canary) => { var flag = canary.GetFlag("new-checkout", new FlagEvaluationContext { UserId = "user-42" }); return flag?.Enabled == true ? "New checkout" : "Old checkout"; }); app.Run();

One-shot script (no stream)

using CanaryGate; var client = new CanaryGateClient("cg_api_key"); await client.InitAsync(); var flag = client.GetFlag("run-migration"); if (flag?.Enabled == true) { await RunMigrationAsync(); } // Disconnect (closes any active stream) so the process can exit client.Disconnect();
Last updated on