Installation
Install thedodopayments package with your package manager:
Quick Start
Create a client, then create a checkout session:bearerToken, the client reads the DODO_PAYMENTS_API_KEY environment variable. If you omit environment, the client connects to live mode. A test mode API key works only with environment: 'test_mode'.
Core Features
TypeScript First
Type definitions for every request parameter and response field, shown in your editor.
Auto-Pagination
List methods fetch the next page for you when you iterate with
for await...of.Error Handling
A typed error class for each HTTP error status, with the status, headers, and response body.
Smart Retries
Two retries by default, with exponential backoff, for connection errors and retryable status codes.
Configuration
Environment Variables
Store your API key in an environment variable:.env
If a base URL is set and you also pass
environment, the constructor throws an “Ambiguous URL” error. To use environment in that case, pass baseURL: null.
To verify a webhook, pass the raw request body and headers to client.webhooks.unwrap(rawBody, { headers }). It checks the signature with your webhook key and returns the parsed event. client.webhooks.unsafeUnwrap(rawBody) parses the body without verifying it, so use it only for testing. See Webhooks.
Timeout Configuration
Requests time out after 1 minute by default. Settimeout, in milliseconds, on the client or on a single request:
APIConnectionTimeoutError. Timed-out requests are retried, so a call can take longer than timeout before it fails.
Retry Configuration
SetmaxRetries on the client or on a single request:
DodoPayments.APIError. Each error has status, headers, and error (the response body) properties. Check for a specific class with instanceof, for example err instanceof DodoPayments.RateLimitError:
Common Operations
The examples in this section use theclient from Quick Start.
Create a Checkout Session
Create a checkout session, then redirect the customer to the returnedcheckout_url:
checkout_url works once and expires after 24 hours. For every session option, see Checkout Sessions.
Manage Customers
Create a customer with an email address and name, then retrieve it by ID:Handle Subscriptions
Create a subscription, charge an on-demand subscription, and read a subscription’s usage history.billing requires only country, a two-letter ISO country code. customer takes { customer_id } to attach an existing customer or { email, name? } to create one. charge is for on-demand subscriptions, and product_price is in the smallest currency unit. retrieveUsageHistory returns a paginated list, which you can iterate as shown in Auto-Pagination.Usage-Based Billing
Ingest Usage Events
Send usage events for a customer:The
event_id is the idempotency key, so give each event a unique value. If the same event_id appears twice in one request, the whole request is rejected. If an event_id was already ingested, the new event is ignored. A request accepts up to 1,000 events. timestamp defaults to the current time and is rejected if it’s more than 1 hour in the past or more than 5 minutes in the future.Retrieve Usage Events
Retrieve a single event by itsevent_id, or list events filtered by customer, event name, and time range:
usageEvents.list also accepts meter_id, and returns a paginated list.
Proxy Configuration
To send requests through a proxy, pass your runtime’s proxy settings infetchOptions.
Node.js (Using Undici)
Pass an undiciProxyAgent as the dispatcher:
Bun
Set theproxy option:
Deno
Create an HTTP client withDeno.createHttpClient and pass it as client:
Logging
Set the log level with thelogLevel client option or the DODO_PAYMENTS_LOG environment variable. The client option overrides the environment variable.
'debug': Debug messages, info, warnings, and errors.'info': Info messages, warnings, and errors.'warn': Warnings and errors. This is the default.'error': Errors only.'off': No logging.
console by default. To use pino, winston, or another logging library, pass your logger as the logger option; logLevel still controls which messages reach it. Log messages are for debugging only, and their format can change between releases.
Migration from Node.js SDK
If you use the legacy Node.js SDK, follow the migration guide to upgrade. The current SDK uses the built-infetch API instead of node-fetch, requires Node.js 20, TypeScript 4.9, and Jest 28 or later, and includes a migration tool that updates most of your code.
View Migration Guide
Learn how to migrate from the Node.js SDK to the TypeScript SDK
Auto-Pagination
List methods return paginated results. Iterate withfor await...of to get items from every page. The SDK requests the next page when it needs it:
page.items and call hasNextPage() and getNextPage():
page_size to the list method, for example client.payments.list({ page_size: 50 }).
Requirements
The SDK supports TypeScript 4.9 or later and these runtimes:- Web browsers (up-to-date Chrome, Firefox, Safari, Edge, and others)
- Node.js 20 LTS or later (non-EOL) versions
- Deno 1.28.0 or later
- Bun 1.0 or later
- Cloudflare Workers
- Vercel Edge Runtime
- Jest 28 or later with the
"node"environment (the"jsdom"environment isn’t supported) - Nitro 2.6 or later
Resources
GitHub Repository
Source code, releases, and the full method list.
API Reference
Every endpoint, parameter, and response.
Discord Community
Ask questions and talk with other developers.
Report Issues
Report bugs or request features.
Support
For help with the TypeScript SDK:- Discord: Join the community server for real-time help.
- Email: Contact support@dodopayments.com.
- GitHub: Open an issue on the repository.