DodoPayments, and an asynchronous client, AsyncDodoPayments, both built on httpx. Nested request parameters are typed dictionaries, and responses are Pydantic models.
Installation
Install the SDK with pip:aiohttp extra:
client.webhooks.unwrap(), also install the webhooks extra: pip install "dodopayments[webhooks]".
The SDK requires Python 3.9 or later. Use the latest stable Python release to get security updates.
Quick Start
Synchronous Client
Create a client, then create a checkout session:bearer_token, 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".
Asynchronous Client
AsyncDodoPayments has the same methods as DodoPayments. Await each call:
Core Features
Pythonic Interface
Keyword arguments for parameters,
TypedDict types for nested objects, and Pydantic models for responses.Async/Await
AsyncDodoPayments for asyncio, with aiohttp as an optional HTTP backend.Type Hints
Type hints on every method, for editor autocomplete and type checking with mypy.
Auto-Pagination
List methods return iterators that fetch the next page as you loop.
Configuration
Environment Variables
Store your API key in an environment variable:.env
If
DODO_PAYMENTS_BASE_URL is set and you also pass environment, the constructor raises an “Ambiguous URL” error. To use environment in that case, pass base_url=None.
To verify a webhook, pass the raw request body and headers to client.webhooks.unwrap(payload, headers=headers). It checks the signature with your webhook key and returns the parsed event. client.webhooks.unsafe_unwrap(payload) parses the body without verifying it, so use it only for testing. See Webhooks.
Timeouts
Requests time out after 1 minute by default, with a 5-second connection timeout. Passtimeout in seconds, or an httpx.Timeout for separate read, write, and connect limits:
APITimeoutError. Timed-out requests are retried, so a call can take longer than timeout before it fails.
Retries
Setmax_retries on the client, or on a single request with with_options():
dodopayments.APIError:
The status exceptions inherit from
dodopayments.APIStatusError, which has status_code and response attributes. APITimeoutError is a subclass of APIConnectionError.
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. retrieve_usage_history returns a paginated list, which you can iterate as shown in 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.List and Retrieve Events
Retrieve a single event by itsevent_id, or list events filtered by customer and event name:
usage_events.list also accepts meter_id, start, and end filters.
Pagination
Auto-Pagination
List methods return an iterator that fetches the next page as you loop:Async Pagination
With the async client, loop withasync for:
Manual Pagination
To work with one page at a time, readitems and call has_next_page() and get_next_page(). next_page_info() returns the parameters for the next request:
HTTP Client Configuration
To add a proxy, a custom transport, or otherhttpx settings, pass your own http_client. DefaultHttpxClient keeps the SDK’s default connection limits, timeout, and redirect settings:
client.with_options(http_client=...).
Async with AIOHTTP
By default, the async client sends requests withhttpx. For better concurrency, install the aiohttp extra and pass DefaultAioHttpClient() as http_client:
Logging
The SDK logs with the standard librarylogging module. To turn on logging, set DODO_PAYMENTS_LOG to info:
debug:
Framework Integration
These examples create a checkout session from a web endpoint and return its URL.FastAPI
This endpoint uses the async client:Django
This view uses the sync client: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 Python SDK:- Discord: Join the community server for real-time help.
- Email: Contact support@dodopayments.com.
- GitHub: Open an issue on the repository.