Stripe.net Documentation

repository·master·Indexed 23 days ago

https://github.com/stripe/stripe-dotnet

The official Stripe client library for .NET, providing a strongly-typed interface to interact with the Stripe API. It supports .NET Standard 2.0+, .NET Core 5+, and .NET Framework 4.6.2+. The library features the StripeClient for per-client configuration, automatic retries, and support for manual or automatic pagination. It allows for custom HttpClient implementations, raw requests via RawRequestAsync, and the use of undocumented parameters and properties.

Tokens
2.2K
Snippets
13
Records
17
Agent score
31%

What's inside Stripe.net

  1. How StripeClient and Global Configuration work

    master

    Starting with version 46, the recommended way to interact with the Stripe API is using the StripeClient class. This approach allows for better IDE discoverability and enables per-client configuration (such as different API keys or retry settings) without affecting a global state.

    StripeClient Pattern (Recommended): Instantiate a StripeClient with your secret key. All services are accessible via the .V1 property.

    Global Configuration Pattern (Legacy): Sets a global StripeConfiguration.ApiKey. While still supported, it is less flexible for applications requiring multiple configurations.

    // StripeClient pattern (Recommended)
    var client = new StripeClient("sk_test_...");
    Customer customer = client.V1.Customers.Get("cus_1234");
    
    // Global Configuration pattern (Legacy)
    StripeConfiguration.ApiKey = "sk_test_...";
    var service = new CustomerService();
    Customer customer = service.Get("cus_1234");
  2. List resources with manual or automatic pagination

    master

    The List method returns a single page of results. To iterate through all results manually, you must use the StartingAfter parameter with the ID of the last item in the current page.

    For a simpler experience, use ListAutoPaging(), which automatically handles page iteration for you.

  3. Use undocumented parameters and properties

    master

    If you need to use beta features or undocumented fields that are not yet part of the typed library:

    Parameters: Use AddExtraParam(key, value) on your options object before calling a service method.

    Properties: Access the RawJObject property on the returned resource to retrieve values from the raw JSON response.

    // Undocumented Parameters
    var options = new CustomerCreateOptions
    {
        Email = "jenny.rosen@example.com"
    };
    options.AddExtraParam("secret_feature_enabled", "true");
    
    var client = new StripeClient("sk_test_...");
    var customer = client.V1.Customers.Create(options);
    
    // Undocumented Properties
    customer.RawJObject["secret_feature_enabled"];
  4. Use Public and Private Preview SDKs

    master

    Stripe provides preview versions of the SDK for testing new features:

    Public Preview: Install versions with the -beta.X suffix (e.g., 45.1.0-beta.2). Some features may require setting a beta version header using StripeConfiguration.AddBetaVersion("feature_name", "version").

    Private Preview: Install versions with the -alpha.X suffix. Access to these may require separate approval.

    Note: It is recommended to pin your version in the project file because preview versions may contain breaking changes without a major version bump.

  5. Install Stripe.net

    master

    You can install the Stripe.net library using several methods depending on your development environment:

    Using .NET CLI:

    dotnet add package Stripe.net

    Using NuGet CLI:

    nuget install Stripe.net

    Using Package Manager Console:

    Install-Package Stripe.net

    Using Visual Studio:

    1. Open the Solution Explorer.
    2. Right-click on your project and select Manage NuGet Packages...
    3. Go to the Browse tab and search for "Stripe.net".
    4. Select the package, choose your version, and click Install.
    dotnet add package Stripe.net
  6. Use custom OpenAPI specifications and fixtures for testing

    master

    To use custom OpenAPI specifications and fixtures files during testing, place them in the src/StripeTests/openapi/ directory.

    Requirements:

    • Files must be in JSON format.
    • The OpenAPI specification must be named spec3.json.
    • The fixtures file must be named fixtures3.json.

    When these files are detected, the test suite automatically starts a stripe-mock process on a random available port. For this to function, stripe-mock must be available in your environment's PATH.

  7. Configure automatic retries

    master

    The library automatically retries requests on intermittent failures (connection errors, timeouts, or 409 Conflict). By default, it performs up to two retries. You can adjust this using StripeConfiguration.MaxNetworkRetries.

    StripeConfiguration.MaxNetworkRetries = 0; // Zero retries
  8. Configure a custom HttpClient

    master

    You can provide your own HttpClient implementation to the library (e.g., for using a proxy or custom message handlers) by setting StripeConfiguration.StripeClient.

    StripeConfiguration.StripeClient = new StripeClient(
        apiKey,
        httpClient: new SystemNetHttpClient(httpClient));
  9. Delete a resource

    master

    Use the Delete method on a service class to remove a resource. This typically requires the resource ID and an options object.

    var client = new StripeClient("sk_test_...");
    Customer customer = client.V1.Customers.Delete("cus_123", options);
  10. Identify your plugin with AppInfo

    master

    If you are developing a plugin that uses Stripe.net, you can identify your application to Stripe by setting StripeConfiguration.AppInfo. This helps Stripe understand how their API is being used.

    StripeConfiguration.AppInfo = new AppInfo
    {
        Name = "MyAwesomePlugin",
        Url = "https://myawesomeplugin.info",
        Version = "1.2.34",
    };