TypeScript SDK
The Agentix TypeScript SDK provides a typed client for interacting with the Agentix API from JavaScript and TypeScript applications.
Installation
npm install @agentix/sdkpnpm add @agentix/sdkyarn add @agentix/sdkQuick Start
import { AgentixClient } from '@agentix/sdk'
const client = new AgentixClient({
apiKey: process.env.AGENTIX_API_KEY,
// Optional: custom base URL
// baseUrl: 'https://app.agentix.cl',
})
// Send a message
const response = await client.chat('agent_abc123', {
message: 'Hello, what can you help me with?',
sessionId: 'session-001',
})
console.log(response.message)Configuration
const client = new AgentixClient({
// Required: your API key
apiKey: 'agx_live_YOUR_KEY',
// Optional: API base URL (defaults to https://app.agentix.cl)
baseUrl: 'https://app.agentix.cl',
// Optional: request timeout in milliseconds (default: 30000)
timeout: 30000,
// Optional: custom fetch implementation
fetch: customFetch,
})Chat
Send a Message
const response = await client.chat('agent_id', {
message: 'What products do you have?',
sessionId: 'session-001',
visitorEmail: 'user@example.com',
visitorName: 'John',
metadata: { source: 'mobile-app' },
})
// Response
console.log(response.message) // Agent's text response
console.log(response.conversationId) // Conversation ID
console.log(response.messageId) // Message ID
console.log(response.uiSpec) // Rich UI spec (if any)Streaming Responses
const stream = client.chatStream('agent_id', {
message: 'Tell me a story',
sessionId: 'session-001',
})
for await (const chunk of stream) {
process.stdout.write(chunk.text)
}Team Chat
const response = await client.teamChat('team_id', {
message: 'I need billing help',
sessionId: 'session-001',
})
console.log(response.message)
console.log(response.agentId) // Which agent handled it
console.log(response.agentName) // Agent's display name
console.log(response.routingReason) // Why this agent was chosenAgents
Get Agent Configuration
const agent = await client.getAgent('agent_id')
console.log(agent.name)
console.log(agent.description)
console.log(agent.theme.primaryColor)
console.log(agent.features.enableRating)Get Widget Configuration
const widgetConfig = await client.getWidgetConfig('agent_id')
console.log(widgetConfig.widget.position)
console.log(widgetConfig.widget.autoOpen)
console.log(widgetConfig.theme)Conversations
Get Conversation History
const conversation = await client.getConversation('agent_id', 'session-001', {
limit: 20,
offset: 0,
})
for (const msg of conversation.messages) {
console.log(`${msg.role}: ${msg.content}`)
}Update Conversation
await client.updateConversation('agent_id', 'session-001', {
rating: 5,
feedback: 'Very helpful!',
visitorEmail: 'user@example.com',
})Error Handling
The SDK throws typed errors for API failures:
import { AgentixClient, AgentixError } from '@agentix/sdk'
try {
await client.chat('agent_id', {
message: 'Hello',
sessionId: 'session-001',
})
} catch (error) {
if (error instanceof AgentixError) {
console.error(`API Error: ${error.code}`) // e.g., 'AGENT_NOT_FOUND'
console.error(`Status: ${error.status}`) // e.g., 404
console.error(`Message: ${error.message}`) // Human-readable message
} else {
// Network or other error
console.error('Unexpected error:', error)
}
}Error Codes
| Code | Status | Description |
|---|---|---|
MISSING_API_KEY | 401 | No API key configured |
INVALID_API_KEY | 401 | API key is invalid |
AGENT_NOT_FOUND | 404 | Agent does not exist |
AGENT_NOT_PUBLIC | 403 | Agent is not public |
ORG_MISMATCH | 403 | API key org mismatch |
RATE_LIMITED | 429 | Too many requests |
INTERNAL_ERROR | 500 | Server error |
TypeScript Types
The SDK exports all types for use in your application:
import type {
AgentixClientOptions,
ChatRequest,
ChatResponse,
StreamChatResponse,
AgentConfig,
WidgetConfig,
ConversationResponse,
ConversationMessage,
ConversationUpdateRequest,
} from '@agentix/sdk'Server-Side Usage
The SDK is designed for server-side use. Never expose your API key in client-side code.
// Next.js API Route
import { AgentixClient } from '@agentix/sdk'
const client = new AgentixClient({
apiKey: process.env.AGENTIX_API_KEY!,
})
export async function POST(request: Request) {
const { message, sessionId } = await request.json()
const response = await client.chat('agent_id', {
message,
sessionId,
})
return Response.json(response)
}Next Steps
- Integration SDK - Build custom integrations
- Chat API Reference - Full endpoint documentation
- Webhooks - React to events
Last updated on