Spell Tome of the JS SDK
— Allons-y.
Setup
import { CStarClient } from '@cstar.help/js';
const cstar = new CStarClient({
apiKey: process.env.CSTAR_SECRET_KEY,
teamId: process.env.CSTAR_TEAM_ID
}); Server-side only. sk_live_* in browsers is a one-way ticket to a key rotation.
Common moves
// Bare object on single-resource calls
const ticket = await cstar.tickets.create({ title: 'Help' });
ticket.id; // tkt_...
// { data, pagination, hasMore } on lists
const { data } = await cstar.tickets.list({ status: 'open' });
// Update / delete
await cstar.tickets.update(ticket.id, { status: 'resolved' });
await cstar.tickets.delete(ticket.id);Power moves
// Idempotent retries
await cstar.tickets.create(
{ title: 'Bug' },
{ idempotencyKey: `bug_${formId}` }
);
// Real-time stream
const off = cstar.realtime.on('ticket.*', (event) => {
console.log(event.type, event.data);
});
// Offline queue
const cstar = new CStarClient({
...,
offline: { storage: 'localStorage' }
});Errors
import { CStarRateLimitError, CStarValidationError } from '@cstar.help/js';
try {
await cstar.tickets.create(payload);
} catch (e) {
if (e instanceof CStarRateLimitError) return retryAfter(e.retryAfter);
if (e instanceof CStarValidationError) return showError(e.param, e.message);
console.error(e.code, e.requestId, e.docUrl);
throw e;
} Catch by class. Strings rotate; class identity doesn't.
Gotchas
- lastMeta overwrites on every call. Capture it on the next line.
- Lists return an envelope; everything else returns the bare object.
- Anonymous chat can create tickets, but list calls throw — call
identify()first. - Webhook signing secret is only on the create response. Save it then.
- Offline replays are at-least-once. Pair with
idempotencyKey.
Entry points
@cstar.help/js— admin (server)@cstar.help/js/auth— customer auth@cstar.help/js/chat— customer chat@cstar.help/js/library— public KB@cstar.help/js/community— public forum@cstar.help/js/quickhelp— contextual answers@cstar.help/js/proactive— signal detection@cstar.help/js/webhook— Web Crypto verify@cstar.help/js/webhook/node— Node-only verify