The BasisTheory C# library provides convenient access to the BasisTheory APIs from C#.
- Documentation
- Installation
- Reference
- Usage
- Environments
- Exception Handling
- Pagination
- Advanced
- Contributing
- Requirements
API reference documentation is available here.
dotnet add package BasisTheoryA full reference for this library is available here.
Instantiate and use the client with the following:
var client = new global::BasisTheory.Client.BasisTheory("API_KEY", "CORRELATION_ID");
await client.Tenants.Self.GetAsync();This SDK allows you to configure different environments for API requests.
using BasisTheory.Client;
var client = new BasisTheory(new ClientOptions
{
BaseUrl = BasisTheoryEnvironment.Default
});When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.
using BasisTheory.Client;
try {
var response = await client.Tenants.Self.GetAsync(...);
} catch (BasisTheoryApiException e) {
System.Console.WriteLine(e.Body);
System.Console.WriteLine(e.StatusCode);
}List endpoints are paginated. The SDK provides an async enumerable so that you can simply loop over the items:
using global::BasisTheory.Client;
var client = new global::BasisTheory.Client.BasisTheory("API_KEY", "CORRELATION_ID");
var items = await client.Applications.ListAsync(
new ApplicationsListRequest
{
Id = new List<string>() { "id" },
Type = new List<string>() { "type" },
Page = 1,
Start = "start",
Size = 1,
}
);
await foreach (var item in items)
{
// do something with item
}The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
Which status codes are retried depends on the retryStatusCodes generator configuration:
legacy (current default): retries on
recommended: retries on
- 408 (Timeout)
- 429 (Too Many Requests)
- 502 (Bad Gateway)
- 503 (Service Unavailable)
- 504 (Gateway Timeout)
Use the MaxRetries request option to configure this behavior.
var response = await client.Tenants.Self.GetAsync(
...,
new RequestOptions {
MaxRetries: 0 // Override MaxRetries at the request level
}
);The SDK defaults to a 30 second timeout. Use the Timeout option to configure this behavior.
var response = await client.Tenants.Self.GetAsync(
...,
new RequestOptions {
Timeout: TimeSpan.FromSeconds(3) // Override timeout to 3s
}
);Access raw HTTP response data (status code, headers, URL) alongside parsed response data using the .WithRawResponse() method.
using BasisTheory.Client;
// Access raw response data (status code, headers, etc.) alongside the parsed response
var result = await client.Tenants.Self.GetAsync(...).WithRawResponse();
// Access the parsed data
var data = result.Data;
// Access raw response metadata
var statusCode = result.RawResponse.StatusCode;
var headers = result.RawResponse.Headers;
var url = result.RawResponse.Url;
// Access specific headers (case-insensitive)
if (headers.TryGetValue("X-Request-Id", out var requestId))
{
System.Console.WriteLine($"Request ID: {requestId}");
}
// For the default behavior, simply await without .WithRawResponse()
var data = await client.Tenants.Self.GetAsync(...);If you would like to send additional headers as part of the request, use the AdditionalHeaders request option.
var response = await client.Tenants.Self.GetAsync(
...,
new RequestOptions {
AdditionalHeaders = new Dictionary<string, string?>
{
{ "X-Custom-Header", "custom-value" }
}
}
);If you would like to send additional query parameters as part of the request, use the AdditionalQueryParameters request option.
var response = await client.Tenants.Self.GetAsync(
...,
new RequestOptions {
AdditionalQueryParameters = new Dictionary<string, string>
{
{ "custom_param", "custom-value" }
}
}
);While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
This SDK requires: