Use this file to discover all available pages before exploring further.
The TypeScript SDK provides convenient server-side access to the Dodo Payments REST API for TypeScript and JavaScript applications. It features comprehensive type definitions, error handling, retries, timeouts, and auto-pagination for seamless payment processing.
Initialize the client with your API key and start processing payments:
import DodoPayments from 'dodopayments';const client = new DodoPayments({ bearerToken: process.env['DODO_PAYMENTS_API_KEY'], // This is the default and can be omitted environment: 'test_mode', // defaults to 'live_mode'});const checkoutSessionResponse = await client.checkoutSessions.create({ product_cart: [{ product_id: 'product_id', quantity: 1 }],});console.log(checkoutSessionResponse.session_id);
Always store your API keys securely using environment variables. Never commit them to version control or expose them in client-side code.
// Create a subscriptionconst subscription = await client.subscriptions.create({ billing: { country: 'US', city: 'San Francisco', state: 'CA', street: '1 Market St', zipcode: '94105', }, customer: { customer_id: 'cus_123', // or pass { email, name } to create a new customer }, product_id: 'pdt_456', quantity: 1,});// Charge an on-demand subscription// product_price is in the lowest currency denomination (e.g., 2500 = $25.00 USD)const chargeResponse = await client.subscriptions.charge(subscription.subscription_id, { product_price: 2500,});// Retrieve subscription usage history (for metered subscriptions)const usageHistory = await client.subscriptions.retrieveUsageHistory(subscription.subscription_id, { start_date: '2024-01-01T00:00:00Z', end_date: '2024-03-31T23:59:59Z',});
billing يتطلب الحد الأدنى من رمز البلد ISO ذو الحرفين. customer هو اتحاد من { customer_id } (لإرفاق عميل موجود) أو { email, name? } (لإنشاء عميل جديد). product_price يُعبر عنها بأصغر وحدة نقدية.
يجب أن تحتوي الأحداث على قيم event_id فريدة لضمان عدم التكرار. يتم رفض المعرفات المكررة ضمن نفس الطلب، وتُتجاهل الطلبات اللاحقة التي تحتوي على معرفات موجودة بالفعل.
// Get a specific eventconst event = await client.usageEvents.retrieve('api_call_12345');// List events with filteringconst events = await client.usageEvents.list({ customer_id: 'cus_abc123', event_name: 'api_request', start: '2024-01-14T10:30:00Z', end: '2024-01-15T10:30:00Z'});
import DodoPayments from 'dodopayments';import * as undici from 'undici';const proxyAgent = new undici.ProxyAgent('http://localhost:8888');const client = new DodoPayments({ fetchOptions: { dispatcher: proxyAgent, },});
التحكم في وضوح السجلات باستخدام متغيرات البيئة أو خيارات العميل:
// Via client optionconst client = new DodoPayments({ logLevel: 'debug', // Show all log messages});
# Via environment variableexport DODO_PAYMENTS_LOG=debug
مستويات السجل المتاحة:
'debug' - عرض رسائل التصحيح، المعلومات، التحذيرات، والأخطاء
'info' - عرض رسائل المعلومات، التحذيرات، والأخطاء
'warn' - عرض التحذيرات والأخطاء (افتراضي)
'error' - عرض الأخطاء فقط
'off' - تعطيل جميع السجلات
في مستوى التصحيح، يتم تسجيل جميع طلبات واستجابات HTTP، بما في ذلك الرؤوس والهيئات. يتم حجب بعض رؤوس المصادقة، ولكن قد تظل البيانات الحساسة مرئية في الهيئات.