WIP
A flexible and lightweight SDK for building Stripe integrations with dynamic endpoints, caching, and response transformations.
Stripe SDK for Node.js provides the ability to call Stripe REST API from a Node.js server. It includes making API requests to Stripe servers, handling payments, customers, subscriptions, and supporting all v1 endpoints as well as the officially available v2 endpoints for metered billing and events.
Based on the Stripe REST API, supporting both v1 and v2 endpoints.
- Node.js >= 18.0.0
- Pre-configured API endpoints for Stripe's platform
- Support for both v1 and officially available v2 endpoints with appropriate content types
- Redis and in-memory caching capabilities
- Easy extensibility
- Convenient methods for Payment Intents, Customers, Products, and more
- TypeScript support with namespaced types for API versions
- Stripe SDK
Install the SDK using pnpm
or yarn
:
pnpm add @nuecms/stripe-sdk
# or
yarn add @nuecms/stripe-sdk
import { stripeSdk } from '@nuecms/stripe-sdk';
const sdk = stripeSdk({
apiKey: 'your-stripe-api-key', // sk_test_... or sk_live_...
apiVersion: '2023-10-16', // Optional, defaults to latest version
});
The SDK comes with pre-registered endpoints, but you can add custom ones:
sdk.r('customEndpoint', '/v1/custom/path', 'POST');
const paymentIntent = await sdk.createPaymentIntent({
amount: 2000, // $20.00 in cents
currency: 'usd',
payment_method_types: ['card']
});
console.log(paymentIntent.id);
All V2 API endpoints are prefixed with v2
in the method name:
// Create a meter event for usage-based billing
const meterEvent = await sdk.v2CreateMeterEvent({
meter_event_stream: 'mes_123456789',
measurement_time: Math.floor(Date.now() / 1000),
payload: { quantity: 5 }
});
The SDK can be configured using environment variables. Copy the env.example
file to .env
and modify as needed:
cp env.example .env
Required environment variables:
VITE_STRIPE_API_KEY
: Your Stripe Secret API Key (starts withsk_test_
orsk_live_
)
Example usage:
import { stripeSdk, RedisCacheProvider } from '@nuecms/stripe-sdk';
import Redis from 'ioredis';
// Initialize with environment variables
const redis = new Redis(process.env.REDIS_URL);
const sdk = stripeSdk({
apiKey: process.env.VITE_STRIPE_API_KEY,
});
// Create a payment intent for $25
const paymentIntent = await sdk.createPaymentIntent({
amount: 2500,
currency: 'usd',
payment_method_types: ['card'],
description: 'Product purchase'
});
// Later, confirm the payment intent with a payment method
const confirmedIntent = await sdk.confirmPaymentIntent({
payment_intent_id: paymentIntent.id,
payment_method: 'pm_card_visa'
});
// Create a customer
const customer = await sdk.createCustomer({
email: '[email protected]',
name: 'Jane Smith'
});
// Attach a payment method to the customer
await sdk.attachPaymentMethod({
payment_method_id: 'pm_card_visa',
customer: customer.id
});
// Create a subscription
const subscription = await sdk.createSubscription({
customer: customer.id,
items: [{ price: 'price_premium_monthly' }]
});
// Create a meter event stream to define the schema
const stream = await sdk.v2CreateMeterEventStream({
display_name: 'API Calls',
schema: {
type: 'object',
properties: {
quantity: { type: 'number' },
operation: { type: 'string' }
},
required: ['quantity']
}
});
// Create a meter event session
const session = await sdk.v2CreateMeterEventSession({
meter_event_stream: stream.id
});
// Create a meter event
const event = await sdk.v2CreateMeterEvent({
meter_event_stream: stream.id,
measurement_time: Math.floor(Date.now() / 1000),
payload: {
quantity: 1,
operation: 'get_data'
}
});
// Close the session when done
await sdk.v2UpdateMeterEventSession({
meter_event_session_id: session.id,
status: 'closed'
});
// Create an event destination for webhooks
const destination = await sdk.v2CreateEventDestination({
endpoint_url: 'https://example.com/webhook',
events_types: ['billing.meter_event.created'],
description: 'Meter event webhook'
});
// List all event destinations
const destinations = await sdk.v2ListEventDestinations({
limit: 10
});
// Enable/disable an event destination
await sdk.v2EnableEventDestination({ id: destination.id });
await sdk.v2DisableEventDestination({ id: destination.id });
// Test an event destination with a ping
const pingResult = await sdk.v2PingEventDestination({ id: destination.id });
if (pingResult.status === 'succeeded') {
console.log('Webhook endpoint is reachable');
}
// Delete an event destination when no longer needed
await sdk.v2DeleteEventDestination({ id: destination.id });
Stripe provides two API namespaces with different behaviors. Our SDK supports both:
- v1 API: Most common endpoints using form-encoded requests
- v2 API: Limited set of newer endpoints with JSON requests/responses
Currently, the v2 API includes:
- Billing: Meter Events, Event Adjustments, Event Sessions, Event Streams
- Core: Events and Event Destinations
For detailed information about the differences between v1 and v2 APIs, see our V2 API Overview document.
Key differences handled by our SDK:
- Content Types: v1 uses form-encoding, v2 uses JSON
- Pagination: v1 uses cursor-based, v2 uses page tokens
- Features: Different idempotency behavior, metadata handling, and more
We welcome contributions to improve this SDK! To get started:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name
). - Commit your changes (
git commit -m "Add feature X"
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
This SDK is released under the MIT License. You're free to use, modify, and distribute this project. See the LICENSE
file for more details.