Stripe integration patterns - API keys, webhooks, checkout, subscriptions. Loads when working with payments.
Based on stripe/ai (MIT). Latest API version: 2026-01-28.
Priority order:
Enable dynamic payment methods in the Stripe Dashboard rather than hardcoding payment_method_types.
# .env.local (never commit)
STRIPE_SECRET_KEY=sk_test_...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
# .env.example (commit this)
STRIPE_SECRET_KEY=
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
STRIPE_WEBHOOK_SECRET=
Keep sk_ keys server-side only. Only pk_ keys may be exposed to the browser.
Always verify webhook signatures:
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const body = await req.text();
const signature = req.headers.get('stripe-signature')!;
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(
body,
signature,
process.env.STRIPE_WEBHOOK_SECRET!
);
} catch (err) {
return new Response('Webhook signature verification failed', { status: 400 });
}
switch (event.type) {
case 'checkout.session.completed':
// Fulfill the order
break;
case 'invoice.payment_succeeded':
// Update subscription status
break;
case 'customer.subscription.deleted':
// Handle cancellation
break;
}
return new Response('OK', { status: 200 });
}
customer.subscription.created, updated, deletedinvoice.payment_failed to handle failed renewalsUse the SetupIntent API to save payment methods for future use:
const setupIntent = await stripe.setupIntents.create({
customer: customerId,
automatic_payment_methods: { enabled: true },
});
on_behalf_ofcontroller propertiestry {
const session = await stripe.checkout.sessions.create({ ... });
} catch (err) {
if (err instanceof Stripe.errors.StripeCardError) {
// Card declined - show user-friendly message
} else if (err instanceof Stripe.errors.StripeInvalidRequestError) {
// Invalid parameters - fix the request
} else {
// Unexpected error - log and alert
}
}