Next.js SDK
Guided sample
Open the Next.js sample. Begin with the README, then read lib/env.ts (public versus server settings), lib/toggly.ts (initialization), lib/order-context.ts (entity mapping), and the server/client/edge pages. Browser, Node server, and edge APIs are distinct—follow the matching runtime page.
A NEXT_PUBLIC_ value is shipped to the browser. Use only public App Keys there; keep management credentials out of browser configuration. Offline defaults and fixture tests do not establish live dashboard connectivity.
Use Toggly's feature flags in Next.js 14+ applications with full support for Server Components, Client Components, Server Actions, Middleware, and Edge Runtime.
Grab the printable Next.js cheat sheet (download PDF) — server components, client hooks, edge middleware, SSR hydration.
Architecture
The Next.js SDK is modular with 4 packages:
| Package | Description |
|---|---|
@ops-ai/nextjs-toggly-core | Shared client, types, and local/remote evaluation |
@ops-ai/nextjs-toggly-server | Server Components, Server Actions, Route Handlers |
@ops-ai/nextjs-toggly-client | React hooks and Client Components |
@ops-ai/nextjs-toggly-edge | Middleware and Edge Runtime support |
Server packages evaluate flags in Node — pass user identity and entity context on each check. Client and edge packages load flags for the browser and middleware. When a flag depends on an entity (an order, a page, an account), evaluate it on the server and pass the result to the client.
Benefits
- Server Components render features without client JavaScript
- Client Components get real-time updates with React hooks
- Middleware enables feature-based routing at the edge
- Full TypeScript support with strict types
- SSR/SSG compatible with proper hydration
- Supports both App Router and Pages Router patterns
Installation
Install the packages you need:
# Install all packages (recommended)
npm install @ops-ai/nextjs-toggly-core @ops-ai/nextjs-toggly-server @ops-ai/nextjs-toggly-client @ops-ai/nextjs-toggly-edge
Or install individually based on your needs:
# Server Components only
npm install @ops-ai/nextjs-toggly-core @ops-ai/nextjs-toggly-server
# Client Components only
npm install @ops-ai/nextjs-toggly-core @ops-ai/nextjs-toggly-client
# Middleware only
npm install @ops-ai/nextjs-toggly-core @ops-ai/nextjs-toggly-edge
Quick Start
Server Components
import { initServerToggly, Feature } from '@ops-ai/nextjs-toggly-server'
// Initialize in the root layout (process-wide singleton)
await initServerToggly({
appKey: process.env.TOGGLY_APP_KEY!,
})
export default async function DashboardPage() {
return (
<Feature featureKey="new-dashboard">
<NewDashboard />
</Feature>
)
}
Client Components
'use client'
import { TogglyProvider } from '@ops-ai/nextjs-toggly-client'
export function Providers({ children }: { children: React.ReactNode }) {
return (
<TogglyProvider
config={{
appKey: process.env.NEXT_PUBLIC_TOGGLY_APP_KEY!,
environment: 'Production',
}}
>
{children}
</TogglyProvider>
)
}
'use client'
import { useFeatureFlag } from '@ops-ai/nextjs-toggly-client'
export function FeatureButton() {
const { isEnabled, isLoading } = useFeatureFlag('new-button')
if (isLoading) return <LoadingSpinner />
return isEnabled ? <NewButton /> : <OldButton />
}
Middleware
import { createFeatureMiddleware } from '@ops-ai/nextjs-toggly-edge'
const featureMiddleware = createFeatureMiddleware({
appKey: process.env.TOGGLY_APP_KEY!,
})
export async function middleware(request: NextRequest) {
return featureMiddleware(request, {
featureKey: 'beta-access',
redirectTo: '/waitlist',
})
}
export const config = {
matcher: '/beta/:path*',
}
Environment Variables
# Server-side (private)
TOGGLY_APP_KEY=your-app-key
# Client-side (public)
NEXT_PUBLIC_TOGGLY_APP_KEY=your-app-key
Features
Server-Side
- Async Components:
Feature,FeatureVariant(negate,context/contextKind) - Server Actions:
checkFeature(),checkFeatureGate()(string identity or{ identity, context, contextKind }) - Local evaluation: identity and entity context are per-call, not a fetch query
- Caching: Built-in caching with
unstable_cache
Client-Side
- Hooks:
useFeatureFlag(),useFeatureGate(),useFeatures() - Components:
Feature,FeatureGate,FeatureSwitch - Context:
TogglyProvider,useToggly() - Identity: Persist user identity for targeting
Edge Runtime
- Middleware:
createFeatureMiddleware() - Path Matching:
createPathFeatureMiddleware() - HOC:
withFeatureGate() - Edge Caching: Cloudflare cache integration
Next Steps
- Server-Side Usage
- Client-Side Usage
- Middleware & Edge
- Components Reference
- Usage & metrics — server gRPC (optional) + edge HTTPS
- WebSocket sync
- Evaluated-signed