Extending your integration with Cakemail Next-gen API Webhooks
This guide will show you how to create and manage HTTP callbacks on events occurring in Cakemail Next-gen API. These callbacks are sent with a payload processed by web service endpoints created and controlled by you, which extend your application and integration with Cakemail.
Even though it is optional, it is recommended to validate the origin of callbacks. Webhooks sent by Cakemail are verified by computing a digital signature. Each webhook request includes an x-signature header generated using a secret and the data sent in the request. To verify that the request comes from your configured webhook, calculate the HMAC digest according to the following algorithm and compare it to the value in the x-signature header. If they match, you can be sure that the webhook was sent from your configured webhook in your Cakemail account.
app.use( express.json({ // Store the rawBody buffer on the request verify: (req: any, res, buf) => { req.rawBody = buf; }, }));app.post("/webhook", async (req, res) => { //Extract x-signature Header from the request const hmacHeader = req.get("x-signature"); //Create a hash based on the parsed body const hash = crypto .createHmac("sha256", secret) .update(req.rawBody) .digest("base64"); // Compare the created hash with the value of the x-signature header if (hash === hmacHeader) { console.log("Webhook is originating from Cakemail"); res.sendStatus(200); } else { console.log("Signature is invalid, rejected"); res.sendStatus(403); }});