DodoPayments와 비동기 클라이언트인 AsyncDodoPayments를 제공하며, 두 클라이언트 모두 httpx를 기반으로 합니다. 중첩된 request parameter는 타입이 지정된 dictionary이고, response는 Pydantic model입니다.
설치
pip로 SDK를 설치합니다:aiohttp extra를 설치합니다:
client.webhooks.unwrap()로 webhook signature를 검증하려면 webhooks extra도 설치합니다: pip install "dodopayments[webhooks]".
SDK에는 Python 3.9 이상이 필요합니다. 보안 업데이트를 받으려면 최신 stable Python release를 사용하세요.
빠른 시작
동기 클라이언트
client를 생성한 다음 checkout session을 생성합니다:bearer_token를 생략하면 client가 DODO_PAYMENTS_API_KEY 환경 변수를 읽습니다. environment를 생략하면 client가 live mode에 연결합니다. test mode API key는 environment="test_mode"에서만 작동합니다.
비동기 클라이언트
AsyncDodoPayments에는 DodoPayments와 동일한 method가 있습니다. 각 호출을 await하세요:
핵심 기능
Pythonic Interface
parameter에는 keyword argument를, 중첩된 object에는
TypedDict type을, response에는 Pydantic model을 사용합니다.Async/Await
asyncio용
AsyncDodoPayments를 제공하며, aiohttp를 optional HTTP backend로 사용할 수 있습니다.Type Hints
모든 method에 type hint를 제공하므로 editor autocomplete와 mypy를 사용한 type checking이 가능합니다.
Auto-Pagination
List method는 반복할 때 다음 page를 가져오는 iterator를 반환합니다.
Configuration
Environment Variables
API key를 environment variable에 저장합니다:.env
DODO_PAYMENTS_BASE_URL가 설정되어 있고 environment도 전달하면 constructor가 “Ambiguous URL” error를 발생시킵니다. 이 경우 environment를 사용하려면 base_url=None를 전달하세요.
webhook을 검증하려면 raw request body와 header를 client.webhooks.unwrap(payload, headers=headers)에 전달하세요. 이 method는 webhook key로 signature를 확인하고 parsed event를 반환합니다. client.webhooks.unsafe_unwrap(payload)는 검증 없이 body를 parse하므로 testing에만 사용하세요. Webhooks을 참조하세요.
Timeouts
Request는 기본적으로 1분 후 timeout되며 connection timeout은 5초입니다.timeout를 초 단위로 전달하거나, read·write·connect limit을 পৃথ도로 설정하려면 httpx.Timeout를 전달하세요:
APITimeoutError를 발생시킵니다. timeout된 request는 retry되므로 실패하기 전까지 timeout보다 오래 걸릴 수 있습니다.
Retries
client에서max_retries를 설정하거나, 단일 request에서 with_options()를 사용하세요:
dodopayments.APIError의 subclass를 발생시킵니다:
status exception은
dodopayments.APIStatusError를 상속하며, 여기에는 status_code 및 response attribute가 있습니다. APITimeoutError는 APIConnectionError의 subclass입니다.
일반적인 작업
이 section의 example은 빠른 시작의client를 사용합니다.
Checkout Session 생성
checkout session을 생성한 다음 반환된checkout_url로 customer를 redirect합니다:
checkout_url는 한 번만 작동하며 24시간 후 만료됩니다. 모든 session option은 Checkout Sessions을 참조하세요.
Customer 관리
email address와 name으로 customer를 생성한 다음 ID로 조회합니다:Subscription 처리
subscription을 생성하고, on-demand subscription에 요금을 청구하며, subscription의 usage history를 읽습니다.billing에는 두 글자 ISO country code인 country만 필요합니다. customer는 기존 customer를 연결하려면 {"customer_id": ...}를, customer를 생성하려면 {"email": ..., "name": ...}를 사용합니다. charge는 on-demand subscription에 사용하며, product_price는 currency의 smallest unit입니다. retrieve_usage_history는 paginated list를 반환하며, Pagination에 나온 것처럼 반복할 수 있습니다.Usage-Based Billing
Usage Event 수집
customer의 usage event를 전송합니다:event_id는 idempotency key이므로 각 event에 고유한 값을 지정하세요. 동일한 event_id가 하나의 request에 두 번 나타나면 전체 request가 거부됩니다. event_id가 이미 수집된 경우 새 event는 무시됩니다. 하나의 request에는 최대 1,000개의 event를 포함할 수 있습니다. timestamp는 현재 시간으로 기본 설정되며, 1시간보다 과거이거나 5분보다 미래인 경우 거부됩니다.Event 나열 및 조회
event_id로 단일 event를 조회하거나 customer와 event name으로 filtering한 event를 나열합니다:
usage_events.list는 meter_id, start 및 end filter도 허용합니다.
Pagination
Auto-Pagination
List method는 반복할 때 다음 page를 가져오는 iterator를 반환합니다:Async Pagination
async client에서는async for를 사용해 반복합니다:
Manual Pagination
한 번에 하나의 page를 처리하려면items를 읽고 has_next_page() 및 get_next_page()를 호출하세요. next_page_info()는 다음 request에 사용할 parameter를 반환합니다:
HTTP Client Configuration
proxy, custom transport 또는 기타httpx 설정을 추가하려면 자체 http_client를 전달하세요. DefaultHttpxClient는 SDK의 기본 connection limit, timeout 및 redirect 설정을 유지합니다:
client.with_options(http_client=...)를 호출하세요.
AIOHTTP를 사용한 Async
기본적으로 async client는httpx로 request를 전송합니다. 더 나은 concurrency를 위해 aiohttp extra를 설치하고 DefaultAioHttpClient()를 http_client로 전달하세요:
Logging
SDK는 standard library의logging module로 logging합니다. logging을 활성화하려면 DODO_PAYMENTS_LOG를 info로 설정하세요:
debug로 설정하세요:
Framework Integration
이 example은 web endpoint에서 checkout session을 생성하고 URL을 반환합니다.FastAPI
이 endpoint는 async client를 사용합니다:Django
이 view는 sync client를 사용합니다:Resources
GitHub Repository
Source code, release 및 전체 method list입니다.
API Reference
모든 endpoint, parameter 및 response입니다.
Discord Community
질문하고 다른 developer와 소통하세요.
Report Issues
bug를 report하거나 feature를 요청하세요.
지원
Python SDK에 대한 도움말은 다음을 참조하세요:- Discord: 실시간 도움을 받으려면 community server에 참여하세요.
- Email: support@dodopayments.com으로 문의하세요.
- GitHub: repository에 issue를 등록하세요.