Overview
The Next.js adapter enables seamless integration between Autumn and Next.js applications using the App Router. It provides a clean API for handling Autumn requests in your Next.js route handlers.Installation
Install the Autumn JS SDK which includes the Next.js adapter:Quick Start
Create a catch-all route handler in your Next.js app:app/api/autumn/[...autumn]/route.ts
Configuration
ThenextAutumnHandler accepts the following options:
Options
(request: Request) => AuthResult
required
Function to identify the customer from the Next.js request. Return
null for unauthenticated requests.Returns: { customerId: string; customerData?: { name?: string; email?: string; ... } } or nullstring
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.
string
default:"/api/autumn"
The path prefix for all Autumn routes. Should match your route file location.
Route Handler Structure
The adapter returns an object with HTTP method handlers that you can export:GET- For read operationsPOST- For create/update operationsDELETE- For delete operations
Integration with Next-Auth
Using Next-Auth v5 (Auth.js)
app/api/autumn/[...autumn]/route.ts
Using Next-Auth v4
app/api/autumn/[...autumn]/route.ts
Integration with Clerk
app/api/autumn/[...autumn]/route.ts
Integration with Custom JWT Auth
app/api/autumn/[...autumn]/route.ts
Integration with Cookies
app/api/autumn/[...autumn]/route.ts
Available Routes
Once configured with the catch-all route[...autumn], the following endpoints are automatically available:
Customer Routes
POST /api/autumn/getOrCreateCustomer- Get or create customer
Billing Routes
POST /api/autumn/attach- Attach a plan to customerPOST /api/autumn/previewAttach- Preview plan attachmentPOST /api/autumn/updateSubscription- Update subscriptionPOST /api/autumn/previewUpdateSubscription- Preview subscription updatePOST /api/autumn/openCustomerPortal- Open customer billing portalPOST /api/autumn/multiAttach- Attach multiple plansPOST /api/autumn/previewMultiAttach- Preview multiple plan attachmentsPOST /api/autumn/setupPayment- Setup payment method
Plan Routes
POST /api/autumn/listPlans- List available plans
Event Routes
POST /api/autumn/listEvents- List customer eventsPOST /api/autumn/aggregateEvents- Aggregate event data
Referral Routes
POST /api/autumn/createReferralCode- Create referral codePOST /api/autumn/redeemReferralCode- Redeem referral code
File Structure
Place your Autumn handler in a catch-all route:/api/autumn/*.
How It Works
The adapter:- Receives requests to your catch-all route
- Parses request method and body from the Next.js Request object
- Calls your identify function to determine customer identity
- Routes to the appropriate handler based on the URL path
- Returns NextResponse with JSON body and proper status codes
TypeScript Support
The adapter is fully typed:Error Handling
The adapter automatically handles errors:- 401 Unauthorized - When
identifyreturnsnullfor protected routes - 404 Not Found - When the route doesn’t exist
- 400 Bad Request - For invalid request bodies
- 500 Internal Server Error - For SDK errors
Custom Path Prefix
If you place your route in a different location, update thepathPrefix:
app/api/billing/[...billing]/route.ts
Environment Variables
Create a.env.local file:
Middleware Integration
You can use Next.js middleware to protect your Autumn routes:middleware.ts
Complete Example
Here’s a complete setup with Next-Auth:app/api/autumn/[...autumn]/route.ts
.env.local
Server Components
Since route handlers run on the server, you can safely use server-only code:Next Steps
Better Auth Integration
Use Autumn with Better Auth for authentication
React Hooks
Call Autumn endpoints from your React components
API Reference
Explore all available API endpoints
Billing Integration
Learn about attaching plans and billing