From 31c916de7ea307d1774428c9d40d2944af362393 Mon Sep 17 00:00:00 2001 From: kleemeo Date: Tue, 27 Jun 2023 12:28:17 -0400 Subject: [PATCH] add nextjs app router example --- docs/receiving/verifying-payloads/how.mdx | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/receiving/verifying-payloads/how.mdx b/docs/receiving/verifying-payloads/how.mdx index 44d613b..9d7d98e 100644 --- a/docs/receiving/verifying-payloads/how.mdx +++ b/docs/receiving/verifying-payloads/how.mdx @@ -467,6 +467,42 @@ export default async function handler(req, res) { } ``` +### Node.js (Next.js 13 App Router) + +```js +import { Webhook } from "svix"; + +const webhookSecret: string = process.env.WEBHOOK_SECRET || "your-secret"; + +export async function POST(req: Request) { + const svix_id = req.headers.get("svix-id") ?? ""; + const svix_timestamp = req.headers.get("svix-timestamp") ?? ""; + const svix_signature = req.headers.get("svix-signature") ?? ""; + + const body = await req.text(); + + const sivx = new Webhook(webhookSecret); + + let msg; + + try { + msg = sivx.verify(body, { + "svix-id": svix_id, + "svix-timestamp": svix_timestamp, + "svix-signature": svix_signature, + }); + } catch (err) { + return new Response("Bad Request", { status: 400 }); + } + + console.log(msg); + + // Rest + + return new Response("OK", { status: 200 }); +} +``` + ### Node.js (Netlify Functions) ```js