# Stripe webhooks on SvelteKit

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.

## The setup

```
// 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 });
}
```

## Notes

- `$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.