Overview
The SnapPay JavaScript SDK provides a comprehensive interface for integrating payment processing, subscription management, and real-time event streaming into your JavaScript/Node.js applications. Built with modern async/await patterns and robust error handling, it offers seamless integration with SnapPayβs complete platform. Requirements: Node.js 18+Key Features
- Modern async/await design with fetch API for optimal performance
- Comprehensive error handling with typed error responses
- Real-time event streaming via Server-Sent Events (SSE)
- Service-based architecture for organized functionality
- Automatic retry logic and error handling
- Environment-based configuration
- TypeScript support with full type definitions
Installation
Configuration & Initialization
The SDK is configured through environment variables and the clientβs constructor. All operations require proper authentication and should use async/await patterns for optimal resource management.API Key Authentication
Authentication is handled via an API key that must start withpk_test_ (for testing) or pk_live_ (for production). Configure your API key in one of these ways:
Environment Variable (Recommended):
Core Methods
All methods are async functions that return promises and must be called withawait. The SDK uses a service-based architecture where methods are organized into logical groups: customers, checkout, access, and usage.
Customer Management
customers.get
Retrieves or creates a customer record (upsert logic).customerId(string): Customer identifieremail(string, optional): Customer email addressname(string, optional): Customer full name
Customer object with:
customerId(string): SnapPay customer IDemail(string): Customer emailname(string): Customer name
Checkout Sessions
checkout.createSession
Creates a payment checkout session URL for customer purchases.customerId(string): SnapPay customer IDproductId(string): Product ID from your SnapPay dashboardsuccessUrl(string): URL to redirect after successful paymentcancelUrl(string, optional): URL to redirect on cancellationpriceId(string, optional): The optional price ID
CheckoutSession object with:
sessionId(string): Unique session identifierurl(string): Checkout URL for customerexpiresAt(string): Session expiration timestamp
Access Control
access.check
Checks if a customer has access to a specific feature based on their subscription and usage limits.customerId(string): SnapPay customer IDfeatureId(string): Feature identifier
AccessCheck object with:
hasAccess(boolean): Whether customer has accessfeatureId(string): Feature being checkedusage(number | null): Current usage (null if unlimited/no access)allowance(number | null): Usage limit (null if unlimited/no access)nextResetAt(number | null): Next reset timestamp in epoch seconds (null if unlimited/no access)
Usage Tracking
usage.track
Reports usage for a metered feature. This action is idempotent to prevent duplicate tracking.customerId(string): SnapPay customer IDfeatureId(string): Feature identifier for usage trackingusage(number): Usage amount to trackidempotencyKey(string, optional): Prevents duplicate tracking (random UUID if not set)
TrackUsageResponse object with tracking confirmation details.
customerId(string): The ID of the customer whose usage is being tracked.featureId(string): The ID of the feature being tracked for usage.usageRecorded(number): The amount of usage recorded in this operation.usageTotal(number): The total usage amount after this operation.timestamp(string): The timestamp when the usage was recorded.idempotencyKey(string): A unique key to prevent duplicate tracking of this operation.
usage.get
Retrieves current usage details for a customerβs feature.customerId(string): SnapPay customer IDfeatureId(string): Feature identifier
GetUsageResponse[] array with items containing:
totalUsage(number)productId(string)featureId(string)remaining(number | null)limit(number | null)nextResetAt(number | null)
Real-time Event Handling
Say goodbye to webhook hell! SnapPay eliminates the complexity of managing payment webhooks by processing all provider webhooks (Stripe, PayPal, etc.) internally and delivering clean, structured events directly to your application via Server-Sent Events (SSE).π« What You DONβT Need:
- No webhook endpoints to create and maintain
- No webhook signature verification
- No webhook retry logic or failure handling
- No webhook security concerns
- No debugging webhook delivery issues
β What You GET:
- Real-time events delivered instantly to your application
- Guaranteed delivery with automatic reconnection
- Clean, structured data - no raw webhook payloads
- Multiple consumption patterns to fit your architecture
- Type-safe event objects with full IDE support