Get help on Flowglad's Discord
We’re online to help you with any questions you have.
Features
- Automatic Customer Creation: Customers are automatically created when users sign up or organizations are created
- Declarative Customer Mapping: Choose whether customers are users or organizations with a simple configuration
- Custom Customer Extraction: Full control over how customer data is extracted from your auth session
- Seamless Integration: Works with Better Auth’s hooks and endpoints
Setup
Add the Flowglad plugin to your auth config
The Flowglad plugin integrates with Better Auth to automatically create customers and provide a convenient way to get the customer external ID based on your authentication setup.
- 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.Next.js: Add FlowgladProvider wrapper
For Next.js applications using Better Auth, you must use a client-side wrapper component that reactively watches the session state to ensureloadBilling updates correctly when the user authenticates.
components/providers.tsx
app/layout.tsx
For Better Auth apps in Next.js, you must use a client-side wrapper component that uses
authClient.useSession() to reactively watch for session changes. Otherwise, you may hit an edge case where loadBilling’s value does not change when the user authenticates.What It Does
The Flowglad Better Auth plugin provides two 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. -
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.user.id - For
customerType: "organization": Returnssession.user.activeOrganizationId(requires the organization plugin) - For custom
getCustomer: Returns theexternalIdfrom your custom function
- For
Usage
Get Customer External ID
Use the plugin’sgetExternalId endpoint to retrieve the customer external ID, which you can then pass to your Flowglad factory function.
Use in Route Handlers
When setting up your Flowglad route handler, usegetExternalId to automatically resolve the customer external ID:
app/api/flowglad/[...path]/route.ts
Configuration Reference
FlowgladBetterAuthPluginOptions
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
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 user has an
activeOrganizationId
loadBilling not updating reactively
For Better Auth apps in Next.js, you must use a client-side wrapper component that usesauthClient.useSession() to watch for session changes. See the Next.js setup section above.
Organization customer creation issues
Make sure:- The
organization()plugin is installed before theflowgladPlugin customerTypeis set to"organization"- The user has an
activeOrganizationIdin theirsessionobject once the organization has been created