Alerts Quick Start

Updated
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

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

terminal
npm install @chaoslabs/ai-sdk

Step 2 — Create the client

alerts-quickstart.ts
import { Chaos } from '@chaoslabs/ai-sdk';
 
const chaos = new Chaos({
  apiKey: process.env.CHAOS_API_KEY!,
});

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:

browse-templates.ts
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(', '));

Step 4 — Create a price alert

Subscribe to an alert that fires when ETH exceeds $4,000:

create-alert.ts
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);
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Step 5 — Poll the inbox

Check for alerts that have already triggered:

poll-inbox.ts
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);
}

Step 6 — Stream alerts in real-time

For low-latency delivery, connect via WebSocket:

stream-alerts.ts
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();
});
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Complete example

Putting it all together:

alerts-quickstart-full.ts
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();
});

Next steps

Was this helpful?