Overview
The Better Auth plugin provides first-class integration between Autumn and Better Auth, a modern authentication library for TypeScript. It automatically handles customer identification based on your Better Auth session and organization context.Installation
Install both Autumn and Better Auth:Quick Start
Server Setup
Add the Autumn plugin to your Better Auth configuration:lib/auth.ts
Client Setup
Add the Autumn client plugin:lib/auth-client.ts
Configuration
Plugin Options
string
default:"process.env.AUTUMN_SECRET_KEY"
Your Autumn API secret key. Defaults to the
AUTUMN_SECRET_KEY environment variable.string
default:"https://api.useautumn.com/v1"
The Autumn API base URL. Only change this if you’re self-hosting Autumn.
CustomerScope
default:"user"
How to resolve customer identity from the session. Options:
"user"- Use the authenticated user as the customer"organization"- Use the active organization as the customer"user_and_organization"- Prefer organization, fall back to user
IdentifyFn
Custom identity resolver function. Overrides
customerScope for advanced use cases.Customer Scope Modes
User Scope (Default)
Customers are identified by user ID:Organization Scope
Customers are identified by organization ID (requires Better Auth organization plugin):User and Organization Scope
Prefer organization if active, otherwise use user:Custom Identity Resolution
For advanced scenarios, provide a customidentify function:
Client Usage
Calling Autumn Endpoints
Use the auth client to call Autumn endpoints:Available Endpoints
All Autumn endpoints are available underauthClient.autumn.*:
Customer Endpoints
Billing Endpoints
Plan Endpoints
Event Endpoints
Referral Endpoints
React Integration
Use Autumn with Better Auth React hooks:components/billing/PlanSelector.tsx
Server-Side Usage
Access Autumn endpoints server-side using Better Auth’s server methods:app/api/billing/route.ts
Organization Support
When using Better Auth’s organization plugin:lib/auth.ts
- Use the active organization as the customer when one is selected
- Use organization name for customer data
- Return
nullfor customer when no organization is active
TypeScript Support
The plugin provides full TypeScript support:How It Works
- Session Resolution - Better Auth provides the current session
- Organization Resolution - If using organizations, gets the active organization
- Identity Resolution - Resolves customer identity based on
customerScopeor customidentifyfunction - Endpoint Routing - Routes requests to appropriate Autumn API endpoints
- Response Handling - Returns typed responses to your client
Error Handling
The plugin handles authentication errors:Environment Variables
.env
Complete Example
Here’s a full example with organizations:lib/auth.ts
lib/auth-client.ts
components/Billing.tsx
Next Steps
Hono Adapter
Use Autumn with Hono web framework
Next.js Adapter
Integrate Autumn with Next.js App Router
React Hooks
Build billing UIs with React hooks
API Reference
Explore all available API endpoints