NPM Package: @flowglad/nextjs
Overview
The Next.js SDK provides a complete integration for both App Router and Pages Router, including server-side utilities and client-side components.Installation
Requirements
- React 18 or 19
- Next.js 14 or 15
Quick Start
1. Set Up Environment Variables
Add your Flowglad API key to your environment:.env
2. Create Server Client
Create a Flowglad server factory function in a shared file, eg.lib/flowglad.ts:
Important:
B2B apps: Pass
customerExternalId is the ID from your app’s database (e.g., user.id or organization.id), not Flowglad’s customer ID.B2C apps: Pass user.id as customerExternalIdB2B apps: Pass
organization.id or team.id as customerExternalIdFlowglad integrates seamlessly with your auth provider. Read more about auth options here.
3. Create API Route Handler
Create a route handler to handle Flowglad API requests from your frontend. The Server SDK provides route handler constructors that enable your client to communicate with your server, allowing you to load billing and feature access data via theuseBilling hook on your frontend. It should handle requests at /api/flowglad/...
- Next.js App Router
- Next.js Pages Router
- Express
- Other Frameworks
app/api/flowglad/[...path]/route.ts
4. Wrap Your App with FlowgladProvider
- Next.js App Router
- Next.js Pages Router
- Vite + React
app/layout.tsx
5. Use the Billing Hook
- Next.js
- Vite + React
Key Features
- App Router Support: First-class support for Next.js App Router with Server Components
- Pages Router Support: Full compatibility with Pages Router
- Type-safe Route Handlers: Automatic type inference for API routes
- Server-side Utilities: Access billing data in Server Components and API routes
- Client Components: Pre-built components for subscriptions and billing
- React Context: Global billing state management
API Reference
Server Exports
Import from@flowglad/nextjs/server:
FlowgladServer
The main server class for backend operations. See the Server SDK documentation for full details.
Route Handler
Creates GET and POST handlers for App Router:Client Exports
You can import the following from@flowglad/nextjs:
FlowgladProvider
The main provider component. See the React SDK documentation for full details on props and usage.
useBilling()
Access billing data and functions. See the React SDK documentation for full API reference.
Server Setup
lib/flowglad.ts
app/api/flowglad/[...path]/route.ts
Important:
B2B apps: Pass
customerExternalId is the ID from your app’s database (e.g., user.id or organization.id), not Flowglad’s customer ID.B2C apps: Pass user.id as customerExternalIdB2B apps: Pass
organization.id or team.id as customerExternalIdFlowglad doesn’t need to know how you authenticate—just provide the ID from your system that represents the customer entity which will be billed.Complete Example
For full working examples, check out our example projects with authentication and billing integration.Next Steps
React SDK
Learn about React components and hooks
Server SDK
Explore server-side capabilities