SDK API Reference
Detailed API reference for all Truflag client SDKs.
Public client key
In the dashboard, go to Environments and copy the Public key. Pass it asapiKey when configuring any SDK.
Configure options
Configure once at app startup with at least apiKey.
await Flags.configure({ apiKey: process.env.NEXT_PUBLIC_TRUFLAG_CLIENT_KEY!, user: { id: "user-123", attributes: { plan: "pro" } },});apiKey
RequiredstringPublic environment key copied from the dashboard.
user
OptionalTruflagUserInitial active user. If omitted, the SDK restores cached user or creates anonymous identity.
id
RequiredstringStable user identifier used for evaluation and bucketing.
attributes
OptionalUserAttributesOptional targeting attributes for the active user.
streamEnabled
Optionalbooleandefault: true
Enable streaming with polling fallback.
cacheTtlMs
Optionalnumberdefault: 300000
Cache snapshot freshness window.
pollingIntervalMs
Optionalnumberdefault: 60000
Polling interval when stream is unavailable.
requestTimeoutMs
Optionalnumberdefault: 6000
Per-request timeout.
telemetryEnabled
Optionalbooleandefault: true
Enable exposures and custom telemetry events.
telemetryFlushIntervalMs
Optionalnumberdefault: 10000
Telemetry flush interval.
telemetryBatchSize
Optionalnumberdefault: 50
Max events per telemetry flush.
logLevel
Optional"debug" | "info" | "warn" | "error" | "silent"default: "debug"
Minimum SDK log level.
logger
OptionalLoggerCustom logger implementation.
fetchFn
OptionalFetchLikeCustom fetch implementation.
storage
OptionalStorageAdapterCustom persistence adapter.
Runtime methods
Core identity, refresh, and flag-read APIs.
Flags.login()
Sets active user and refreshes flag state.
Flags.login(user: { id: string; attributes?: Record<string, string | number | boolean | null>;}): Promise<void>Returns
Promise resolved after refresh finishes.
Flags.setAttributes()
Merges attributes into current user and refreshes.
Flags.setAttributes( attributes: Record<string, string | number | boolean | null>): Promise<void>Returns
Promise resolved after refresh finishes.
Flags.refresh()
Fetches latest flags for active user.
Flags.refresh(): Promise<void>Returns
Promise resolved after refresh attempt.
Flags.logout()
Switches to anonymous identity and refreshes.
Flags.logout(): Promise<void>Returns
Promise resolved after anonymous refresh.
Flags.getFlag()
Reads current flag value from SDK state.
Flags.getFlag<T>(flagKey: string, defaultValue: T): TReturns
Resolved value, or fallback when state is not ready or key is missing.
Notes
- Automatically queues exposure for this read.
Flags.getFlagPayload()
Returns metadata payload for a flag.
Flags.getFlagPayload(flagKey: string): Record<string, unknown> | nullReturns
Payload object or null.
Notes
- Does not emit exposure by itself.
Flags.getAllFlags()
Returns all loaded flags.
Flags.getAllFlags(): Record<string, unknown>Returns
Object keyed by flag key.
Notes
- Queues exposures for returned flags.
Flags.isReady()
Readiness state for runtime reads.
Flags.isReady(): booleanReturns
True when SDK has usable state.
Flags.subscribe()
Subscribes to SDK state changes.
Flags.subscribe(callback: () => void): () => voidReturns
Unsubscribe function.
Reactive APIs
Subscribe to state or flag changes in UI flows.
useFlagsReady()
Subscribes to readiness state.
useFlagsReady(): booleanReturns
True when SDK has usable state.
useFlag()
Subscribes to a single flag value.
useFlag<T>(flagKey: string, defaultValue: T): TReturns
Resolved value or fallback.
Notes
- Automatically reports exposure when payload signature changes.
useFlagPayload()
Subscribes to flag payload metadata.
useFlagPayload(flagKey: string): Record<string, unknown> | nullReturns
Payload object or null.
Telemetry and events
Custom events, exposures, and manual read-reporting APIs.
Flags.track()
Queues a custom telemetry event.
Flags.track( eventName: string, properties?: Record<string, unknown>, options?: { immediate?: boolean }): Promise<void>Returns
Promise resolved after optional immediate flush.
Notes
- Blank event names are ignored.
- Flushes immediately once queue reaches telemetryBatchSize.
Flags.expose()
Manually emits exposure for a flag.
Flags.expose( flagKey: string, options?: { immediate?: boolean; properties?: Record<string, unknown> }): Promise<void>Returns
Promise resolved after optional immediate flush.
Flags.notifyFlagRead()
Advanced API: reports a flag read without returning value.
Flags.notifyFlagRead(flagKey: string): voidReturns
Nothing.
Notes
- No-op if SDK is not ready or flag is missing.