Menu

Webhooks in Next.js Route Handlers: Verify, Answer Fast, Work After
📰

Webhooks in Next.js Route Handlers: Verify, Answer Fast, Work After

DEV Community: javascript·Muhammad Hassaan Anjum·6 days ago
#UD53HtHp
#devtojavascript#devto#dev#lead#sender
Long article (1263 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.