Skip to main content

Adding Webhook Support in C# v4 SDKs (Beta)

· 2 min read

C# v4 SDKs now understand the webhooks declared in your OpenAPI specification. The SDK generates a typed event parser and, when the specification describes a signing scheme, a signature verifier, so consumers can receive and verify events with the same type safety they get when calling your API.

Details​

Each webhook event's payload becomes a typed model. The parser reads the discriminator your specification names, from the request body or a header, and returns an event whose Type names the event and whose Payload exposes one TryGet accessor per event.

When the specification describes how events are signed, the SDK also generates a signature verifier configured through WebhookOptions: the shared Secret, a ReplayTolerance where the specification sets one, and a TimeProvider. Generated Add{Api}Webhooks and Add{Api}WebhookSignatureVerifier methods register the parser and the verifier with ASP.NET Core dependency injection.

Usage Example​

Register the parser and the verifier, then verify and parse each incoming request in your webhook endpoint:

builder.Services.AddPaymentsWebhooks();
builder.Services.AddPaymentsWebhookSignatureVerifier(options =>
options.Secret = builder.Configuration["Payments:WebhookSecret"]);

var app = builder.Build();

app.MapPost("/webhooks/payments", async (HttpRequest http, PaymentsWebhookParser parser,
PaymentsWebhookSignatureVerifier verifier, CancellationToken cancellationToken) =>
{
var webhookRequest = await WebhookRequest.FromStream(http.Body, http.Headers, cancellationToken);
if (!verifier.Verify(webhookRequest))
return Results.Unauthorized();

var evt = parser.Parse(webhookRequest);
if (evt.Type == PaymentsEventType.PaymentStatusUpdated &&
evt.Payload.TryGetPaymentStatusUpdatedEvent(out var updated))
{
Console.WriteLine($"Payment status changed: {updated.Status.Value}");
}

return Results.Ok();
});

Parse only deserializes the event, so always call Verify first when your API signs its webhooks.

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.