SDK
Edge SDK
Evaluate feature flags at the edge using evaluateAtEdge, designed for Vercel Edge Functions, Cloudflare Workers, and Next.js Middleware.
Why a separate edge SDK?
Edge runtimes are ephemeral — each invocation starts fresh with no persistent memory. The standard SDK's in-memory Map cache would be useless since it's cleared on every request.
The edge SDK uses fetch with cache: 'force-cache' and next: { revalidate: 60 }, letting the edge network handle caching. Flag configs are fetched once and cached at the CDN layer for 60 seconds.
Usage
import { evaluateAtEdge } from '@switchflag/sdk/edge'
const result = await evaluateAtEdge<boolean>(
{ apiKey: 'sf_production_...' },
'maintenance-mode',
{ userId: 'user-123' },
)evaluateAtEdge
function evaluateAtEdge<T>(
config: EdgeConfig,
key: string,
context: EvaluationContext,
): Promise<EvaluationResult<T>>| Parameter | Type | Description |
|---|---|---|
config | EdgeConfig | API key and optional base URL |
key | string | The flag key |
context | EvaluationContext | User/request attributes for targeting rules |
EdgeConfig
interface EdgeConfig {
readonly apiKey: string
readonly baseUrl?: string
}| Property | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | — | Your environment API key |
baseUrl | string | No | https://api.switchflag.dev | API base URL override |
Caching behavior
The edge SDK passes these options to fetch:
{
cache: 'force-cache',
next: { revalidate: 60 }
}force-cache— use the cached response if available, regardless of freshnessrevalidate: 60— revalidate the cache entry after 60 seconds
This means flag config changes propagate to the edge within 60 seconds.
Example: Next.js Middleware
import { evaluateAtEdge } from '@switchflag/sdk/edge'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const userId = request.cookies.get('userId')?.value ?? 'anonymous'
const { value: isBeta } = await evaluateAtEdge<boolean>(
{ apiKey: process.env.SWITCHFLAG_API_KEY! },
'beta-dashboard',
{ userId },
)
if (isBeta) {
return NextResponse.rewrite(new URL('/beta/dashboard', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*'],
}Example: Vercel Edge Function
import { evaluateAtEdge } from '@switchflag/sdk/edge'
export const config = { runtime: 'edge' }
export default async function handler(request: Request) {
const { value: enabled } = await evaluateAtEdge<boolean>(
{ apiKey: process.env.SWITCHFLAG_API_KEY! },
'new-api',
{ userId: 'anonymous' },
)
return new Response(JSON.stringify({ enabled }), {
headers: { 'Content-Type': 'application/json' },
})
}Differences from standard SDK
| Feature | Standard | Edge |
|---|---|---|
| Import path | @switchflag/sdk | @switchflag/sdk/edge |
| Cache | In-memory Map (no TTL) | fetch cache (60s revalidation) |
| API | createClient() → client.getFlag() | evaluateAtEdge() (stateless) |
| Best for | Long-lived processes | Ephemeral edge functions |