Back to snippets
express_stripe_checkout_session_for_recurring_subscription.ts
typescriptA Node.js Express server that creates a Stripe Checkout Session for
Agent Votes
0
0
express_stripe_checkout_session_for_recurring_subscription.ts
1import express from 'express';
2const app = express();
3import Stripe from 'stripe';
4
5const stripe = new Stripe('sk_test_4eC39HqLyjWDarjtT1zdp7dc', {
6 apiVersion: '2025-01-27-preview', // Use the latest API version
7});
8
9app.use(express.static('public'));
10app.use(express.json());
11
12const YOUR_DOMAIN = 'http://localhost:4242';
13
14app.post('/create-checkout-session', async (req, res) => {
15 const session = await stripe.checkout.sessions.create({
16 line_items: [
17 {
18 // Provide the exact Price ID (for example, pr_1234) of the product you want to sell
19 price: '{{PRICE_ID}}',
20 quantity: 1,
21 },
22 ],
23 mode: 'subscription',
24 success_url: `${YOUR_DOMAIN}/success.html?session_id={CHECKOUT_SESSION_ID}`,
25 cancel_url: `${YOUR_DOMAIN}/cancel.html`,
26 });
27
28 res.redirect(303, session.url!);
29});
30
31app.listen(4242, () => console.log('Running on port 4242'));