Skip to main content

An SDK Hooks Seam in C# v4 SDKs (Beta)

· 2 min read

C# v4 SDKs now expose hooks you can register to observe every request the client sends and every response it receives, with the context the SDK has about the call.

Details​

The SDK now ships an SdkHook type under Core.Hooks. Register hooks client-wide through the Hooks client option, or for one call through RequestOptions.Hooks. A hook runs inside the retry pipeline, once per attempt, after authentication is applied, so it sees exactly what goes out and every raw response, including the status and headers a successful call does not return. Each hook receives a HookContext with the call's method, URI and request options.

Usage Example​

Register a hook that records the rate-limit budget from every response:

var options = new PaymentsClientOptions
{
Hooks =
[
SdkHook.OnResponse((response, context) =>
{
if (response.Headers.TryGetValues("X-RateLimit-Remaining", out var remaining))
Console.WriteLine($"{context.Method} {context.Uri}: {remaining.First()} calls left");
}),
],
};

A hook observes the call. An exception thrown from a hook fails the call and is not retried.

This feature is available in the C# v4 SDK (beta). Generate it from the APIMatic CLI:

apimatic sdk generate --language=csharp --codegen-version=v4 --stability=beta

For the full set of flags and options, see the APIMatic CLI documentation.