Skip to content

JavaScript SDK

Terminal window
npm install @storelayer/sdk
import { Storelayer } from '@storelayer/sdk'
const client = new Storelayer({
apiKey: process.env.STORELAYER_API_KEY,
})
// List customers
const customers = await client.customers.list({ limit: 10 })
// Create a customer
const customer = await client.customers.create({
email: 'jane@example.com',
name: 'Jane Doe',
})
// Credit points
await client.wallets.credit(customer.id, {
amount: 500,
reason: 'Welcome bonus',
})
// Check balance
const balance = await client.wallets.balance(customer.id)
console.log(`Balance: ${balance.balance} points`)
const client = new Storelayer({
apiKey: 'YOUR_API_KEY',
baseUrl: 'https://api.storelayer.io', // default
timeout: 30000, // 30s default
})
// List
const customers = await client.customers.list({ page: 1, limit: 20 })
// Get
const customer = await client.customers.get('cust_abc123')
// Create
const newCustomer = await client.customers.create({ email: 'jane@example.com' })
// Update
await client.customers.update('cust_abc123', { name: 'Jane Smith' })
// Delete
await client.customers.delete('cust_abc123')
// Balance
const balance = await client.wallets.balance('cust_abc123')
// Credit
await client.wallets.credit('cust_abc123', { amount: 100, reason: 'Purchase' })
// Debit
await client.wallets.debit('cust_abc123', { amount: 50, reason: 'Redemption' })
// Transactions
const txns = await client.wallets.transactions('cust_abc123')
// Send event
await client.events.send({
event: 'purchase',
customerId: 'cust_abc123',
amount: 4999,
})
// Batch events
await client.events.sendBatch([
{ event: 'purchase', customerId: 'cust_abc123', amount: 4999 },
{ event: 'review', customerId: 'cust_abc123' },
])
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}`)
}
}

The SDK is written in TypeScript and exports all types:

import type { Customer, Wallet, Transaction } from '@storelayer/sdk'