Skip to main content

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

The nextAutumnHandler 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 null
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.
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:
Supported methods:
  • GET - For read operations
  • POST - For create/update operations
  • DELETE - 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 customer
  • POST /api/autumn/previewAttach - Preview plan attachment
  • POST /api/autumn/updateSubscription - Update subscription
  • POST /api/autumn/previewUpdateSubscription - Preview subscription update
  • POST /api/autumn/openCustomerPortal - Open customer billing portal
  • POST /api/autumn/multiAttach - Attach multiple plans
  • POST /api/autumn/previewMultiAttach - Preview multiple plan attachments
  • POST /api/autumn/setupPayment - Setup payment method

Plan Routes

  • POST /api/autumn/listPlans - List available plans

Event Routes

  • POST /api/autumn/listEvents - List customer events
  • POST /api/autumn/aggregateEvents - Aggregate event data

Referral Routes

  • POST /api/autumn/createReferralCode - Create referral code
  • POST /api/autumn/redeemReferralCode - Redeem referral code

File Structure

Place your Autumn handler in a catch-all route:
This structure captures all requests under /api/autumn/*.

How It Works

The adapter:
  1. Receives requests to your catch-all route
  2. Parses request method and body from the Next.js Request object
  3. Calls your identify function to determine customer identity
  4. Routes to the appropriate handler based on the URL path
  5. 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 identify returns null for 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
All errors return JSON:

Custom Path Prefix

If you place your route in a different location, update the pathPrefix:
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