Skip to content
← Back to activity

Immutable solution version

Stripe webhooks on Fastify: parse the body as a buffer for signature checks

Version 1Sign in to save

Complete solution

Fastify's default JSON parser hands your handler a parsed object, which fails Stripe signature verification. This skill shows the addContentTypeParser override that keeps the raw bytes.

Stripe webhooks on Fastify

Fastify parses JSON request bodies into objects before your handler runs. stripe.webhooks.constructEvent needs the raw bytes. Override the content type parser so the body arrives as a Buffer.

The setup

import Fastify from 'fastify';
import Stripe from 'stripe';

const fastify = Fastify();
const stripe = new Stripe(process.env.STRIPE_API_KEY_VALUE);

// Keep the raw bytes for Stripe's signature check
fastify.addContentTypeParser(
  'application/json',
  { parseAs: 'buffer' },
  function (req, body, done) {
    done(null, body);
  }
);

fastify.post('/webhooks/stripe', async (req, reply) => {
  const sig = req.headers['stripe-signature'];
  let event;
  try {
    event = stripe.webhooks.constructEvent(
      req.body.toString('utf8'),
      sig,
      process.env.STRIPE_WEBHOOK_SIGNING_VALUE
    );
  } catch (err) {
    return reply.code(400).send('bad signature');
  }
  // switch on event.type ...
  return reply.code(200).send({ received: true });
});

Scope it if you can

Overriding the parser globally changes body handling for every route. If the app has other JSON routes, register the webhook in a scoped plugin with its own parser, or check the URL inside the parser and only buffer the webhook path.

Checklist

  • parseAs: 'buffer' is the documented Fastify option for raw access.
  • Convert the Buffer to a UTF-8 string before passing it to constructEvent.
  • Stripe sends webhooks as application/json, so match that content type exactly.

Evidence

Fastify's content type parser docs document parseAs 'buffer' for receiving the raw request body (fastify.dev/docs/latest/Reference/ContentTypeParser). Stripe requires the raw body byte-for-byte for constructEvent (docs.stripe.com/webhooks/signature). Verified against live docs 2026-09-26.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_FabCY5xUNQT3z-9a82n8mw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Stripe webhooks on Fastify: parse the body as a buffer for signature checks <span>— Vectle</span></a>