Zum Hauptinhalt springen

Documentation Index

Fetch the complete documentation index at: https://docs.dodopayments.com/llms.txt

Use this file to discover all available pages before exploring further.

The C# SDK provides convenient access to the Dodo Payments REST API from applications written in C#. It features an async Task-based API with strong typing, automatic retries, and comprehensive error handling.

Installation

Install the package from NuGet:
dotnet add package DodoPayments.Client
The SDK requires .NET 8.0 or later. It works with ASP.NET Core, Console applications, and other .NET project types.

Quick Start

Initialize the client and create a checkout session:
using System;
using DodoPayments.Client;
using DodoPayments.Client.Models.CheckoutSessions;

// Configured using the DODO_PAYMENTS_API_KEY and DODO_PAYMENTS_BASE_URL environment variables
DodoPaymentsClient client = new();

CheckoutSessionCreateParams parameters = new()
{
    ProductCart =
    [
        new()
        {
            ProductID = "product_id",
            Quantity = 1,
        },
    ],
};

var checkoutSessionResponse = await client.CheckoutSessions.Create(parameters);

Console.WriteLine(checkoutSessionResponse.SessionId);
Always store your API keys securely using environment variables, user secrets, or Azure Key Vault. Never hardcode them in your source code or commit them to version control.

Core Features

Async/Await

Full async Task-based API for non-blocking operations

Strong Typing

Comprehensive type safety with nullable reference types

Smart Retries

Automatic retries with exponential backoff for transient errors

Error Handling

Built-in exception hierarchy for precise error management

Configuration

Environment Variables

.env
DODO_PAYMENTS_API_KEY=your_api_key_here
// Automatically reads from environment variables
DodoPaymentsClient client = new();
PropertyEnvironment variableRequiredDefault value
BearerTokenDODO_PAYMENTS_API_KEYtrue-
WebhookKeyDODO_PAYMENTS_WEBHOOK_KEYfalse-
BaseUrlDODO_PAYMENTS_BASE_URLtrue"https://live.dodopayments.com"

Manual Configuration

DodoPaymentsClient client = new() { BearerToken = "My Bearer Token" };

Environments

Switch between live and test mode:
using DodoPayments.Client.Core;

DodoPaymentsClient client = new() { BaseUrl = EnvironmentUrl.TestMode };

Retries

The SDK automatically retries 2 times by default with exponential backoff. It retries on connection errors and status codes 408, 409, 429, and 5xx.
// Custom retry count
DodoPaymentsClient client = new() { MaxRetries = 3 };

Timeouts

Requests time out after 1 minute by default.
DodoPaymentsClient client = new() { Timeout = TimeSpan.FromSeconds(30) };

Per-Request Overrides

Temporarily modify configuration for a single request using WithOptions:
var response = await client
    .WithOptions(options => options with
    {
        Timeout = TimeSpan.FromSeconds(10),
        MaxRetries = 5,
    })
    .CheckoutSessions.Create(parameters);

Common Operations

Create a Checkout Session

var parameters = new CheckoutSessionCreateParams
{
    ProductCart =
    [
        new()
        {
            ProductID = "prod_123",
            Quantity = 1
        }
    ],
    ReturnUrl = "https://yourdomain.com/return"
};

var session = await client.CheckoutSessions.Create(parameters);
Console.WriteLine($"Checkout URL: {session.Url}");

Manage Customers

// Create a customer
var customer = await client.Customers.Create(new CustomerCreateParams
{
    Email = "customer@example.com",
    Name = "John Doe"
});

// Retrieve customer
var retrieved = await client.Customers.Retrieve("cus_123");
Console.WriteLine($"Customer: {retrieved.Name} ({retrieved.Email})");

Handle Subscriptions

using DodoPayments.Client.Models.Payments;
using DodoPayments.Client.Models.Subscriptions;

// Create a subscription
var subscription = await client.Subscriptions.Create(new SubscriptionCreateParams
{
    Billing = new BillingAddress
    {
        Country = "US",
        City = "San Francisco",
        State = "CA",
        Street = "1 Market St",
        Zipcode = "94105",
    },
    Customer = new AttachExistingCustomer { CustomerID = "cus_123" },
    ProductID = "pdt_456",
    Quantity = 1,
});

// Charge an on-demand subscription
// ProductPrice is in the lowest currency denomination (e.g., 2500 = $25.00 USD)
var charge = await client.Subscriptions.Charge(
    subscription.SubscriptionId,
    new SubscriptionChargeParams { ProductPrice = 2500 }
);
Billing erfordert mindestens den zwei Buchstaben ISO Country Code. Verwenden Sie AttachExistingCustomer, um einen bestehenden Kunden zu verknüpfen, oder NewCustomer, um einen neuen zu erstellen. ProductPrice ist in der kleinsten Währungseinheit.

Fehlerbehandlung

Das SDK wirft spezifische Ausnahmen basierend auf dem HTTP-Statuscode. Alle 4xx-Fehler erben von DodoPayments4xxException.
StatusAusnahme
400DodoPaymentsBadRequestException
401DodoPaymentsUnauthorizedException
403DodoPaymentsForbiddenException
404DodoPaymentsNotFoundException
422DodoPaymentsUnprocessableEntityException
429DodoPaymentsRateLimitException
5xxDodoPayments5xxException
andereDodoPaymentsUnexpectedStatusCodeException
Andere Ausnahmearten:
  • DodoPaymentsIOException: I/O-Netzwerkfehler
  • DodoPaymentsInvalidDataException: Fehler bei der Interpretation der analysierten Daten
  • DodoPaymentsException: Basisklasse für alle Ausnahmen

Paginierung

Automatische Paginierung

Durchlaufen Sie alle Ergebnisse über alle Seiten mit der Methode Paginate, die eine IAsyncEnumerable zurückgibt:
var page = await client.Payments.List(parameters);
await foreach (var item in page.Paginate())
{
    Console.WriteLine(item);
}

Manuelle Paginierung

var page = await client.Payments.List();
while (true)
{
    foreach (var item in page.Items)
    {
        Console.WriteLine(item);
    }
    if (!page.HasNext())
    {
        break;
    }
    page = await page.Next();
}

ASP.NET Core-Integration

Registrieren Sie den Client in Ihrem DI-Container:
Program.cs
using DodoPayments.Client;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<DodoPaymentsClient>(sp =>
{
    var configuration = sp.GetRequiredService<IConfiguration>();
    return new DodoPaymentsClient
    {
        BearerToken = configuration["DodoPayments:ApiKey"]
    };
});

var app = builder.Build();
app.Run();
appsettings.json
{
  "DodoPayments": {
    "ApiKey": "your_api_key_here"
  }
}
Für die Entwicklung verwenden Sie Benutzergeheimnisse anstelle von Schlüsseln in appsettings.json:
dotnet user-secrets init
dotnet user-secrets set "DodoPayments:ApiKey" "your_api_key_here"

Ressourcen

NuGet Package

Sehen Sie sich das Paket in der NuGet Gallery an

GitHub Repository

Sehen Sie sich den Quellcode an und tragen Sie bei

API Reference

Komplette API-Dokumentation

Discord Community

Hilfe erhalten und Kontakt mit Entwicklern aufnehmen

Support

Brauchen Sie Hilfe mit dem C# SDK?
Last modified on May 14, 2026