Stripe for JavaScript (Node.js): Complete Guide with Examples
1. What It Is and Why You Need It
Stripe is a leading payment platform that enables online stores, SaaS services, and any other web applications to accept payments from customers worldwide. The official stripe library for Node.js provides developers with a full set of tools to integrate the Stripe Payment API into the server-side of an application. It abstracts the complexity of direct HTTP requests to the Stripe API, offering a convenient, typed, and predictable interface in JavaScript.
Why do you need this library? In modern web development, secure payment processing is one of the most critical tasks. Stripe handles PCI DSS compliance, processes sensitive data (card numbers, CVV), and manages complex scenarios (subscriptions, refunds, disputes). The Node.js library makes it easy to create payment forms, manage customers, set up subscriptions, handle webhooks, and generate reports — all using promise-based asynchronous code.
With this library, you can focus on your application's business logic rather than the low-level details of payment protocols. It's perfect for Node.js applications running on Express, Koa, Fastify, or any other framework. The library is actively maintained by Stripe, regularly updated, and has a massive community.
2. Installation
To install the Stripe library in your Node.js project, use the npm or yarn package manager. Run one of the following commands in your project's root directory:
# Using npm
npm install stripe
# Using yarn
yarn add stripe
After installation, you can import the library into your code:
// Modern syntax (ES Modules)
import Stripe from 'stripe';
// Or CommonJS (Node.js default)
const Stripe = require('stripe');
Important: To work with the library, you'll need a secret key (sk_test_... for test mode or sk_live_... for production). You can get your key in the Stripe Dashboard.
3. Quick Start — Minimal Working Example
Let's create a simple script that creates a PaymentIntent — the standard way to accept a one-time payment.
// Import the library
const Stripe = require('stripe');
// Initialize with your secret key (replace with your test key)
const stripe = Stripe('sk_test_XXXXXXXXXXXXXXXXXXXXXXXX');
async function createPayment() {
try {
// Create a PaymentIntent for 2000 cents ($20.00)
const paymentIntent = await stripe.paymentIntents.create({
amount: 2000,
currency: 'usd',
// Payment description (optional)
description: 'Test payment from Node.js',
// Automatically capture the payment (true) or only authorize (false)
capture_method: 'automatic',
});
console.log('PaymentIntent created successfully:');
console.log(`ID: ${paymentIntent.id}`);
console.log(`Status: ${paymentIntent.status}`);
console.log(`Amount: ${paymentIntent.amount} ${paymentIntent.currency.toUpperCase()}`);
} catch (error) {
console.error('Error creating PaymentIntent:', error.message);
}
}
createPayment();
This code
Also in library
Kotlin Task Scheduler: kotlinx.coroutines and Libraries for Background Tasks
Prometheus metrics in Node.js: prom-client library for monitoring
Sending SMS in TypeScript: API Libraries and Integration
Best Logging Libraries for Go (Golang): Overview and Comparison
inversify for TypeScript: Complete Guide with Examples