Templates / APIs
Stripe Checkout endpoint for a frontend (Lovable, Bolt, v0)
A POST endpoint that creates a Stripe Checkout Session and returns its URL, so a frontend-only app can take payments without exposing the Stripe secret key.
Frontend builders can draw a pricing page but can’t keep a Stripe secret key. This endpoint creates the Checkout Session server-side; the frontend just redirects to the URL it returns.
Build it with Wisp
Type this into Wisp, or press Build this — it writes the code below, deploys it and gives you a URL:
A POST /checkout endpoint that takes { priceId, quantity }, only allows prices listed in ALLOWED_PRICES, creates a Stripe Checkout Session with STRIPE_SECRET_KEY and returns { url } to redirect to.
The code
POST /checkout · API
import { config } from 'wisp';
export async function handler(req) {
const { priceId, quantity = 1 } = req.body ?? {};
const allowed = config.require('ALLOWED_PRICES').split(',').map((p) => p.trim());
if (!allowed.includes(priceId)) return { status: 400, body: { error: 'Unknown price' } };
const qty = Math.min(Math.max(parseInt(quantity, 10) || 1, 1), 10);
const params = new URLSearchParams({
mode: config.get('CHECKOUT_MODE', 'payment'), // or "subscription"
'line_items[0][price]': priceId,
'line_items[0][quantity]': String(qty),
success_url: config.require('SUCCESS_URL') + '?session_id={CHECKOUT_SESSION_ID}',
cancel_url: config.require('CANCEL_URL'),
});
const res = await fetch('https://api.stripe.com/v1/checkout/sessions', {
method: 'POST',
headers: { authorization: `Bearer ${config.require('STRIPE_SECRET_KEY')}`, 'content-type': 'application/x-www-form-urlencoded' },
body: params,
});
const session = await res.json();
if (!res.ok) return { status: 502, body: { error: session.error?.message ?? 'Stripe error' } };
return { body: { url: session.url } };
}
Set it up
Add these in the project’s Variables & secrets (secrets are encrypted and never shown in logs):
STRIPE_SECRET_KEY— Stripe Dashboard → Developers → API keys (use a restricted key with Checkout Sessions write).ALLOWED_PRICES— A variable: the price ids your frontend may sell, comma-separated (price_…).SUCCESS_URL / CANCEL_URL— Variables: pages on your site to return to.
Try it
From the frontend: const { url } = await (await fetch("https://<your-project-url>/checkout", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ priceId: "price_123" }) })).json(); location.href = url; Use test-mode keys and card 4242 4242 4242 4242.
How it works
- The price list is enforced server-side, so nobody can POST a cheaper price id.
- Stripe’s API takes form-encoded bodies;
URLSearchParamsbuilds them, including nestedline_items[0][price]keys. - Pair it with the Stripe webhook template to fulfil orders when payment completes.
Variations
- Pass
customer_emailwhen the user is signed in. - Switch
CHECKOUT_MODEtosubscriptionfor recurring prices. - Add
allow_promotion_codes=truefor coupons.
Questions
Should fulfilment happen on the success page? No — people close tabs. Fulfil from the checkout.session.completed webhook, which Stripe retries until it succeeds.
Build it with Wisp — free trial