JavaScript SDK
Installation
Section titled “Installation”npm install @storelayer/sdkbun add @storelayer/sdkpnpm add @storelayer/sdkQuick Start
Section titled “Quick Start”import { Storelayer } from '@storelayer/sdk'
const client = new Storelayer({ apiKey: process.env.STORELAYER_API_KEY,})
// List customersconst customers = await client.customers.list({ limit: 10 })
// Create a customerconst customer = await client.customers.create({ name: 'Jane Doe',})
// Credit pointsawait client.wallets.credit(customer.id, { amount: 500, reason: 'Welcome bonus',})
// Check balanceconst balance = await client.wallets.balance(customer.id)console.log(`Balance: ${balance.balance} points`)Configuration
Section titled “Configuration”const client = new Storelayer({ apiKey: 'YOUR_API_KEY', baseUrl: 'https://api.storelayer.io', // default timeout: 30000, // 30s default})Resources
Section titled “Resources”Customers
Section titled “Customers”// Listconst customers = await client.customers.list({ page: 1, limit: 20 })
// Getconst customer = await client.customers.get('cust_abc123')
// Create
// Updateawait client.customers.update('cust_abc123', { name: 'Jane Smith' })
// Deleteawait client.customers.delete('cust_abc123')Wallets
Section titled “Wallets”// Balanceconst balance = await client.wallets.balance('cust_abc123')
// Creditawait client.wallets.credit('cust_abc123', { amount: 100, reason: 'Purchase' })
// Debitawait client.wallets.debit('cust_abc123', { amount: 50, reason: 'Redemption' })
// Transactionsconst txns = await client.wallets.transactions('cust_abc123')Events
Section titled “Events”// Send eventawait client.events.send({ event: 'purchase', customerId: 'cust_abc123', amount: 4999,})
// Batch eventsawait client.events.sendBatch([ { event: 'purchase', customerId: 'cust_abc123', amount: 4999 }, { event: 'review', customerId: 'cust_abc123' },])Error Handling
Section titled “Error Handling”import { StorelayerError } from '@storelayer/sdk'
try { await client.wallets.debit('cust_abc123', { amount: 9999, reason: 'test' })} catch (error) { if (error instanceof StorelayerError) { console.error(`Error ${error.code}: ${error.message}`) }}TypeScript
Section titled “TypeScript”The SDK is written in TypeScript and exports all types:
import type { Customer, Wallet, Transaction } from '@storelayer/sdk'