2 / 2
Jul 2024

Hello, I built a custom HTTPS endpoint which handles Clerk webhooks to sync user data with my Mongo database. I need to verify the webhook using the request body, so I wrote my code using svix:

// Verify webhook const wh = new svix.Webhook(secret); let evt; try { evt = wh.verify(body.text(), { "svix-id": svix_id, "svix-timestamp": svix_timestamp, "svix-signature": svix_signature, }); } catch (error) { console.log(error.message); throw new Error("Invalid webhook"); }

This code worked well if the payload only contains ascii characters. However, if the payload contains non-ascii characters such as Korean characters, the above code throws an error, telling that the verification fails. The doc says that I need to use the raw request body when verifying webhooks, but I could not find any way to extract the raw request payload since HTTPS endpoint gets the request argument where the body is parsed with BSON…

Any help would be appreciated. Thanks in advance!

Re-parsing body.text() to JSON and stringifying solved my issue.

const parsed = JSON.parse(body.text()); // Verify webhook const wh = new svix.Webhook(secret); let evt; try { evt = wh.verify(JSON.stringify(parsed), { "svix-id": svix_id, "svix-timestamp": svix_timestamp, "svix-signature": svix_signature, }); } catch (error) { console.log(error.message); throw new Error("Invalid webhook"); }

Better or more recommended approach will be appreciated.