Features
The Flowglad Better Auth plugin provides three main capabilities:-
Automatic Customer Creation: When users sign up (for
customerType: "user") or organizations are created (forcustomerType: "organization"), the plugin automatically creates a corresponding Flowglad customer. -
Billing API Endpoints: The plugin exposes all Flowglad billing endpoints directly on your Better Auth API at
{basePath}/flowglad/*. You no longer need to mount a dedicated flowglad route handler. -
Customer External ID Resolution: The plugin adds a
getExternalIdendpoint to your Better Auth API that automatically returns the correct customer external ID based on yourcustomerTypeconfiguration:- For
customerType: "user": Returnssession.session.userId - For
customerType: "organization": Returnssession.session.activeOrganizationId(requires the organization plugin) - For custom
getCustomer: Returns theexternalIdfrom your custom function
- For
Setup
Add the Flowglad plugin to your auth config
The Flowglad plugin integrates with Better Auth to automatically create customers and expose all Flowglad billing endpoints through your existing Better Auth API. You do not need to set up a separate route handler — the plugin creates endpoints at {your-better-auth-path}/flowglad/*.
- User
- Organization
- Custom
lib/auth.ts
Flowglad will auto-create your customers when they sign up (for
customerType: "user") or when organizations are created (for customerType: "organization"). You can choose who becomes a customer: individual users, organizations, or something custom using the getCustomer function.Add FlowgladProvider to your layout
MountFlowgladProvider directly in your root layout with the betterAuthBasePath prop. This tells the Flowglad React SDK to route all API calls through your Better Auth endpoints instead of a standalone route handler.
Important: The
betterAuthBasePath must match your Better Auth basePath configuration. If your Better Auth API is at /api/auth, set betterAuthBasePath="/api/auth".app/layout.tsx
betterAuthBasePath is set, all Flowglad client calls (like fetching billing data, creating checkout sessions, etc.) are routed to {betterAuthBasePath}/flowglad/* — for example, /api/auth/flowglad/customers/billing.
Usage
Using the React SDK
Once you’ve set upFlowgladProvider with betterAuthBasePath, use the useBilling hook to access billing data and actions:
FlowgladProvider with betterAuthBasePath, all standard Flowglad React hooks work exactly as documented — the Better Auth plugin simply routes API calls through your auth endpoints instead of a standalone handler. See the React SDK documentation for detailed usage of each hook.
Server-Side: Get Customer External ID
For server-side operations, use the plugin’sgetExternalId endpoint to retrieve the customer external ID:
Configuration Reference
FlowgladProvider
When using Better Auth integration, configureFlowgladProvider with betterAuthBasePath:
FlowgladBetterAuthPluginOptions (Server)
Common Patterns
Programmatic Organization Customer Creation
If you need to create a Flowglad customer for an organization programmatically (outside of the Better Auth hook), you can use thecreateFlowgladCustomerForOrganization helper:
Custom Customer Extraction
For advanced use cases, you can provide a customgetCustomer function:
Troubleshooting
Billing data not loading / API calls failing
IfuseBilling() returns errors or billing data doesn’t load:
-
Verify
betterAuthBasePathmatches your Better Auth configuration:- If your Better Auth route is at
/api/auth/[...all]/route.ts, usebetterAuthBasePath="/api/auth" - The Flowglad SDK will make requests to
{betterAuthBasePath}/flowglad/*
- If your Better Auth route is at
- Check browser network tab for failed requests to see the actual URL being called
- Ensure the flowgladPlugin is installed in your Better Auth config
Customer not created after sign-up
Make sure:- Your
customerTypematches your use case ("user"for B2C where users are customers,"organization"for B2B where organizations are customers) - The Better Auth plugin is properly configured in your
authconfig - You have
FLOWGLAD_SECRET_KEYset in your environment variables - Check your server logs for any errors during customer creation
getExternalId returns null
This means the user is not authenticated. Make sure:- You’re passing the correct headers to
auth.api.getExternalId() - The user has an active session
- For organization customers, make sure the organization plugin is installed and the session includes
activeOrganizationId(Better Auth exposes this onsession.session.activeOrganizationId)
Organization customer creation issues
Make sure:- The
organization()plugin is installed before theflowgladPlugin customerTypeis set to"organization"- The session includes
activeOrganizationId(Better Auth exposes this onsession.session.activeOrganizationId) once the organization has been created
”NO_ACTIVE_ORGANIZATION” error
When usingcustomerType: "organization", this error occurs if the user hasn’t selected an organization yet. Make sure:
- The user has created or been added to an organization
- The user has selected an active organization (Better Auth’s organization plugin manages this)
- The
activeOrganizationIdis present in the session