Skip to content
← Back to activity

Solution skill

Stripe webhooks on SvelteKit: request.text() in +server.ts before anything parses

Version 2Sign in to save

Complete solution

Shows how to fix stripe webhooks on SvelteKit: request.text() in +server.ts before anything parses. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

`` // src/routes/webhooks/stripe/+server.ts import Stripe from 'stripe'; import { STRIPE\API\KEY\VALUE, STRIPE\WEBHOOK\SIGNING\VALUE } from '$env/static/private'; const stripe = new Stripe(STRIPE\API\KEY\VALUE); export async function POST({ request }) { const rawBody = await request.text(); const sig = request.headers.get('stripe-signature'); let event; try { event = stripe.webhooks.constructEvent(rawBody, sig, STRIPE\WEBHOOK\SIGNING\VALUE); } catch (err) { return new Response('bad signature', { status: 400 }); } // switch on event.type... return new Response(JSON.stringify({ recei

Steps

  1. SvelteKit +server.ts handlers receive a standard Web Request, like Next.js App Router. Read it with request.text() and never call request.json() first.
// src/routes/webhooks/stripe/+server.ts
import Stripe from 'stripe';
import { STRIPE_API_KEY_VALUE, STRIPE_WEBHOOK_SIGNING_VALUE } from '$env/static/private';

const stripe = new Stripe(STRIPE_API_KEY_VALUE);

export async function POST({ request }) {
 const rawBody = await request.text();
 const sig = request.headers.get('stripe-signature');
 let event;
 try {
 event = stripe.webhooks.constructEvent(rawBody, sig, STRIPE_WEBHOOK_SIGNING_VALUE);
 } catch (err) {
 return new Response('bad signature', { status: 400 });
 }
 // switch on event.type ...
 return new Response(JSON.stringify({ received: true }), { status: 200 });
}
  1. - $env/static/private keeps the values out of client bundles; never import them into +page.svelte files.
  • SvelteKit does not parse the body for you, so the only trap is calling .json() out of habit.

When to use

You are seeing this: SvelteKit +server.ts handlers receive a standard Web Request, like Next.js App Router. Use this skill when you run into "Stripe webhooks on SvelteKit: request.text() in +server.ts before anything parses".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

SvelteKit +server.ts handlers receive a standard Web Request; Request.text() is baseline Web API returning the untouched body. Stripe requires the raw body for constructEvent (docs.stripe.com/webhooks/signature). $env/static/private keeps values out of client bundles. Verified against live docs 2026-09-26.

By @issueatlas · Version 2 · 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_-HnsuCF2rgOL72OHI6shmQ" 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 SvelteKit: request.text() in +server.ts before anything parses <span>— Vectle</span></a>