Skip to Content
SDKJavaScript / TypeScriptInitialization

Constructor

import { CanaryGate } from '@canarygate/sdk/client' // browser // or import { CanaryGate } from '@canarygate/sdk/server' // server runtimes new CanaryGate(apiKey: string, options?: CanaryGateOptions)

Parameters

ParameterTypeRequiredDescription
apiKeystringYesEnvironment API Key. Created under Settings → API Keys.
optionsCanaryGateOptionsNoConfiguration options. See below.

CanaryGateOptions

The API base URL is resolved automatically from the CANARYGATE_BASE_URL environment variable (or NEXT_PUBLIC_CANARYGATE_BASE_URL in browser builds), defaulting to https://api.canarygate.dev. Set the variable when self-hosting or during local development. Options that belong to the other entry point throw at construction.

OptionTypeDefaultDescription
environmentstringdefault environmentEnvironment slug, sent via the X-Environment header.
pollIntervalMsnumber30000Browser polling interval in ms (clamped to a 3000ms minimum); set 0 to disable. Throws on the server entry.
reconnectDelaynumber5000Initial delay in ms before reconnecting a dropped stream. Server-only; throws on the browser entry.
maxReconnectDelaynumber30000Maximum backoff delay in ms for stream reconnection. Server-only.
heartbeatTimeoutMsnumber65000Time in ms without a server heartbeat before the stream is considered dead and reconnects. Server-only.
onUpdatefunction—Called whenever the evaluated flag set changes, with the updated flags. Never called on initial load.

init()

await canary.init()

Executes two actions in sequence:

  1. Makes an HTTP fetch to load all flags for the environment (snapshot)
  2. On the server, opens SSE automatically; in the browser, starts polling.

Always await init() before reading flags — otherwise, getFlag() may return undefined before the initial load completes.

Examples

Browser (React)

In browsers the SDK uses the snapshot plus polling (pollIntervalMs). Changes detected by the polling refresh are reported through the onUpdate callback, so the UI can react without a full reload.

import { CanaryGate, type FlagData } from '@canarygate/sdk/client' import { useEffect, useState } from 'react' export function useCanaryGate() { const [flags, setFlags] = useState<FlagData[]>([]) const [isReady, setIsReady] = useState(false) useEffect(() => { const client = new CanaryGate(process.env.NEXT_PUBLIC_CANARYGATE_KEY!, { onUpdate: (updated) => setFlags(updated) }) client.init().then(() => { setFlags(client.getFlags()) setIsReady(true) }) return () => { client.disconnect() } }, []) return { flags, isReady } }

Node.js (singleton)

import { CanaryGate } from '@canarygate/sdk/server' let client: CanaryGate | null = null export async function getCanaryGate() { if (!client) { client = new CanaryGate(process.env.CANARYGATE_KEY!) await client.init() } return client }

One-shot script

import { CanaryGate } from '@canarygate/sdk/server' const client = new CanaryGate('cg_api_key') await client.init() const flag = client.getFlag('run-migration') if (flag?.enabled) { await runMigration() } // Disconnect (closes the SSE stream so the process can exit) client.disconnect()
Last updated on