Skip to main content

Chat API Overview

The Chat API provides endpoints to create and manage real-time chat conversations between users and AI agents. This API supports both REST endpoints for chat management and WebSocket connections for real-time messaging with streaming responses.

Authentication

All API endpoints require an API key for authentication, which should be included in the request header. Header: X-API-Key Value: YOUR_API_KEY
This API key is unique to your tenant and should be kept secure.

Key Concepts

Chat Sessions

Chat sessions are created via the REST API and contain:
  • User identification
  • Chat metadata and configuration
  • Message history
  • Tenant isolation

Real-Time Messaging

Once a chat is created, users can connect via WebSocket to:
  • Send messages in real-time
  • Receive streaming AI responses
  • Get connection status updates
  • Handle errors gracefully

Authentication Flow

The API uses a simple authentication flow:
  1. API Key: Each tenant has a unique API key
  2. Tenant Resolution: The API key determines which tenant the request belongs to
  3. Access Control: All data is automatically scoped to the authenticated tenant

API Structure

REST API (Chat Management)

WebSocket API (Real-Time Messaging)

Quick Start

1. Create a Chat

2. Connect to WebSocket

3. Send Messages

Pagination

Endpoints that return collections (like chat messages) support pagination using offset and limit parameters:
  • offset: The number of items to skip before starting to collect results (default 0)
  • limit: The maximum number of results to return (default 50)
  • order: Sort order for messages, either ‘asc’ or ‘desc’ (default ‘asc’)
The response includes pagination metadata to help with navigation:

WebSocket Events

The WebSocket connection provides several event types:
  • Connection Events: connection_established, user_created
  • Message Events: message_received, message_stream_started, message_stream_token, message_stream_ended
  • Error Events: error with specific error codes

Error Handling

The API provides clear error responses with:
  • Error Codes: Standardized error identifiers
  • Error Messages: Human-readable descriptions
  • Details: Additional context and suggested fixes
Common error scenarios:
  • Invalid API keys
  • Chat not found
  • Missing required parameters
  • Connection timeouts

Best Practices

Chat Management

  • Create chats via REST API before connecting to WebSocket
  • Store chat IDs for future reference
  • Use meaningful titles and metadata for organization

WebSocket Connections

  • Handle connection errors gracefully
  • Implement reconnection logic for network issues
  • Process streaming tokens incrementally for better UX

Security

  • Keep API keys secure and never expose them in client-side code
  • Validate user permissions before granting chat access
  • Use HTTPS/WSS in production environments

Rate Limits

The API implements reasonable rate limits to ensure service stability:
  • REST API: 100 requests per minute per tenant
  • WebSocket: 10 messages per second per connection
  • Concurrent Connections: 50 per tenant
Rate limit headers are included in REST API responses to help you track usage.