Skip to main content

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
That’s it! Autumn endpoints are now available on your auth client.

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 custom identify function:

Client Usage

Calling Autumn Endpoints

Use the auth client to call Autumn endpoints:

Available Endpoints

All Autumn endpoints are available under authClient.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
Now Autumn will:
  1. Use the active organization as the customer when one is selected
  2. Use organization name for customer data
  3. Return null for customer when no organization is active

TypeScript Support

The plugin provides full TypeScript support:

How It Works

  1. Session Resolution - Better Auth provides the current session
  2. Organization Resolution - If using organizations, gets the active organization
  3. Identity Resolution - Resolves customer identity based on customerScope or custom identify function
  4. Endpoint Routing - Routes requests to appropriate Autumn API endpoints
  5. 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