Using Better Auth? Check out the Flowglad Better Auth plugin for automatic customer creation and simplified integration.
Scoped Server Pattern
The scoped server pattern gives you maximum flexibility for defining customers and works seamlessly with any authentication provider. This approach uses a factory function that creates aFlowgladServer instance scoped to a specific customer.
Key benefits:
- Works with any auth provider
- Full control over customer identity (user ID for B2C, organization ID for B2B)
Server Setup
Create a Flowglad server factory function in a shared file (e.g.,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 customerExternalIdNext Route Handler Setup
Set up your Flowglad API route at/api/flowglad/[...path] to handle requests from your frontend: