dodo-best-practices by dodopayments/skills
npx skills add https://github.com/dodopayments/skills --skill dodo-best-practices请始终查阅 docs.dodopayments.com 以获取最新的 API 参考和代码示例。
Dodo Payments 是一款用于全球发布、扩展和变现的一体化引擎。专为 SaaS 和 AI 产品设计,它无需额外工程即可处理支付、计费、订阅和分发。
DODO_PAYMENTS_API_KEY - 来自仪表板的 API 密钥DODO_PAYMENTS_WEBHOOK_SECRET - 用于验证的 Webhook 签名密钥https://api.dodopayments.com (默认)https://api.dodopayments.com 并设置 environment: 'test_mode'广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
npm install dodopayments
# 或
yarn add dodopayments
# 或
pnpm add dodopayments
import DodoPayments from 'dodopayments';
const client = new DodoPayments({
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
environment: 'live_mode', // 或 'test_mode'
});
pip install dodopayments
from dodopayments import DodoPayments
client = DodoPayments(bearer_token=os.environ["DODO_PAYMENTS_API_KEY"])
go get github.com/dodopayments/dodopayments-go
import "github.com/dodopayments/dodopayments-go"
client := dodopayments.NewClient(
option.WithBearerToken(os.Getenv("DODO_PAYMENTS_API_KEY")),
)
composer require dodopayments/client
use Dodopayments\Client;
$client = new Client(bearerToken: getenv('DODO_PAYMENTS_API_KEY'));
产品是您销售的商品。在仪表板中或通过 API 创建它们:
信用额度是附加在产品上的虚拟余额(API 调用次数、令牌数、计算小时数)。在仪表板 → 产品 → 信用额度中创建:
收取付款的主要方式。创建一个结账会话并重定向客户:
const session = await client.checkoutSessions.create({
product_cart: [
{ product_id: 'prod_xxxxx', quantity: 1 }
],
customer: {
email: 'customer@example.com',
name: 'John Doe',
},
return_url: 'https://yoursite.com/success',
});
// 将客户重定向至:session.checkout_url
监听事件以获取实时更新:
payment.succeeded - 支付完成payment.failed - 支付失败subscription.active - 订阅激活subscription.cancelled - 订阅取消refund.succeeded - 退款处理完成dispute.opened - 收到争议license_key.created - 许可证密钥已生成credit.added - 向客户授予信用额度credit.deducted - 信用额度被消耗credit.balance_low - 信用额度余额低于阈值payment.succeeded webhook// 为一次性支付创建结账
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: 'prod_one_time_product', quantity: 1 }],
customer: { email: 'customer@example.com' },
return_url: 'https://yoursite.com/success',
});
subscription.active webhook 以授予访问权限subscription.cancelled webhook 以撤销访问权限// 为订阅创建结账
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: 'prod_monthly_subscription', quantity: 1 }],
subscription_data: { trial_period_days: 14 }, // 可选试用期
customer: { email: 'customer@example.com' },
return_url: 'https://yoursite.com/success',
});
始终验证 Webhook 签名:
import crypto from 'crypto';
function verifyWebhook(payload: string, signature: string, secret: string): boolean {
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
}
允许客户管理其订阅:
const portal = await client.customers.createPortalSession({
customer_id: 'cust_xxxxx',
return_url: 'https://yoursite.com/account',
});
// 重定向至:portal.url
优雅地处理 API 错误:
try {
const session = await client.checkoutSessions.create({...});
} catch (error) {
if (error.status === 400) {
// 无效请求 - 检查参数
} else if (error.status === 401) {
// 无效的 API 密钥
} else if (error.status === 429) {
// 速率限制 - 实现退避策略
}
}
sk_test_ 开头)4242 4242 4242 4242 - 成功4000 0000 0000 0002 - 拒绝使用 ngrok 或类似工具进行 Webhook 测试:
ngrok http 3000
然后在仪表板中将 ngrok URL 配置为您的 Webhook 端点。
使用 API 路由进行服务器端操作:
// app/api/checkout/route.ts
import { NextResponse } from 'next/server';
import DodoPayments from 'dodopayments';
const client = new DodoPayments({
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
});
export async function POST(req: Request) {
const { productId, email } = await req.json();
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: productId, quantity: 1 }],
customer: { email },
return_url: `${process.env.NEXT_PUBLIC_URL}/success`,
});
return NextResponse.json({ url: session.checkout_url });
}
import express from 'express';
import DodoPayments from 'dodopayments';
const app = express();
const client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY! });
app.post('/create-checkout', async (req, res) => {
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: req.body.productId, quantity: 1 }],
customer: { email: req.body.email },
return_url: 'https://yoursite.com/success',
});
res.json({ url: session.checkout_url });
});
每周安装量
214
仓库
GitHub Stars
7
首次出现
2026年1月21日
安全审计
安装于
opencode178
gemini-cli175
codex170
github-copilot163
cursor146
amp143
Always consultdocs.dodopayments.com for the latest API reference and code examples.
Dodo Payments is the all-in-one engine to launch, scale, and monetize worldwide. Designed for SaaS and AI products, it handles payments, billing, subscriptions, and distribution without extra engineering.
DODO_PAYMENTS_API_KEY - Your API key from the dashboardDODO_PAYMENTS_WEBHOOK_SECRET - Webhook signing secret for verificationhttps://api.dodopayments.com (default)https://api.dodopayments.com with environment: 'test_mode'npm install dodopayments
# or
yarn add dodopayments
# or
pnpm add dodopayments
import DodoPayments from 'dodopayments';
const client = new DodoPayments({
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
environment: 'live_mode', // or 'test_mode'
});
pip install dodopayments
from dodopayments import DodoPayments
client = DodoPayments(bearer_token=os.environ["DODO_PAYMENTS_API_KEY"])
go get github.com/dodopayments/dodopayments-go
import "github.com/dodopayments/dodopayments-go"
client := dodopayments.NewClient(
option.WithBearerToken(os.Getenv("DODO_PAYMENTS_API_KEY")),
)
composer require dodopayments/client
use Dodopayments\Client;
$client = new Client(bearerToken: getenv('DODO_PAYMENTS_API_KEY'));
Products are the items you sell. Create them in the dashboard or via API:
Credits are virtual balances (API calls, tokens, compute hours) attached to products. Create them in Dashboard → Products → Credits:
The primary way to collect payments. Create a checkout session and redirect customers:
const session = await client.checkoutSessions.create({
product_cart: [
{ product_id: 'prod_xxxxx', quantity: 1 }
],
customer: {
email: 'customer@example.com',
name: 'John Doe',
},
return_url: 'https://yoursite.com/success',
});
// Redirect customer to: session.checkout_url
Listen to events for real-time updates:
payment.succeeded - Payment completedpayment.failed - Payment failedsubscription.active - Subscription activatedsubscription.cancelled - Subscription cancelledrefund.succeeded - Refund processeddispute.opened - Dispute receivedlicense_key.created - License key generatedcredit.added - Credits granted to customercredit.deducted - Credits consumedpayment.succeeded webhook// Create checkout for one-time payment
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: 'prod_one_time_product', quantity: 1 }],
customer: { email: 'customer@example.com' },
return_url: 'https://yoursite.com/success',
});
subscription.active webhook to grant accesssubscription.cancelled to revoke access// Create checkout for subscription
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: 'prod_monthly_subscription', quantity: 1 }],
subscription_data: { trial_period_days: 14 }, // Optional trial
customer: { email: 'customer@example.com' },
return_url: 'https://yoursite.com/success',
});
Always verify webhook signatures:
import crypto from 'crypto';
function verifyWebhook(payload: string, signature: string, secret: string): boolean {
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
}
Allow customers to manage their subscriptions:
const portal = await client.customers.createPortalSession({
customer_id: 'cust_xxxxx',
return_url: 'https://yoursite.com/account',
});
// Redirect to: portal.url
Handle API errors gracefully:
try {
const session = await client.checkoutSessions.create({...});
} catch (error) {
if (error.status === 400) {
// Invalid request - check parameters
} else if (error.status === 401) {
// Invalid API key
} else if (error.status === 429) {
// Rate limited - implement backoff
}
}
sk_test_)4242 4242 4242 4242 - Success4000 0000 0000 0002 - DeclineUse ngrok or similar for webhook testing:
ngrok http 3000
Then configure the ngrok URL as your webhook endpoint in the dashboard.
Use API routes for server-side operations:
// app/api/checkout/route.ts
import { NextResponse } from 'next/server';
import DodoPayments from 'dodopayments';
const client = new DodoPayments({
bearerToken: process.env.DODO_PAYMENTS_API_KEY!,
});
export async function POST(req: Request) {
const { productId, email } = await req.json();
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: productId, quantity: 1 }],
customer: { email },
return_url: `${process.env.NEXT_PUBLIC_URL}/success`,
});
return NextResponse.json({ url: session.checkout_url });
}
import express from 'express';
import DodoPayments from 'dodopayments';
const app = express();
const client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY! });
app.post('/create-checkout', async (req, res) => {
const session = await client.checkoutSessions.create({
product_cart: [{ product_id: req.body.productId, quantity: 1 }],
customer: { email: req.body.email },
return_url: 'https://yoursite.com/success',
});
res.json({ url: session.checkout_url });
});
Weekly Installs
214
Repository
GitHub Stars
7
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode178
gemini-cli175
codex170
github-copilot163
cursor146
amp143
Lark Drive API 使用指南:飞书云文档、Wiki、表格 Token 处理与文件管理
23,400 周安装
credit.balance_low