wasAsked.

What are Webhooks?

Infinite Computer Solutions · Senior Technical Lead · technical

Published

Year not specified · System Design

Community answers

Posted by wasAsked editorial collection

Webhooks are user-defined HTTP callbacks: when an event happens in one system, it sends an HTTP POST request to a URL registered by another system. Instead of the consumer repeatedly asking "has anything changed?" (polling), the provider pushes a notification when something happens — often described as a "reverse API". Examples - A payment gateway (Razorpay, Stripe) notifies your server when a payment succeeds or fails. - GitHub calls your CI system when code is pushed. - A courier service notifies your app of shipment status changes. - Twilio sends incoming SMS details to your endpoint. How it works 1. The consumer registers a webhook URL (https://api.example.com/webhooks/payments) and chooses events, either in the provider's dashboard or through an API. 2. When the event occurs, the provider sends a POST request with a JSON payload describing the event (event type, ID, timestamp, data). 3. The consumer validates the request, processes it and returns a 2xx status quickly. 4. If the consumer fails or times out, the provider usually retries with backoff. Receiving webhooks securely and reliably (ASP.NET Core example) [HttpPost("webhooks/payments")] public async Task<IActionResult> Payments(CancellationToken ct) { using var reader = new StreamReader(Request.Body); var body = await reader.ReadToEndAsync(ct); if (!_verifier.IsValidSignature(body, Request.Headers["X-Signature"])) // HMAC with shared secret return Unauthorized(); var evt = JsonSerializer.Deserialize<PaymentEvent>(body)!; if (await _inbox.AlreadyProcessedAsync(evt.Id, ct)) return Ok(); // idempotency await _queue.EnqueueAsync(evt, ct); // process asynchronously return Ok(); } Best practices - Verify authenticity: HMAC signatures over the raw body with a shared secret, timestamps to prevent replay attacks, HTTPS only, and optionally IP allow-lists. - Respond fast: acknowledge within the provider's timeout and do heavy work in background processing. - Idempotency: providers may deliver the same event more than once; deduplicate by event ID. - Ordering: events may arrive out of order; use timestamps or versions, or fetch the latest state from the provider's API. - Reconciliation: periodically poll the provider's API to catch missed events. - Logging and monitoring of failures. Providing webhooks to your customers - Let them register endpoints and select events; sign every payload; retry with exponential backoff; offer a delivery log and manual replay; and version payloads. Webhooks vs polling vs WebSockets - Polling wastes requests and adds delay; webhooks deliver near real time with less load. - WebSockets keep a persistent connection for real-time two-way communication with clients; webhooks are server-to-server and request-based.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics