reliability
Verify the Signature Before You Parse the Body
A webhook endpoint is a public URL that performs privileged work. The order of operations in the handler is what decides whether that is safe.
A webhook endpoint is an odd thing when you look at it plainly. It is a URL on the public internet that anyone can post to, and its job is to take the contents of that post and act on it: mark an invoice paid, grant access, start a workflow, send a message.
The only thing standing between that and a stranger granting themselves access is the signature check. Which makes the order of operations inside the handler more important than almost anything else in the file.
The order
The rule is one line: verify the signature over the raw body, before parsing anything, and refuse outright if a secret is not configured.
Each clause in that sentence is there because the alternative has shipped somewhere.
Over the raw body. A signature covers exact bytes. Parse the JSON first and sign the re-serialised version and you are signing a different string: key order may differ, whitespace is gone, numbers may have been normalised. The verification will fail for legitimate requests and you will be tempted to make it lenient, which is how a signature check becomes decorative.
Before parsing. Parsing attacker-controlled input is itself an operation with a surface. More practically, a handler that parses first tends to start reading fields first, and then logging them, and then doing one small harmless thing with them before the check. The check is the gate. Nothing goes through it early.
Refuse if the secret is missing. The dangerous version is the one that treats an unset secret as "verification not configured, continue". That is a system which is secure in production and wide open in every environment where somebody forgot a variable, which includes the production deployment on the day somebody rotates it incorrectly. Missing secret is a 503 and a log line, not a shrug.
What the signature is
Almost universally it is an HMAC, defined in RFC 2104: a hash of the message combined with a shared secret in a way that resists the length-extension attacks that a naive hash-the-secret-and-the-message construction allows.
Two implementation details carry real weight.
Compare in constant time. A normal string comparison returns as soon as it finds a differing byte, so the time it takes leaks how many leading bytes were correct. That is enough to recover a signature given enough attempts. Every platform has a constant-time comparison function; use it rather than the equality operator.
Include a timestamp in the signed payload, and check it. Without one, a valid request captured once is valid forever, and a replayed webhook is indistinguishable from a real one. With one, you reject anything outside a window of a few minutes. The window should be short enough to bound the risk and long enough to tolerate ordinary clock skew between two servers.
A replay window and an idempotency ledger are not the same defence and you want both. The window stops an old message being re-sent. The ledger stops a current message being processed twice.
Respond fast, work later
The other half of a good handler is about time rather than trust.
Senders expect a quick response and typically treat a slow one as a failure, then retry. If your handler verifies the signature, then does thirty seconds of work, then returns 200, you will get retries for work that is already in progress, and you will get them during exactly the periods when the work is slowest.
The shape that avoids this: verify, record that the event was received, return 200, and do the work afterwards. RFC 9110 has a status for precisely this situation, 202 Accepted, meaning the request was accepted for processing which has not completed. Many senders treat anything in the 2xx range as success, so either works; what matters is that the acknowledgement is not waiting on the work.
What to return when something is wrong
The status code is part of the contract and it changes the sender's behaviour.
A bad signature is 401 and the sender should not retry: retrying will not fix it, and the request may not be from who it claims.
A malformed body that passes the signature check is 400. The sender's payload is wrong and a retry sends the same wrong payload.
Your own failure, a database that is down or a dependency that timed out, is 500 or 503, and the sender should retry. This is the one that is most often misreported. A handler that catches its own exceptions and returns 200 to keep the dashboard green has told the sender the event was handled. It will never be sent again, and the work never happened.
That last one is worth stating plainly because it is the most expensive mistake available here: a 200 you did not earn permanently discards the event. Returning an honest 500 costs you a red line on a chart. Returning a dishonest 200 costs you the data.
The five-minute audit
Open the webhook handler you have. Read down from the top and answer four questions.
Is the first substantive thing a signature check over the raw body? Does it refuse when the secret is unset? Is there a timestamp window? Does an internal error return a 5xx rather than a swallowed 200?
Four yeses is a handler you can leave alone. Anything else is a short afternoon.