JavaScript
Install and use the CastBrick JavaScript SDK
JavaScript SDK
This guide shows how to install, initialize, and use the CastBrick JavaScript SDK (Node.js).
Installation
npm install @castbrick/sdk
# or
yarn add @castbrick/sdkInitialization
import CastBrick from '@castbrick/sdk';
const client = new CastBrick({ apiKey: process.env.CASTBRICK_API_KEY });Send an Email
const res = await client.email.send({
from: 'no-reply@yourdomain.com',
to: ['alice@example.com'],
subject: 'Welcome',
html: '<p>Hello Alice</p>'
});
console.log(res.id, res.status);Send an SMS
const res = await client.sms.send({
from: '+15551234567',
to: ['+15557654321'],
body: 'Your code is 123456'
});
console.log(res.id, res.status);Templates
const tpl = await client.email.templates.create({
name: 'welcome',
subject: 'Welcome, {{name}}',
html: '<p>Hello {{name}}</p>'
});
await client.email.send({
from: 'no-reply@yourdomain.com',
to: ['bob@example.com'],
template_id: tpl.id,
template_data: { name: 'Bob' }
});Error handling
The SDK throws on network errors and returns structured errors for API responses. Example:
try {
await client.email.send(...)
} catch (err) {
console.error(err.status, err.message);
}