Documentation
Overview
Getting Started
API Reference
Examples
SDKs
JavaScript
WordPress
Guides
Products
Subscriptions
Webhooks

JavaScript SDK

Complete guide to CasPay JavaScript SDK

Installation

npm install @caspay/sdk

Initialization

import CasPay from '@caspay/sdk'; const caspay = new CasPay({ apiKey: 'your_api_key', environment: 'testnet', // or 'mainnet' onSuccess: (payment) => { console.log('Payment successful:', payment); }, onError: (error) => { console.error('Payment failed:', error); } });

Payment Methods

Create Payment

const payment = await caspay.payments.create({ productId: 'prod_123', amount: 100, currency: 'CSPR', metadata: { orderId: 'order_456', customField: 'value' } });

Amount is in smallest unit (motes for CSPR). 1 CSPR = 1,000,000,000 motes
Get Payment

const payment = await caspay.payments.get('payment_id'); console.log(payment.status); // 'pending', 'completed', 'failed'

List Payments

const payments = await caspay.payments.list({ limit: 10, offset: 0, status: 'completed' });

Subscription Methods

Create Subscription

const subscription = await caspay.subscriptions.create({ planId: 'plan_123', customerWallet: '0x...', startDate: '2024-01-01' });

Cancel Subscription

await caspay.subscriptions.cancel('subscription_id');

Event Handling

// Listen to payment events caspay.on('payment.completed', (payment) => { console.log('Payment completed:', payment); }); caspay.on('payment.failed', (error) => { console.error('Payment failed:', error); }); // Remove listener caspay.off('payment.completed', handler);

Error Handling

try { const payment = await caspay.payments.create({ productId: 'prod_123', amount: 100 }); } catch (error) { if (error.code === 'INSUFFICIENT_BALANCE') { console.error('User has insufficient balance'); } else if (error.code === 'WALLET_NOT_CONNECTED') { console.error('Please connect your wallet'); } else { console.error('Payment error:', error.message); } }

Next Steps

Products Guide

Learn how to create and manage products

Subscriptions Guide

Set up recurring payments