SDK
createClient
Create a Switchflag client instance for evaluating feature flags with in-memory caching.
Usage
import { createClient } from '@switchflag/sdk'
const client = createClient({
apiKey: 'sf_production_...',
})ClientConfig
interface ClientConfig {
readonly apiKey: string
readonly baseUrl?: string
}| Property | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | — | Your environment API key (e.g. sf_production_...) |
baseUrl | string | No | https://api.switchflag.dev | API base URL override |
SwitchflagClient
createClient returns a SwitchflagClient instance:
interface SwitchflagClient {
readonly getFlag: <T>(
key: string,
context: EvaluationContext,
) => Promise<EvaluationResult<T>>
}Caching
The client uses an in-memory Map to cache flag configurations. When you call getFlag:
- If the flag key is in the cache, the cached config is used — no network request
- If it's not cached, the client fetches
GET /v1/flags/{key}from the API - The response is stored in the cache for future calls
The cache has no TTL — entries persist for the lifetime of the client instance. This is by design for long-lived server processes where flag configs rarely change.
To get fresh configs, create a new client instance.
Example: Express server
import express from 'express'
import { createClient } from '@switchflag/sdk'
const app = express()
const flags = createClient({
apiKey: process.env.SWITCHFLAG_API_KEY!,
})
app.get('/checkout', async (req, res) => {
const { value } = await flags.getFlag<boolean>('new-checkout', {
userId: req.user.id,
email: req.user.email,
})
if (value) {
return res.render('checkout-v2')
}
return res.render('checkout')
})