Stripe Subscription Implementation in Node.js (Backend Guide)
Stripe Subscriptions are used when you want to charge customers on a recurring basis, such as monthly or yearly plans. In this article, we will understand how Stripe subscriptions work internally and how to implement them step by step in a Node.js backend.
Stripe Subscription High-Level Flow
- Create a Product (what you are selling)
- Create a Price (how much and how often to charge)
- Create a Customer
- Create a Subscription
- Stripe automatically handles recurring payments
- Use webhooks to detect payment status
Important Concept: No Manual Order Creation Required
Unlike traditional payment gateways, Stripe subscriptions do not require you to manually create an order. Stripe automatically manages invoices and recurring charges. On your side, you only need to store Stripe references in your database.
Recommended fields to store:
- stripeCustomerId
- subscriptionId
- priceId
- subscriptionStatus
Step 1: Stripe Account Setup
Create a Stripe account using the official website:
From the Stripe Dashboard, copy your Secret Key and Webhook Secret.
Step 2: Install Required Packages
npm install stripe express dotenv
Basic Node.js Setup
const express = require('express'); const Stripe = require('stripe'); require('dotenv').config(); const app = express(); app.use(express.json()); const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
Step 3: Create a Product
A product represents the service or plan you are selling, such as a “Pro Plan”.
const product = await stripe.products.create({ name: 'Pro Plan' });
Step 4: Create a Price (Subscription Plan)
The price defines how much the customer is charged and at what interval.
const price = await stripe.prices.create({ unit_amount: 999, currency: 'usd', recurring: { interval: 'month' }, product: product.id });
Step 5: Create a Customer
const customer = await stripe.customers.create({ email: 'user@example.com' });
Step 6: Create a Subscription (Core API)
This API starts the subscription process.
const subscription = await stripe.subscriptions.create({ customer: customer.id, items: [{ price: price.id }], payment_behavior: 'default_incomplete', payment_settings: { save_default_payment_method: 'on_subscription' }, expand: ['latest_invoice.payment_intent'] });
What Happens After Subscription Creation?
- Stripe automatically creates an invoice
- A PaymentIntent is generated for the invoice
- The client receives a clientSecret
- The subscription becomes active after successful payment
How Automatic Recurring Payments Work
Once a payment method is saved, Stripe automatically charges the customer on every billing cycle. No cron jobs or manual payment APIs are required.
How Stripe Automatically Detects Payments
Stripe uses Webhooks to notify your backend whenever a payment-related event occurs.
Webhook Implementation (Very Important)
Official documentation:
https://stripe.com/docs/webhooks
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => { const sig = req.headers['stripe-signature']; let event; event = stripe.webhooks.constructEvent( req.body, sig, process.env.STRIPE_WEBHOOK_SECRET ); if (event.type === 'invoice.payment_succeeded') { console.log('Subscription payment successful'); } if (event.type === 'invoice.payment_failed') { console.log('Subscription payment failed'); } res.json({ received: true }); });
Cancel a Subscription
await stripe.subscriptions.del('sub_xxxxx');
Upgrade or Downgrade a Subscription
await stripe.subscriptions.update('sub_xxxxx', { items: [{ id: 'si_xxxxx', price: 'price_new' }] });
Useful Stripe Documentation Links
Conclusion
Stripe subscriptions are fully automated using invoices and webhooks. Once implemented correctly, recurring payments require no manual intervention, making Stripe an excellent choice for SaaS and subscription-based applications.


