Inbound Webhooks
Third-party services push state changes to BT via signed HTTP webhooks. The SDK provides three composable primitives:
IWebhookSignatureScheme— verify the third party signed the bodyIWebhookIdempotencyStore— drop duplicate retriesHttpRequest.ReadAndBufferBodyAsync()— capture raw bytes the way the third party sent them
Adapters compose these in a controller and dispatch the verified events into the SDK pipeline.
Anatomy of a webhook controller
[ApiController]
[Route("api/{adapter}/webhooks")]
public sealed class EnodeWebhookController(
IWebhookSignatureScheme signature,
IWebhookIdempotencyStore idempotency,
IPropertiesClient properties,
IDeviceRepository devices,
IOptions<EnodeOptions> options) : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Receive(CancellationToken ct)
{
// 1. Capture raw bytes — must be exactly what the third party signed.
var body = await Request.ReadAndBufferBodyAsync(ct: ct);
// 2. Verify the signature.
var secret = Encoding.UTF8.GetBytes(options.Value.WebhookSecret!);
if (!signature.Verify(body, Request.Headers, secret, out var failure))
return Unauthorized(new { error = failure });
// 3. Parse + idempotency check + dispatch.
var evt = JsonNode.Parse(body);
var eventId = evt?["id"]?.GetValue<string>();
if (eventId is not null
&& !await idempotency.TryReserveAsync(eventId, ct))
{
return Ok(); // already processed; respond success so the
// third party stops retrying
}
var externalId = evt?["data"]?["id"]?.GetValue<string>();
if (externalId is null) return Ok();
var device = await devices.FindByExternalIdAsync(externalId, ct);
if (device is null) return Ok(); // no BT side yet — skip
properties.Report(device.DeviceId, (JsonObject)evt!["data"]!);
return Ok();
}
}
Signature schemes
The default HmacSha256TimestampScheme covers Stripe-style headers:
X-Webhook-Signature: t=<unix>,v1=<hex-hmac>
where v1 is HMAC-SHA256(secret, "<t>." + body). Replay window is
configurable (default 5 minutes).
Vendors with custom shapes implement their own IWebhookSignatureScheme
and register it instead:
builder.Services.AddSingleton<IWebhookSignatureScheme, MyVendorScheme>();
The contract is intentionally narrow:
bool Verify(
ReadOnlySpan<byte> body,
IHeaderDictionary headers,
ReadOnlySpan<byte> secret,
out string failure);
Implementations MUST use timing-safe comparison
(CryptographicOperations.FixedTimeEquals).
Idempotency
InMemoryWebhookIdempotencyStore is registered by default — TTL
1 hour, capacity-capped. For multi-instance providers register a
Redis-backed implementation instead (one is not shipped in v1).
TryReserveAsync returns:
true— first sighting; caller MUST process the eventfalse— already seen; caller MUST treat as duplicate (return 200, do not re-process)
Per the third-party retry semantics: returning a 4xx/5xx tells them to retry, returning 200 to a duplicate event id means "yes I have this".
Body capture
HttpRequest.ReadAndBufferBodyAsync() enables ASP.NET request buffering,
reads the body to a byte array, and rewinds the body stream so subsequent
JSON parsing still works. Do not call Request.ReadFromJsonAsync()
before signature verification — that consumes the bytes the third party
signed.
Default cap is 1 MB; pass maxBytes: for larger payloads.
Receiving webhooks during local development
A development machine usually has no public address. A laptop sits behind NAT, and a Windows 365 Cloud PC is on a private network and reaches the internet through a proxy. A third party cannot open a connection to it.
To receive webhooks there, use an outbound tunnel: a client on your machine dials out to a tunnel service, and the service gives you a public URL that forwards requests back down that connection.
Tunnel tools
- Cloudflare Tunnel:
cloudflared tunnel --url http://localhost:5000prints a randomhttps://*.trycloudflare.comURL and needs no account. A named tunnel needs a domain on Cloudflare. The client connects out on port 7844, which corporate proxies often block. - ngrok:
ngrok http 5000. The client connects out on port 443. It needs an account; custom domains and TCP tunnels are paid. - Microsoft Dev Tunnels:
devtunnel host -p 5000 --allow-anonymous. It needs a Microsoft or Entra login. A tunnel is private unless you allow anonymous access, which a third-party sender needs. - Azure Relay (Hybrid Connections): an Azure resource your company owns, with its own keys and logging. More setup, but suitable beyond testing.
What to point the tunnel at
Point the tunnel at the provider, not at the whole tenant.
Provider running on your machine. Under btk3s provider dev <name> and F5 (see
Develop a provider), the provider runs on
the host. The scaffold sets no URL, so ASP.NET's default http://localhost:5000 applies. With
the controller above, give the third party:
https://<tunnel-host>/api/<adapter>/webhooks
Run the tunnel client where the provider runs. On Windows, that is inside WSL.
Provider deployed in the cluster. The scaffold chart has no ingress, only a ClusterIP Service on port 8080. Forward it to your machine and tunnel to the forwarded port:
kubectl -n btk3s port-forward svc/<provider> 8080:8080
cloudflared tunnel --url http://localhost:8080
Security
- A tunnel URL is public. Signature verification (
IWebhookSignatureScheme) is what keeps other senders out, so keep it on while developing. - Tunnel only the provider. Never tunnel the cluster's Traefik on port 443 as a whole.
- Use quick tunnels for testing only.
- On a company machine, check the company's policy first. A public tunnel may be against it even when the proxy lets it through.
See also
- Outbound Webhook Registration
- External Device Id — for the
FindByExternalIdAsynclookup