Skip to content

Advanced client usage

Olivier Bellone edited this page Jun 6, 2019 · 2 revisions

This page illustrates some advanced usages of StripeClient and SystemNetHttpClient.

Note: this documentation applies to Stripe.net v27.0.0 or more recent.

Using a proxy server

using System.Net;
using System.Net.Http;
using Stripe;

var handler = new HttpClientHandler
{
    Proxy = new WebProxy(proxyUrl),
    UseProxy = true,
};
var httpClient = new HttpClient(handler);

var stripeClient = new StripeClient(
    stripeApiKey,
    httpClient: new SystemNetHttpClient(httpClient)
);
StripeConfiguration.StripeClient = stripeClient;

Using a custom message handler

E.g. to use Xamarin's AndroidClientHandler:

using System.Net.Http;
using Stripe;

var handler = new Xamarin.Android.Net.AndroidClientHandler();
var httpClient = new HttpClient(handler);

var stripeClient = new StripeClient(
    stripeApiKey,
    httpClient: new SystemNetHttpClient(httpClient)
);
StripeConfiguration.StripeClient = stripeClient;

Using custom base URLs

This is useful to send API requests to a local server, e.g. stripe-mock:

using Stripe;

var stripeClient = new StripeClient(
    stripeApiKey,
    apiBase: "http://localhost:12111",
    filesBase: "http://localhost:12111",
);
StripeConfiguration.StripeClient = stripeClient;

Enabling automatic request retries

using Stripe;

var stripeClient = new StripeClient(
    stripeApiKey,
    httpClient: new SystemNetHttpClient(maxNetworkRetries: 2)
);
StripeConfiguration.StripeClient = stripeClient;