Quick Start

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

This guide walks you through installing the Chaos AI SDK, sending your first request, and reading the response. By the end you will have a working script that queries the Chaos AI platform and prints the result.

Step 1 — Install the SDK

Install the package from npm:

npm install @chaoslabs/ai-sdk
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Step 2 — Create the client

Import the Chaos class and the WALLET_MODEL constant, then instantiate a client with your API key:

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

The Chaos constructor accepts a ChaosSDKOptions object:

OptionTypeDefaultDescription
apiKeystringrequiredYour Chaos Labs API key (starts with ck-)
baseUrlstringhttps://ai.chaoslabs.coAPI base URL
timeoutnumber120000Request timeout in milliseconds

Step 3 — Send your first request

Call chaos.chat.responses.create() with a model, input messages, and metadata. The WALLET_MODEL constant selects wallet mode, which gives the AI access to your on-chain portfolio data.

quickstart.ts
const response = await chaos.chat.responses.create({
  model: WALLET_MODEL,
  input: [
    { type: 'message', role: 'user', content: 'What is in my portfolio?' },
  ],
  metadata: {
    user_id: 'user-123',
    session_id: 'session-abc',
    wallets: [{ address: '0xYourWalletAddress', chain: 'ethereum' }],
  },
});
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Step 4 — Extract text from the response

The response object has a messages array of ChaosSDKMessage items. Use the extractText helper to pull out all text content:

quickstart.ts
import { Chaos, WALLET_MODEL, extractText } from '@chaoslabs/ai-sdk';
 
const chaos = new Chaos({
  apiKey: process.env.CHAOS_API_KEY!,
});
 
const response = await chaos.chat.responses.create({
  model: WALLET_MODEL,
  input: [
    { type: 'message', role: 'user', content: 'What is in my portfolio?' },
  ],
  metadata: {
    user_id: 'user-123',
    session_id: 'session-abc',
    wallets: [{ address: '0xYourWalletAddress', chain: 'ethereum' }],
  },
});
 
// Extract all text from the response
const text = extractText(response);
console.log(text);

extractText iterates over response.messages, filters for messages where type === 'text', and joins their data.text values with newlines.

Step 5 — Check for blocks

Responses can include structured blocks such as tables, charts, and transaction actions. Use the extractBlocks helper to retrieve them:

quickstart.ts
import { extractBlocks } from '@chaoslabs/ai-sdk';
 
const blocks = extractBlocks(response);
 
for (const block of blocks) {
  console.log(`Block type: ${block.type}`);
 
  if (block.type === 'table') {
    console.log('Table headers:', block.tableHeaders);
    console.log('Table rows:', block.tableRows.length);
  }
 
  if (block.type === 'pie_chart' || block.type === 'bar_chart') {
    console.log('Chart title:', block.title);
  }
 
  if (block.type === 'action') {
    console.log('Transaction primitives:', block.primitives.map(p => p.type));
  }
}

Block types include table, pie_chart, bar_chart, timeseries_chart, action, markdown, interactive, alert, code, and form.

Step 6 — Check response status

The response includes a status field that is either "completed" or "failed":

quickstart.ts
if (response.status === 'completed') {
  console.log('Request completed successfully');
  console.log('Text:', extractText(response));
  console.log('Blocks:', extractBlocks(response).length);
} else {
  console.error('Request failed:', response.error?.message);
}

Complete example

Here is the full working script:

quickstart.ts
import { Chaos, WALLET_MODEL, extractText, extractBlocks } from '@chaoslabs/ai-sdk';
 
async function main() {
  const chaos = new Chaos({
    apiKey: process.env.CHAOS_API_KEY!,
  });
 
  const response = await chaos.chat.responses.create({
    model: WALLET_MODEL,
    input: [
      { type: 'message', role: 'user', content: 'What is in my portfolio?' },
    ],
    metadata: {
      user_id: 'user-123',
      session_id: 'session-abc',
      wallets: [{ address: '0xYourWalletAddress', chain: 'ethereum' }],
    },
  });
 
  if (response.status === 'completed') {
    console.log('--- Text ---');
    console.log(extractText(response));
 
    console.log('\n--- Blocks ---');
    const blocks = extractBlocks(response);
    for (const block of blocks) {
      console.log(`[${block.type}]`, 'title' in block ? block.title : '');
    }
  } else {
    console.error('Request failed:', response.error?.message);
  }
}
 
main();

Next steps

Was this helpful?