Checkout Quickstart Fresh
Build a working Stripe Checkout integration with Node.js. Customers click a button on your site and are redirected to a Stripe-hosted payment page.
1. Install the Stripe library
bash
npm install --save stripe2. Create a server
javascript
// server.js
const express = require('express');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app = express();
app.use(express.static('public'));
app.use(express.json());
// Create Checkout Session
app.post('/create-checkout-session', async (req, res) => {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'Stubborn Attachments',
images: [], // no external images
},
unit_amount: 2000,
},
quantity: 1,
},
],
mode: 'payment',
success_url: `${req.headers.origin}/success.html?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${req.headers.origin}/cancel.html`,
});
res.redirect(303, session.url);
});
app.listen(4242, () => console.log('Running on http://localhost:4242'));3. Create the frontend
html
<!-- public/index.html -->
<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
</head>
<body>
<h1>Stubborn Attachments</h1>
<p>$20.00</p>
<form action="/create-checkout-session" method="POST">
<button type="submit">Checkout</button>
</form>
</body>
</html>4. Create success and cancel pages
html
<!-- public/success.html -->
<!DOCTYPE html>
<html>
<body>
<h1>Thanks for your order!</h1>
<p>Payment successful. We'll send a confirmation email shortly.</p>
</body>
</html>html
<!-- public/cancel.html -->
<!DOCTYPE html>
<html>
<body>
<h1>Order canceled</h1>
<p>Come back when you're ready.</p>
</body>
</html>5. Set environment variables
bash
STRIPE_SECRET_KEY=sk_test_YOUR_TEST_KEY6. Run the server
bash
node server.jsVisit http://localhost:4242 and click Checkout. Use card 4242 4242 4242 4242 to complete a test payment.
7. Handle fulfillment with webhooks
Don't rely on the redirect URL alone — customers may close the tab. Listen for the webhook:
javascript
// Add webhook handler to server.js
app.post('/webhook', express.raw({ type: 'application/json' }), async (req, res) => {
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
req.headers['stripe-signature'],
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
console.log('Payment succeeded for session:', session.id);
// Fulfill the order here
}
res.json({ received: true });
});8. Test webhooks locally
bash
stripe listen --forward-to localhost:4242/webhookThen trigger a test event:
bash
stripe trigger checkout.session.completedUsing existing prices
If you have products and prices defined in the Dashboard:
javascript
const session = await stripe.checkout.sessions.create({
line_items: [
{ price: 'price_1ExistingPriceId', quantity: 1 },
],
mode: 'payment',
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
});Next steps
- Embedded Checkout — keep customers on your domain
- Fulfillment — handle post-payment order fulfillment
- Save Payment Methods — save cards for future purchases