In EventsIQ , leads arrive by webhook. Meta, TikTok, Snapchat and Google each call a URL the moment someone fills in a lead form. Every lead is money, so the webhook handlers are some of the most careful code in the app.
These are Next.js App Router route handlers. Here is what I learned building them.
The basic shape
A webhook handler has four jobs, in this order:
Read the raw body.
Check it really came from the sender.
Reply quickly , with the status code the sender expects.
Do the slow work afterwards.
The interesting part is where "store the lead" goes, and that depends on the sender.
Read the raw body first
Signatures are computed over the exact bytes that were sent. If you parse the JSON first and turn it back into a string, the bytes can change (spacing, key order, number formats) and a valid signature stops matching.
So always start with request.text() :
export const dynamic = "force-dynamic" ;
export async function POST ( request : Request ) { const rawBody = await request .…