autumn-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAutumn Integration Guide
Autumn集成指南
Always consult docs.useautumn.com for code examples and latest API.
Autumn is a TypeScript-first billing SDK supporting subscriptions, usage-based pricing, credits, trials, and more via Stripe.
Quick Reference
快速参考
Environment Variables
环境变量
- - API key (required). Get one at app.useautumn.com
AUTUMN_SECRET_KEY
- - API密钥(必填)。可在app.useautumn.com获取。
AUTUMN_SECRET_KEY
Installation
安装
bash
npm install autumn-js # Node.js
pip install autumn-py # Pythonbash
npm install autumn-js # Node.js
pip install autumn-py # PythonCore Methods
核心方法
| Method | Purpose |
|---|---|
| Create or get customer (idempotent) |
| Get Stripe URL or payment preview |
| Confirm purchase (card on file) |
| Cancel subscription |
| Verify feature access |
| Record usage |
| Get products with billing scenarios |
| 方法 | 用途 |
|---|---|
| 创建或获取客户(幂等操作) |
| 获取Stripe支付链接或支付预览 |
| 确认购买(绑定卡片) |
| 取消订阅 |
| 验证功能访问权限 |
| 记录使用量 |
| 获取包含计费场景的产品列表 |
Core Config Options
核心配置选项
| Option | Notes |
|---|---|
| Required. From env |
| Optional. Defaults to |
| 选项 | 说明 |
|---|---|
| 必填,从环境变量 |
| 可选,默认值为 |
Billing Patterns
计费模式
Check → Work → Track
检查 → 执行操作 → 上报用量
Always follow this order for protected actions:
typescript
const { data } = await autumn.check({ customer_id, feature_id: "api_calls" });
if (!data.allowed) return { error: "Limit reached" };
const result = await doWork();
await autumn.track({ customer_id, feature_id: "api_calls", value: 1 });
return result;对于受保护的操作请始终遵循这个顺序:
typescript
const { data } = await autumn.check({ customer_id, feature_id: "api_calls" });
if (!data.allowed) return { error: "Limit reached" };
const result = await doWork();
await autumn.track({ customer_id, feature_id: "api_calls", value: 1 });
return result;Two-Step Checkout
两步结账流程
typescript
const { data } = await autumn.checkout({ customer_id, product_id: "pro" });
if (data.url) return redirect(data.url); // New customer → Stripe
// Returning customer → show confirmation, then:
await autumn.attach({ customer_id, product_id: "pro" });typescript
const { data } = await autumn.checkout({ customer_id, product_id: "pro" });
if (data.url) return redirect(data.url); // 新客户 → 跳转Stripe
// 老客户 → 展示确认页,然后执行:
await autumn.attach({ customer_id, product_id: "pro" });Product Scenarios
产品场景
Use to get scenarios. Don't build custom logic.
products.list| Scenario | Meaning |
|---|---|
| Not subscribed |
| Currently on plan |
| Scheduled for future |
| Higher tier available |
| Lower tier available |
| Cancelled, can reactivate |
使用获取场景,不要自行编写自定义逻辑。
products.list| 场景 | 含义 |
|---|---|
| 未订阅 |
| 当前订阅生效中 |
| 订阅已排期,未来生效 |
| 可升级到更高档位 |
| 可降级到更低档位 |
| 已取消,可重新激活 |
Feature Types
功能类型
| Type | Behavior |
|---|---|
| Access granted or denied |
| Usage tracked against limit |
| Pool for multiple features |
| 类型 | 行为 |
|---|---|
| 允许或拒绝访问 |
| 按使用量限制,跟踪用量 |
| 支持多个功能共享的积分池 |
React Hooks
React Hooks
| Hook | Purpose |
|---|---|
| Get customer, checkout, attach, check |
| Get products with scenarios |
tsx
import { AutumnProvider } from "autumn-js/react";
<AutumnProvider>{children}</AutumnProvider>| Hook | 用途 |
|---|---|
| 获取客户信息、结账、绑定支付方式、权限检查 |
| 获取包含场景的产品列表 |
tsx
import { AutumnProvider } from "autumn-js/react";
<AutumnProvider>{children}</AutumnProvider>Handler Imports
处理器导入
| Framework | Import |
|---|---|
| Next.js | |
| React Router | |
| Hono | |
| Express | |
| Fastify | |
| Generic | |
| 框架 | 导入路径 |
|---|---|
| Next.js | |
| React Router | |
| Hono | |
| Express | |
| Fastify | |
| 通用 | |
Common Gotchas
常见注意事项
- URL field - Checkout URL is , not
data.urldata.checkout_url - Frontend checks - For UX only. Always enforce on backend
- Track after success - Only track usage after work completes
- Credit systems - Track metered features, not the credit system itself
- Cancel via free plan - Prefer over
attach({ product_id: "free" })cancel() - Idempotent creation - returns existing customer if ID exists
customers.create
- URL字段 - 结账链接是,不是
data.urldata.checkout_url - 前端权限检查 - 仅用于优化用户体验,必须在后端再次校验
- 成功后再上报用量 - 仅在操作执行完成后上报使用量
- 积分系统 - 只需上报计量类功能的用量,无需上报积分系统本身的用量
- 通过免费套餐取消订阅 - 优先使用而非
attach({ product_id: "free" })cancel() - 幂等创建 - 如果客户ID已存在,会返回已有的客户信息
customers.create