Guides1 min read

Write an OpenFeature provider in 80 lines

OpenFeature gives your code one flag API whatever vendor sits behind it. Here is a complete provider for Driftwire, in about 80 lines of TypeScript.

Mara Sato

Mara Sato

Developer Advocate ·

Guides cover: Write an OpenFeature provider in 80 lines

OpenFeature (opens in new tab) is a vendor-neutral API for feature flags. Your application calls client.getBooleanValue(); a provider decides where the answer comes from. Swap the provider and no application code changes.

Install

terminalShell
// What an edge node receives: a diff against the version it holds.
interface RulesetDiff {
    version: number
    previous: number
    changed: Record<string, FlagRules>
    removed: string[]
}

function apply(diff: RulesetDiff) {
    if (diff.previous !== store.version) return resync() // missed one: fetch a snapshot
    store.merge(diff.changed, diff.removed)
    store.version = diff.version
}

The provider

driftwire-provider.tsTypeScript
import type { EvaluationContext, Provider, ResolutionDetails } from '@openfeature/server-sdk'
import { createClient, type DriftwireClient } from '@driftwire-demo/sdk'

export class DriftwireProvider implements Provider {
    readonly metadata = { name: 'driftwire' } as const
    private client: DriftwireClient

    constructor(sdkKey: string) {
        this.client = createClient({ sdkKey })
    }

    async initialize() {
        await this.client.ready()
    }

    async resolveBooleanEvaluation(
        flagKey: string,
        defaultValue: boolean,
        context: EvaluationContext
    ): Promise<ResolutionDetails<boolean>> {
        const result = await this.client.evaluate(flagKey, toDriftwire(context))
        return {
            value: typeof result.value === 'boolean' ? result.value : defaultValue,
            reason: result.reason, // 'TARGETING_MATCH' | 'SPLIT' | 'DEFAULT'
            variant: result.variant,
        }
    }

    // resolveString / resolveNumber / resolveObject follow the same shape.
}

const toDriftwire = ({ targetingKey, ...attributes }: EvaluationContext) => ({
    key: targetingKey ?? 'anonymous',
    ...attributes,
})

Wire it up

server.tsTypeScript
import { OpenFeature } from '@openfeature/server-sdk'
import { DriftwireProvider } from './driftwire-provider'

await OpenFeature.setProviderAndWait(new DriftwireProvider(process.env.DRIFTWIRE_SDK_KEY!))

const flags = OpenFeature.getClient()
const enabled = await flags.getBooleanValue('new-checkout-flow', false, { targetingKey: user.id })

That is the whole integration. The context you pass is the same context Driftwire’s targeting rules see, so a rule like plan is "enterprise" works unchanged.

OpenFeatureTypeScriptSDK
Mara Sato

Written by

Mara Sato

Developer Advocate

Mara writes the guides and talks to the teams who use Driftwire every day, then writes those conversations down as case studies.

More posts by Mara Sato