Skip to main content

Overview

The Hono adapter provides seamless integration between Autumn and the Hono web framework. It automatically handles routing, request parsing, and customer identification for all Autumn endpoints.

Installation

Install the Autumn JS SDK which includes the Hono adapter:

Quick Start

Set up the Autumn handler as middleware in your Hono application:

Configuration

The honoAutumnHandler accepts the following options:

Options

(c: Context) => AuthResult
required
Function to identify the customer from the Hono context. 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. Routes will be registered under this prefix.

Integration with Authentication

Using Hono Context Variables

If your auth middleware sets user data on the Hono context, you can access it in the identify function:

Using JWT Authentication

Available Routes

Once configured, the following routes 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

How It Works

The adapter:
  1. Intercepts requests to the configured path prefix (default: /api/autumn/*)
  2. Parses the request body and method
  3. Calls your identify function to get customer identity
  4. Routes to the appropriate handler based on the endpoint path
  5. Returns JSON responses with appropriate HTTP status codes
  6. Passes through 404s to your next middleware if the route doesn’t match

TypeScript Support

The adapter provides full TypeScript support with proper type inference:

Error Handling

The adapter automatically handles errors and returns appropriate responses:
  • 401 Unauthorized - When identify returns null for protected routes
  • 404 Not Found - When the route doesn’t match (passed to next middleware)
  • 400 Bad Request - For invalid request bodies
  • 500 Internal Server Error - For SDK errors
All error responses follow the format:

Custom Path Prefix

You can customize where Autumn routes are mounted:

Environment Variables

The adapter uses the following environment variables:

Complete Example

Here’s a complete example with authentication and custom routes:

Next Steps

Better Auth Integration

Use Autumn with Better Auth for authentication

React Hooks

Call Autumn endpoints from your React frontend

API Reference

Explore all available API endpoints

Billing Integration

Learn about attaching plans and billing