Stripe webhook - Possible to access raw body in custom extension endpoint?

Hi there!

Is there a way to access the raw body buffer data in a custom extension endpoint? In order to verify a request to a webhook at “my-domain”/stripe/webhook the stripe verification function stripe.webhooks.constructEvent() needs the raw body data alongside the stripe-signature header and the endpoint secret.

I managed to get access to the raw data by modifying the Directus source code: api > src > app.ts with my own middleware like this:

app.use((req, res, next) => { 
	if (req.originalUrl.startsWith('/stripe') && req.method === 'POST') { 
		(express.raw({ type: 'application/json' }) as RequestHandler)(req, res, (err: any) => { 
		if (err) return next(new InvalidPayloadError({ reason: err.message })); return next(); }); 
	} 
});

but it would be nice it it was possible to achieve the same thing withing the extension endpoint code.

Any help or tips would be highly appreciated. :slight_smile:

Hi Attaclar!

Thanks a lot for pointing me in the right direction. The webhook endpoint seems to do the exact thing I was trying to achieve. I´ll give it a try and see how it goes! :smiley:

Hi Magnus,

Welcome to the community!
I have seen people do this without modifying the source code.
One of the examples is: GitHub - nexthis/directus-stripe: Receive payments on your Directus commerce application using Stripe.
Maybe you can check that out and let me know if you have any questions