A tiny, dependency-free feature flag library designed for serverless, edge, and modern JavaScript runtimes. Built to be safe, deterministic, and easy to integrate anywhere.
rollout
npm install tiny-feature-flags
import { TinyFlags } from 'tiny-feature-flags'
const flags = await TinyFlags.load('https://example.com/flags.json', {
userId: 'user-123',
traits: { region: 'us', plan: 'pro' },
})
if (flags.enabled('newSidebar')) {
showNewSidebar()
}
{
"newSidebar": { "enabled": true, "rollout": 30 },
"v3Checkout": { "enabled": true }
}
Each flag has:
enabled: boolean
— the base togglerollout: number
— deterministic activation based on user ID hashUnknown flags always return false
.
class TinyFlags {
constructor(flags: FlagSet, context?: FlagContext)
static async load(url: string, context?: FlagContext): Promise<TinyFlags>
enabled(flagName: string): boolean
}
type FlagContext = {
userId?: string
traits?: Record<string, string>
}
type FlagDefinition = {
enabled: boolean
rollout?: number
}
type FlagSet = Record<string, FlagDefinition>
Serve JSON from any HTTPS endpoint. It can be hosted on:
export const runtime = 'edge'
import { TinyFlags } from 'tiny-feature-flags'
import { NextRequest } from 'next/server'
export async function GET(req: NextRequest) {
const userId = req.cookies.get('user_id')?.value || 'anonymous'
const flags = await TinyFlags.load('https://example.com/flags.json', {
userId,
traits: { region: 'br', plan: 'pro' },
})
return Response.json({
showNewSidebar: flags.enabled('newSidebar'),
})
}
false
for unknown flagsContributions are welcome. See CONTRIBUTING.md.
This library is licensed under the MIT License. See the LICENSE file for details.