Task, requests and responses are typed classes, and the client retries failed requests for you.
Installation
Install the package from NuGet:The SDK requires .NET Standard 2.0 or later, and it also ships a .NET 8 build. It works with ASP.NET Core, console applications, and other .NET project types. The examples on this page use C# 12 syntax, such as collection expressions.
Quick Start
Create a client, then create a checkout session:BearerToken, the client reads the DODO_PAYMENTS_API_KEY environment variable. If you don’t set BaseUrl or DODO_PAYMENTS_BASE_URL, the client connects to live mode. To use test mode, see Environments. A test mode API key works only in test mode.
Core Features
Async/Await
Every API method returns a
Task and accepts an optional CancellationToken.Strong Typing
Typed request and response classes, with nullable reference type annotations.
Smart Retries
Two retries by default, with exponential backoff, for connection errors and retryable status codes.
Error Handling
An exception class for each common HTTP error status, with the status code and response body.
Configuration
Environment Variables
Store your API key in an environment variable:.env
new() reads its settings from the environment:
If neither
BearerToken nor DODO_PAYMENTS_API_KEY is set, the client throws DodoPaymentsInvalidDataException. WebhookKey holds your webhook signing secret, but the C# SDK has no method that verifies webhook signatures. To verify them, follow Webhooks.
Manual Configuration
Set properties on the client to override the environment variables:Environments
The client connects to live mode (https://live.dodopayments.com) by default. To use test mode (https://test.dodopayments.com), set BaseUrl to EnvironmentUrl.TestMode:
Retries
The SDK retries connection errors and responses with status 408, 409, 429, or 500 and above. It retries twice by default, with exponential backoff. SetMaxRetries to change the number of retries, or set it to 0 to turn retries off:
Timeouts
Each request attempt times out after 1 minute by default. The timeout doesn’t include retries. SetTimeout to change it:
Per-Request Overrides
To change settings for a single call, callWithOptions on the client or on a service. It returns a modified copy that shares the same connection pool, and the original client doesn’t change:
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 returnedCheckoutUrl:
Manage Customers
Create a customer with an email address and name, then retrieve it by ID:Customers.Retrieve also accepts the ID as a string, for example client.Customers.Retrieve("cus_123").
Handle Subscriptions
Create a subscription, then charge it if it’s an on-demand subscription.Billing requires only Country, a two-letter ISO country code. Customer takes an AttachExistingCustomer to attach an existing customer or a NewCustomer to create one. Charge is for on-demand subscriptions, and ProductPrice is in the smallest currency unit.Error Handling
When the API returns an error status, the SDK throws a subclass ofDodoPaymentsApiException, which has StatusCode and ResponseBody properties. The exception class depends on the status code. All 4xx exceptions inherit from DodoPayments4xxException.
A 4xx status without its own class, such as 409, throws
DodoPayments4xxException. DodoPaymentsUnexpectedStatusCodeException covers statuses outside the 4xx and 5xx ranges.
The SDK also throws these exceptions:
DodoPaymentsIOException: An I/O or network error.DodoPaymentsInvalidDataException: The SDK couldn’t interpret the response data, for example because a required property is missing.DodoPaymentsException: The base class of every SDK exception.
Pagination
List methods return one page of results. You can iterate over every item or move through the pages yourself.Auto-Pagination
Paginate returns an IAsyncEnumerable that fetches the next page when it needs it:
Manual Pagination
To work with one page at a time, readItems, then call HasNext() and Next():
PaymentListParams from the DodoPayments.Client.Models.Payments namespace, for example client.Payments.List(new PaymentListParams { PageSize = 50 }).
ASP.NET Core Integration
Register one client as a singleton in the dependency injection container, and read the API key from configuration:Program.cs
appsettings.json:
appsettings.json
Resources
NuGet Package
Package versions and install commands.
GitHub Repository
Source code, releases, and examples.
API Reference
Every endpoint, parameter, and response.
Discord Community
Ask questions and talk with other developers.
Support
For help with the C# SDK:- Discord: Join the community server for real-time help.
- Email: Contact support@dodopayments.com.
- GitHub: Open an issue on the repository.