How do i access the rawbody

Hi Directus community,

I’m trying to implement a Stripe webhook endpoint inside Directus, and I need access to the raw request body so I can verify the webhook signature with stripe.webhooks.constructEvent().

I tried adding this hook middleware:

import { defineHook } from "@directus/extensions-sdk";
import express from "express";

export default defineHook(({ init }) => {
  init("middlewares.before", async ({ app }) => {
    app.use(
      express.json({
        verify: (req, res, buf) => {
          if (req.originalUrl.startsWith("/stripe-response")) {
            req.rawBody = buf.toString("utf8");
          }
        },
      })
    );
  });
});

After implementing the above hook i get the following error on all extension endpoint

I’d add that /stripe-response prefix in the app.use call, so your middleware only fires on that route. That’ll prevent your middleware from affecting other routes :slightly_smiling_face:

// [...]

app.use("/stripe-response", express.json(/* ... */));

// [...]