Alerts Quick Start
Updated
This guide walks you through creating an alert subscription, checking your inbox for triggered events, and connecting to real-time streaming. By the end you will have a working script that monitors ETH price movements.
Step 1 — Install the SDK
npm install @chaoslabs/ai-sdkCopy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Step 2 — Create the client
import { Chaos } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({
apiKey: process.env.CHAOS_API_KEY!,
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
The same Chaos client you use for chat also provides the alerts namespace. No separate initialization is needed.
Step 3 — Browse available templates
Templates are predefined alert configurations you can use as starting points:
const { templates, supportedChains } = await chaos.alerts.templates.list();
for (const tpl of templates) {
console.log(`${tpl.name} [${tpl.category}] — ${tpl.description}`);
}
console.log('Supported chains:', supportedChains.join(', '));Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Step 4 — Create a price alert
Subscribe to an alert that fires when ETH exceeds $4,000:
const subscription = await chaos.alerts.subscriptions.create({
alert: {
alert_type: 'price_change',
asset: 'ETH',
target_price: 4000,
condition: 'above',
},
});
console.log('Created:', subscription.id);
console.log('Status:', subscription.status); // 'active'
console.log('Name:', subscription.name);Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Step 5 — Poll the inbox
Check for alerts that have already triggered:
const { events, hasMore } = await chaos.alerts.inbox.list({
readState: 'unread',
limit: 10,
});
console.log(`${events.length} unread alerts`);
for (const event of events) {
console.log(`[${event.severity}] ${event.title}`);
console.log(` ${event.summary}`);
console.log(` Triggered: ${event.triggeredAt}`);
// Mark as read
await chaos.alerts.inbox.markRead(event.id);
}Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Step 6 — Stream alerts in real-time
For low-latency delivery, connect via WebSocket:
const stream = chaos.alerts.connect();
stream.on('connected', () => {
console.log('Listening for alerts...');
});
stream.on('alert', (event) => {
console.log(`[${event.severity}] ${event.title}`);
console.log(` ${event.summary}`);
});
stream.on('error', (err) => {
console.error('Stream error:', err.message);
});
// Keep the process alive
process.on('SIGINT', () => {
stream.close();
process.exit();
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Complete example
Putting it all together:
import { Chaos } from '@chaoslabs/ai-sdk';
const chaos = new Chaos({ apiKey: process.env.CHAOS_API_KEY! });
// 1. Create a price alert
const sub = await chaos.alerts.subscriptions.create({
alert: {
alert_type: 'price_change',
asset: 'ETH',
target_price: 4000,
condition: 'above',
},
});
console.log(`Created alert: ${sub.name} (${sub.id})`);
// 2. Check inbox
const { unreadCount } = await chaos.alerts.inbox.unreadCount();
console.log(`${unreadCount} unread alerts in inbox`);
// 3. Stream real-time
const stream = chaos.alerts.connect();
stream.on('alert', (event) => {
console.log(`[${event.severity}] ${event.title}: ${event.summary}`);
});
stream.on('connected', () => console.log('Streaming...'));
process.on('SIGINT', () => {
stream.close();
process.exit();
});Copy code
Open in Cursor
Open in VS Code
Open in v0
Open in Claude
Open in ChatGPT
Next steps
- Managing Subscriptions — Full CRUD, snooze/mute, and NLP creation
- Alert Inbox — Filtering, pagination, and batch operations
- Real-time Alerts — WebSocket events and reconnection
- Webhook Delivery — Push alerts to your server
- Alerts API Reference — Complete type definitions
Was this helpful?