설치
패키지 관리자를 사용하여dodopayments package를 설치하세요:
빠른 시작
client를 생성한 다음 checkout session을 생성하세요:bearerToken를 생략하면 client가 DODO_PAYMENTS_API_KEY environment variable을 읽습니다. environment를 생략하면 client가 live mode에 연결됩니다. test mode API key는 environment: 'test_mode'에서만 작동합니다.
핵심 기능
TypeScript First
모든 request parameter와 response field에 대한 type definition이 editor에 표시됩니다.
Auto-Pagination
for await...of로 순회하면 list method가 다음 page를 자동으로 가져옵니다.Error Handling
각 HTTP error status에 대한 typed error class를 제공하며, status, headers 및 response body가 포함됩니다.
Smart Retries
connection error 및 retry 가능한 status code에 대해 exponential backoff를 사용하여 기본적으로 두 번 retry합니다.
Configuration
Environment Variables
API key를 environment variable에 저장하세요:.env
base URL이 설정되어 있고
environment도 전달하면 constructor가 “Ambiguous URL” error를 발생시킵니다. 이 경우 environment를 사용하려면 baseURL: null를 전달하세요.
webhook을 검증하려면 raw request body와 headers를 client.webhooks.unwrap(rawBody, { headers })에 전달하세요. webhook key로 signature를 확인하고 parsed event를 반환합니다. client.webhooks.unsafeUnwrap(rawBody)는 검증 없이 body를 parsing하므로 testing에만 사용하세요. Webhooks를 참조하세요.
Timeout Configuration
Request는 기본적으로 1분 후 timeout됩니다. client 또는 single request에 밀리초 단위로timeout를 설정하세요:
APIConnectionTimeoutError를 throw합니다. timeout된 request는 retry되므로 call이 실패하기까지 timeout보다 오래 걸릴 수 있습니다.
Retry Configuration
client 또는 single request에maxRetries를 설정하세요:
DodoPayments.APIError의 subclass를 throw합니다. 각 error에는 status, headers 및 error(response body) property가 있습니다. instanceof로 특정 class인지 확인할 수 있습니다. 예: err instanceof DodoPayments.RateLimitError:
일반적인 작업
이 section의 예제에서는 빠른 시작의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을 charge하며, subscription의 usage history를 조회합니다.billing에는 두 글자의 ISO country code인 country만 필요합니다. customer에는 기존 customer를 연결하기 위한 { customer_id } 또는 새 customer를 생성하기 위한 { email, name? }를 전달할 수 있습니다. charge는 on-demand subscriptions에 사용되며, product_price는 통화의 최소 단위입니다. retrieveUsageHistory는 paginated list를 반환하며, Auto-Pagination에 설명된 것처럼 순회할 수 있습니다.Usage-Based Billing
Usage Event 수집
customer에 대한 usage event를 전송하세요:event_id는 idempotency key이므로 각 event에 고유한 값을 지정하세요. 하나의 request에서 동일한 event_id가 두 번 나타나면 전체 request가 거부됩니다. event_id가 이미 수집된 경우 새 event는 무시됩니다. 하나의 request에는 최대 1,000개의 event를 포함할 수 있습니다. timestamp는 현재 시간으로 기본 설정되며, 1시간보다 과거이거나 5분보다 미래이면 거부됩니다.Usage Event 조회
event_id로 단일 event를 조회하거나 customer, event name 및 time range로 filtering한 event 목록을 가져오세요:
usageEvents.list는 meter_id도 허용하며 paginated list를 반환합니다.
Proxy Configuration
proxy를 통해 request를 전송하려면 runtime의 proxy settings를fetchOptions에 전달하세요.
Node.js (Undici 사용)
undiciProxyAgent를 dispatcher로 전달하세요:
Bun
proxy option을 설정하세요:
Deno
Deno.createHttpClient로 HTTP client를 생성하고 client로 전달하세요:
Logging
logLevel client option 또는 DODO_PAYMENTS_LOG environment variable로 log level을 설정하세요. client option이 environment variable보다 우선합니다.
'debug': Debug message, info, warning 및 error입니다.'info': Info message, warning 및 error입니다.'warn': Warning 및 error입니다. 기본값입니다.'error': Error만 기록합니다.'off': Logging하지 않습니다.
console에 logging합니다. pino, winston 또는 다른 logging library를 사용하려면 logger를 logger option으로 전달하세요. logLevel는 여전히 전달되는 message를 제어합니다. Log message는 debugging 용도로만 사용되며 format은 release 간에 변경될 수 있습니다.
Node.js SDK에서 마이그레이션
legacy Node.js SDK를 사용 중이라면 migration guide에 따라 upgrade하세요. 현재 SDK는node-fetch 대신 기본 제공되는 fetch API를 사용하고, Node.js 20, TypeScript 4.9 및 Jest 28 이상이 필요하며, 대부분의 code를 업데이트하는 migration tool을 포함합니다.
View Migration Guide
Node.js SDK에서 TypeScript SDK로 마이그레이션하는 방법 알아보기
Auto-Pagination
List method는 paginated result를 반환합니다.for await...of로 순회하여 모든 page의 item을 가져오세요. SDK는 필요할 때 다음 page를 request합니다:
page.items를 읽고 hasNextPage() 및 getNextPage()를 호출하세요:
page_size를 전달하세요. 예: client.payments.list({ page_size: 50 }).
Requirements
SDK는 TypeScript 4.9 이상 및 다음 runtime을 지원합니다:- Web browser(최신 Chrome, Firefox, Safari, Edge 및 기타 브라우저)
- Node.js 20 LTS 이상(non-EOL version)
- Deno 1.28.0 이상
- Bun 1.0 이상
- Cloudflare Workers
- Vercel Edge Runtime
"node"environment에서 Jest 28 이상("jsdom"environment는 지원되지 않음)- Nitro 2.6 이상
Resources
GitHub Repository
Source code, release 및 전체 method 목록입니다.
API Reference
모든 endpoint, parameter 및 response입니다.
Discord Community
질문하고 다른 developer와 대화하세요.
Report Issues
bug를 신고하거나 feature를 요청하세요.
Support
TypeScript SDK에 대한 도움이 필요하면 다음을 참조하세요:- Discord: 실시간 지원을 받으려면 community server에 참여하세요.
- Email: support@dodopayments.com으로 문의하세요.
- GitHub: repository에 issue를 등록하세요.