net/http and a connection pool, retries failed requests, iterates through paginated lists for you, and ships RBI and RBS type definitions.
Installation
Add the gem to your Gemfile:Gemfile
The SDK requires Ruby 3.2.0 or later.
Quick Start
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".
Core Features
Ruby Conventions
Snake_case methods and keyword arguments, with plain hashes accepted for nested parameters.
Elegant Syntax
Responses are objects with attribute readers, and
obj[:prop] also reads fields the SDK doesn’t define.Auto-Pagination
auto_paging_each iterates over every item and fetches the next page when needed.Type Safety
RBI definitions for Sorbet, with no dependency on
sorbet-runtime.Configuration
Dodopayments::Client.new takes bearer_token, webhook_key, environment, base_url, max_retries, timeout, initial_retry_delay, and max_retry_delay. When you omit them, it reads DODO_PAYMENTS_API_KEY, DODO_PAYMENTS_WEBHOOK_KEY (your webhook signing secret), and DODO_PAYMENTS_BASE_URL from the environment. The client is thread-safe and keeps its own connection pool, so create one client for your application and reuse it.
To verify a webhook, pass the raw request body and headers to dodo_payments.webhooks.unwrap(payload, headers: headers). It checks the signature with your webhook key and returns the parsed event. dodo_payments.webhooks.unsafe_unwrap(payload) parses the body without verifying it, so use it only for testing. See Webhooks.
Timeout Configuration
Requests time out after 60 seconds by default. Settimeout, in seconds, on the client or on a single request:
Dodopayments::Errors::APITimeoutError. Timed-out requests are retried by default.
Retry Configuration
The SDK retries connection errors, timeouts, and responses with status 408, 409, 429, or 500 and above. It retries twice by default, with a short exponential backoff. Setmax_retries on the client or on a single request:
Common Operations
The examples in this section use thedodo_payments client from Quick Start.
Create a Checkout Session
Create a checkout session, then redirect the customer to the returnedcheckout_url:
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 update a subscription’s metadata.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.Pagination
Auto-Pagination
List methods return a page. Readitems for the current page, or call auto_paging_each to iterate over every item. It fetches the next page when it needs it:
Manual Pagination
To move one page at a time, callnext_page? and next_page:
Error Handling
When the SDK can’t connect to the API, or the API returns a 4xx or 5xx status, the SDK raises a subclass ofDodopayments::Errors::APIError:
status, headers, and body attributes:
Type Safety with Sorbet
The SDK ships RBI definitions and doesn’t depend onsorbet-runtime. To get type-checked request parameters, pass model classes instead of hashes:
Advanced Usage
Undocumented Endpoints
To call an endpoint that has no SDK method, userequest. It applies the same authentication and retries as the SDK methods:
Undocumented Parameters
To send parameters that the SDK doesn’t define, pass them inrequest_options. An extra_* parameter that has the same name as a documented parameter overrides it:
Rails Integration
Create an Initializer
Create one client when Rails starts, inconfig/initializers/dodo_payments.rb:
Service Object Pattern
Wrap the client in a service object:Controller Integration
Call the service from a controller and redirect to the checkout page:Sinatra Integration
Create the client once in aconfigure block and use it in your routes:
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 Ruby SDK:- Discord: Join the community server for real-time help.
- Email: Contact support@dodopayments.com.
- GitHub: Open an issue on the repository.