TypeScript SDK
A small typed client for Node 20 or later, with no dependencies.
npm install chatter-sdkimport { Chatter } from 'chatter-sdk';
const chatter = new Chatter({ apiKey: process.env.CHATTER_API_KEY! });Launch and wait
const { taskId } = await chatter.chats.launch({
template: 'bestbuy',
details: {
orderNumber: 'BBY01-806912345678',
customerName: 'Jordan Lee',
customerEmail: 'jordan@example.com',
customerPhone: '555-010-2030',
issue: 'Item arrived with a cracked screen',
resolution: 'refund',
},
});
const chat = await chatter.chats.waitUntilDone(taskId!, {
onMessages: (messages) => messages.forEach((m) => console.log(m.sender, m.content)),
onHelp: async (request) => {
console.log('The AI is asking:', request.reason);
return { action: 'ignore_and_push' }; // or return nothing to leave it for a person
},
});
console.log(chat.outcome, chat.summary);waitUntilDone keeps waiting on the server's side until the chat is over. onHelp runs when the AI asks for help: return an answer to send it, or return nothing and answer later with chatter.chats.answer(id, ...).
Many chats
const { ids } = await chatter.chats.launchMany(
rows.map((row) => ({ template: 'bestbuypricematch', details: { ...customer, ...row } })),
);
let waiting = ids;
while (waiting.length > 0) {
const result = await chatter.chats.waitAny(waiting);
for (const task of result.tasks.filter((t) => result.finished.includes(t.id))) {
console.log(task.orderNumber, task.outcome, task.summary);
}
for (const id of result.needsHelp) await chatter.chats.answer(id, { action: 'ignore_and_push' });
waiting = [...result.stillRunning, ...result.needsHelp];
}Everything else
| Area | Methods |
|---|---|
| Start here | overview(), templates(), me() |
chats | launch, launchMany, list, get, getFull, messages, wait, waitAny, waitUntilDone, sendMessage, pause, resume, answer, cancel, restart, followUp, reschedule, delete |
drafts | create, deploy, delete |
orders | list, get, create, update, attempts, details, importCsv, delete |
proxies | list, add, replace, clear |
billing | summary, usage, topupLink |
webhooks | list, create, update, delete, deliveries, test |
Anything not wrapped is one call away: chatter.request('GET', '/api/...').
Retries and safety
- Calls that launch or create send an
Idempotency-Keyfor you, so a retry can never start a chat twice. Pass your own with{ idempotencyKey }to make your own retries safe as well. - Rate limits, server errors and network failures are retried twice, waiting as long as the server asks. Change it with
new Chatter({ apiKey, maxRetries }).
Errors
import { ChatterError } from 'chatter-sdk';
try {
await chatter.chats.launch(input);
} catch (err) {
if (err instanceof ChatterError && err.code === 'chat_closed') {
await chatter.chats.launch({ ...input, scheduledAt: err.body.nextOpenAt as string });
} else {
throw err;
}
}code values are listed in Errors and limits.
Webhooks
import { verifyWebhook } from 'chatter-sdk';
const event = verifyWebhook(process.env.CHATTER_WEBHOOK_SECRET!, rawBody, request.headers['x-chatter-signature']);
if (event.type === 'chat.needs_help') {
// ...
}It throws if the signature is wrong or more than five minutes old. Pass the raw body exactly as received.