Switchflag
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>>
ParameterTypeDescription
configEdgeConfigAPI key and optional base URL
keystringThe flag key
contextEvaluationContextUser/request attributes for targeting rules

EdgeConfig

interface EdgeConfig {
  readonly apiKey: string
  readonly baseUrl?: string
}
PropertyTypeRequiredDefaultDescription
apiKeystringYesYour environment API key
baseUrlstringNohttps://api.switchflag.devAPI 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 freshness
  • revalidate: 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

FeatureStandardEdge
Import path@switchflag/sdk@switchflag/sdk/edge
CacheIn-memory Map (no TTL)fetch cache (60s revalidation)
APIcreateClient()client.getFlag()evaluateAtEdge() (stateless)
Best forLong-lived processesEphemeral edge functions