Installation
Setup
Add the Provider
FlagmintProvider initialises the SDK and provides flag context to all child components. Place it as high as possible in your component tree.
Provider Props
| Prop | Type | Default | Description |
|---|---|---|---|
options | FlagClientOptions | Required | SDK configuration (see below) |
initialFlags | Record<string, any> | {} | Pre-evaluated flags for SSR hydration |
deferInitialization | boolean | false | If true, don’t connect until context is set |
FlagClientOptions
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your environment’s SDK key (starts with ff_) |
context | object | undefined | Evaluation context (user, org attributes) |
transportMode | 'auto' | 'websocket' | 'long-polling' | 'auto' | Connection method |
enableOfflineCache | boolean | true | Cache flags in localStorage for offline use |
persistContext | boolean | false | Persist context across page reloads |
onError | (error: Error) => void | undefined | Error handler callback |
Evaluating Flags
useFlag
Returns the evaluated value of a single flag. Re-renders the component when the flag value changes.| Parameter | Type | Description |
|---|---|---|
key | string | The flag key (must match the key in the dashboard) |
defaultValue | T | Fallback value used when the SDK isn’t ready or the flag doesn’t exist |
useFlagmintReady
Returnstrue once the SDK has connected and received its initial flag values.
User Context
Setting Context at Init
Pass context when the user is known (typically after login):Authentication Pattern
The recommended pattern for apps with login is to always render children, but only pass context after authentication:Transport Modes
| Mode | Best For | Real-Time? |
|---|---|---|
websocket | Browser apps, dashboards | Yes — server pushes updates instantly |
long-polling | Server-side rendering, restricted networks | No — polls every 10 seconds |
auto | Most apps (default) | Tries WebSocket, falls back to polling |
websocket is recommended. The SDK maintains a single persistent connection and receives flag updates the moment they change in the dashboard.
Offline & Caching
WhenenableOfflineCache is true (the default), the SDK caches flag values in localStorage. If the server is unreachable on the next page load, the SDK serves cached values until a connection is re-established.
Cached values expire after 24 hours.
TypeScript
The SDK is fully typed. Use generics withuseFlag to get type-safe flag values:
Troubleshooting
useFlagmintReady always returns false
useFlagmintReady always returns false
The SDK can’t connect to the server. Check that your API key is correct, the API URL is reachable, and CORS allows your app’s origin.
Flags don't update in real-time
Flags don't update in real-time
Verify you’re using
transportMode: 'websocket'. If you’re behind a corporate proxy that blocks WebSocket connections, switch to 'long-polling'.Hydration mismatch in Next.js
Hydration mismatch in Next.js
The server doesn’t have flag values during SSR. Use the
initialFlags prop to pass pre-evaluated flags, or wrap the flag-dependent UI with useFlagmintReady to defer rendering until the client has connected.WebSocket disconnects and reconnects repeatedly
WebSocket disconnects and reconnects repeatedly
Check your server’s heartbeat interval. The SDK sends a ping every 25 seconds. If your server’s heartbeat timeout is shorter than the SDK’s ping interval, the server will terminate the connection. Align both to 30 seconds.
CORS error when connecting
CORS error when connecting
Your Flagmint API must include your app’s origin in its CORS configuration. For local development, ensure
localhost:3000 (or your dev port) is allowed. The WebSocket upgrade request also needs CORS headers.Next Steps
Targeting Rules
Control which users see which features.
API Reference
Direct API access for custom integrations.