Skip to content

Billing (Stripe)

@delightstack/stripe is Stripe billing for SvelteKit apps on Cloudflare Workers — define your plans and meters in code, sync them to Stripe, handle webhooks, and gate features by subscription.

  • Config-driven plans & meters — declare them once with defineBillingConfig; sync to Stripe.
  • Subscriptions — checkout, subscription state, and a webhook-driven sync into your database.
  • Metered usage — report usage events for usage-based pricing.
  • Webhooks — verified webhook handling with handleWebhook.
  • Subscription guards — gate SvelteKit routes by plan/entitlement.
Terminal window
pnpm add @delightstack/stripe stripe
ImportUse
@delightstack/stripe/serverdefineBillingConfig, createBillingHandle, syncSubscription, syncAll, reportMeterEvent, handleWebhook, getStripe
@delightstack/stripe/sveltekitcreateBillingGuards
@delightstack/stripe/clientreactive Svelte 5 billing client
import { defineBillingConfig } from '@delightstack/stripe/server';
export const billing = defineBillingConfig({
secret_key: env.STRIPE_SECRET_KEY,
plans: [
{ id: 'pro', name: 'Pro', price: 2000, interval: 'month', entitlements: ['premium'] },
],
meters: [
{ id: 'ai-tokens', name: 'AI tokens', event_name: 'ai_tokens' },
],
});
hooks.server.ts
import { createBillingHandle } from '@delightstack/stripe/server';
export const billingHandle = createBillingHandle({
config: billing,
getDb: (event) => event.platform.env.DB,
});

syncAll() pushes your plans/meters to Stripe; handleWebhook() verifies incoming events and syncSubscription() reconciles them into your database.

+page.server.ts
import { createBillingGuards } from '@delightstack/stripe/sveltekit';
const { requirePlan } = createBillingGuards(billing);
export const load = (event) => {
requirePlan(event, 'pro'); // redirect / 402 if not subscribed
};
import { reportMeterEvent } from '@delightstack/stripe/server';
await reportMeterEvent(billing, { meter: 'ai-tokens', value: 1200, customer });

A subscription sync writes the org’s plan integer through the auth RPC. The package owns only the bits of entitlements that some plan in plans grants, at their index in entitlements. It reads the org (getOrg), sets or clears those bits, and leaves every other bit alone, so flags your app keeps in plan survive every webhook. Nothing is written when the value would not change. Pass an auth RPC that has getOrg (the @delightstack/auth server does); without it the sync replaces the whole integer.

The hook runs after every re-sync of a customer: the customer.subscription.* and subscription-mode checkout.session.completed webhooks, and the POST /subscription, DELETE /subscription and POST /sync routes. It describes the most relevant subscription (active, then trialing, past_due, canceled). A customer whose only subscription is incomplete, unpaid or paused is reported with that status, plan_id: null and no entitlements. A customer with no subscription at all is not reported. Webhook calls carry event_id; key any non-idempotent side effect by it.

export const billing = defineBillingConfig({
secret_key: env.STRIPE_SECRET_KEY,
publishable_key: env.STRIPE_PUBLISHABLE_KEY,
stripe_api_url: dev ? env.STRIPE_API_URL : undefined, // e.g. 'http://localhost:12111'
});

stripe_api_url points every Stripe API call at another server. Its protocol, host and port go to the Stripe SDK; the path is ignored. The client always uses the SDK’s fetch HTTP client, so it works on Cloudflare Workers.