This page is the shortest path to calling a TOON-first API from a .NET application.
dotnet add package DevOp.Toon.Client
using DevOp.Toon.Client;
builder.Services.AddToonClient(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.Timeout = TimeSpan.FromSeconds(30);
});
AddToonClient(...) also registers IToonService automatically if it is not already present. HTTP response decompression (GZip, Deflate, Brotli) is enabled by default.
public sealed class ProductService
{
private readonly IToonClient client;
public ProductService(IToonClient client)
{
this.client = client;
}
public Task<List<Product>?> GetProductsAsync(CancellationToken cancellationToken)
=> client.GetAsync<List<Product>>("api/products", cancellationToken);
public Task<Product?> CreateProductAsync(CreateProductRequest request, CancellationToken cancellationToken)
=> client.PostAsync<CreateProductRequest, Product>("api/products", request, cancellationToken);
}
Use ResponseEncodeOverrides to send X-Toon-Option-* headers that instruct the server how to format its TOON response:
using DevOp.Toon.Core;
builder.Services.AddToonClient(options =>
{
options.BaseAddress = new Uri("https://api.example.com/");
options.ResponseEncodeOverrides = new ToonResponseEncodeOverrideOptions
{
ObjectArrayLayout = ToonObjectArrayLayout.Columnar,
KeyFolding = ToonKeyFolding.Off,
IgnoreNullOrEmpty = true
};
});
Only non-null properties send a header, so you can set just what you need.
All failure paths throw ToonClientException. It carries the HTTP status code, raw response body, and a Decode<T>() method for reading structured error bodies:
try
{
var product = await client.GetAsync<Product>("api/products/99", cancellationToken);
}
catch (ToonClientException ex)
{
Console.WriteLine($"Status: {ex.StatusCode}");
Console.WriteLine($"Type: {ex.ContentType}");
Console.WriteLine($"Raw: {ex.Content}");
// Decode a structured error body — works for both TOON and JSON responses
var problem = ex.Decode<ProblemDetails>();
}
ToonClientException is thrown on non-2xx responses, deserialization failures, and unsupported response content types.
IToonClient surface