context.Context, request parameters use a Field wrapper that separates zero values from omitted fields, and you can add middleware to every request.
Installation
Add the module to your project:The SDK requires Go 1.22 or later.
Quick Start
Create a client, then create a checkout session:option.WithBearerToken, NewClient reads the DODO_PAYMENTS_API_KEY environment variable. If you omit option.WithEnvironmentTestMode(), the client connects to live mode. A test mode API key works only in test mode.
Core Features
Context Support
Every method takes a
context.Context for cancellation and timeouts.Strong Typing
Typed request parameters and response structs for compile-time checks.
Middleware
Add middleware with
option.WithMiddleware for logging, metrics, and custom logic.Goroutine Safe
Share one client across goroutines.
Configuration
NewClient reads DODO_PAYMENTS_API_KEY, DODO_PAYMENTS_WEBHOOK_KEY (your webhook signing secret), and DODO_PAYMENTS_BASE_URL from the environment. Options you pass, such as option.WithBearerToken, option.WithWebhookKey, and option.WithBaseURL, override them.
To verify a webhook, pass the raw request body and headers to client.Webhooks.Unwrap(rawBody, r.Header). 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.
The examples on this page use the client from Quick Start.
Context and Timeouts
Requests don’t time out by default. A context deadline limits the whole call, including retries. To limit each attempt, addoption.WithRequestTimeout():
Retry Configuration
The SDK retries connection errors and responses with status 408, 409, 429, or 500 and above. It retries twice by default, with exponential backoff. Setoption.WithMaxRetries on the client or on a single request:
Common Operations
The examples in this section also use a context, for examplectx := context.Background().
Create a Checkout Session
Create a checkout session, then redirect the customer to the returnedCheckoutURL:
Manage Customers
Create a customer with an email address and name, then retrieve it by ID. Metadata values use the union types from theshared package:
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 is a CustomerRequestUnionParam: pass AttachExistingCustomerParam{CustomerID: ...} for an existing customer or NewCustomerParam{Email: ..., Name: ...} to create one. Charge is for on-demand subscriptions, and ProductPrice is in the smallest currency unit. GetUsageHistory returns one page of results; GetUsageHistoryAutoPaging iterates every page.Usage-Based Billing
Ingest Usage Events
Send usage events for a customer:The
EventID is the idempotency key, so give each event a unique value. If the same EventID appears twice in one request, the whole request is rejected. If an EventID 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 Usage Events
List events filtered by customer and event name:List returns one page. To iterate every page, call client.UsageEvents.ListAutoPaging(ctx, params) and loop with iter.Next(), iter.Current(), and iter.Err(). Other list methods have the same AutoPaging variant, and each page has a GetNextPage() method.
Error Handling
When the API returns a non-success status code, the SDK returns an error of type*dodopayments.Error. It has the StatusCode, the *http.Request and *http.Response, and the JSON of the error body. Use errors.As to inspect it, and branch on StatusCode to handle specific cases:
*url.Error that wraps a *net.OpError. apiErr.DumpRequest(true) returns the serialized request.
Middleware
Add middleware withoption.WithMiddleware. A middleware receives each request and a next function that sends it:
option.WithMiddleware call run left to right. Middleware passed to NewClient runs before middleware passed to a single request.
Concurrency
The client is safe for concurrent use, so you can share one client across goroutines: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 Go SDK:- Discord: Join the community server for real-time help.
- Email: Contact support@dodopayments.com.
- GitHub: Open an issue on the repository.