Loading...
Loading...
Comprehensive guide for integrating Dodo Payments - the all-in-one payment and billing platform for SaaS and AI products.
npx skill4agent add dodopayments/skills dodo-best-practicesDODO_PAYMENTS_API_KEYDODO_PAYMENTS_WEBHOOK_SECREThttps://api.dodopayments.comhttps://api.dodopayments.comenvironment: 'test_mode'npm install dodopayments
# or
yarn add dodopayments
# or
pnpm add dodopaymentsimport DodoPayments from 'dodopayments';
const client = new DodoPayments({
bearerToken: process.env.DODO_PAYMENTS_API_KEY,
environment: 'live_mode', // or 'test_mode'
});pip install dodopaymentsfrom dodopayments import DodoPayments
client = DodoPayments(bearer_token=os.environ["DODO_PAYMENTS_API_KEY"])go get github.com/dodopayments/dodopayments-goimport "github.com/dodopayments/dodopayments-go"
client := dodopayments.NewClient(
option.WithBearerToken(os.Getenv("DODO_PAYMENTS_API_KEY")),
)composer require dodopayments/clientuse Dodopayments\Client;
$client = new Client(bearerToken: getenv('DODO_PAYMENTS_API_KEY'));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_urlpayment.succeededpayment.failedsubscription.activesubscription.cancelledrefund.succeededdispute.openedlicense_key.createdpayment.succeeded// 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.activesubscription.cancelled// 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',
});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',
});
// Redirect to: portal.urltry {
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 42424000 0000 0000 0002ngrok http 3000// 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 });
});