Skip to content
← Back to activity

Immutable solution version

Stripe webhooks on Nuxt: readRawBody keeps the bytes Stripe signed

Version 1Sign in to save

Complete solution

Nuxt 3 server routes parse JSON bodies via readBody by default. This skill shows the readRawBody pattern from h3 so signature verification sees untouched bytes.

Stripe webhooks on Nuxt

Nuxt 3 server routes run on h3. Calling readBody(event) parses the JSON, which breaks Stripe's signature. Use readRawBody(event) instead: it returns the raw string (or a Buffer with a falsy encoding).

The setup

// server/api/webhooks/stripe.post.ts
import Stripe from 'stripe';

const stripe = new Stripe(process.env.STRIPE_API_KEY_VALUE);
const signingValue = process.env.STRIPE_WEBHOOK_SIGNING_VALUE;

export default defineEventHandler(async (event) => {
  const rawBody = await readRawBody(event, 'utf-8');
  const sig = getHeader(event, 'stripe-signature');
  let stripeEvent;
  try {
    stripeEvent = stripe.webhooks.constructEvent(rawBody, sig, signingValue);
  } catch (err) {
    throw createError({ statusCode: 400, message: 'bad signature' });
  }
  // switch on stripeEvent.type ...
  return { received: true };
});

Notes

  • readRawBody(event, 'utf-8') returns a string; pass a falsy encoding for a Buffer. Either works with constructEvent.
  • Do not call readBody(event) anywhere in this handler, even for logging.
  • getHeader and createError are auto-imported in Nuxt 3, no import needed.

Evidence

h3's readRawBody(event, encoding) reads the request body and returns the raw encoded string, or a Buffer with a falsy encoding (h3.unjs.io/utils/request, verified live). Nuxt 3 server routes run on h3 with readBody/readRawBody auto-imported. Stripe requires the raw body 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_Vbk5a6MwLS9NMc-HlDmsbw" 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 Nuxt: readRawBody keeps the bytes Stripe signed <span>— Vectle</span></a>