Skip to content

Chat API

AI conversation with tool calling support.

Endpoints

MethodEndpointDescription
GET/api/agents/{id}/chatGet message history
POST/api/agents/{id}/chatSend message
DELETE/api/agents/{id}/chatClear history

Get Messages

bash
GET /api/agents/{id}/chat?limit=50&conversation_id=conv-123

Query Parameters

ParameterTypeDescription
limitnumberMax messages (default: 50)
conversation_idstringFilter by conversation
beforenumberMessages before ID
afternumberMessages after ID

Response

json
{
  "messages": [
    {
      "id": 1,
      "role": "user",
      "content": "Hello!",
      "created_at": "2024-12-15T10:00:00Z"
    },
    {
      "id": 2,
      "role": "assistant",
      "content": "Hi there! How can I help you?",
      "created_at": "2024-12-15T10:00:01Z"
    }
  ],
  "meta": {
    "total": 100,
    "has_more": true
  }
}

Send Message

bash
POST /api/agents/{id}/chat
Content-Type: application/json

Request Body

json
{
  "message": "What's the weather like?",
  "conversation_id": "conv-123"
}

Response

json
{
  "id": 3,
  "role": "assistant",
  "content": "I don't have access to weather information...",
  "conversation_id": "conv-123",
  "created_at": "2024-12-15T10:01:00Z"
}

With Tool Calls

json
{
  "id": 3,
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_123",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"location\": \"Tokyo\"}"
      }
    }
  ]
}

Clear History

bash
DELETE /api/agents/{id}/chat

Query Parameters

ParameterTypeDescription
conversation_idstringClear specific conversation

Response

json
{
  "success": true,
  "deleted": 150
}

Message Roles

RoleDescription
userUser message
assistantAI response
systemSystem instruction
toolTool result

Message Format

User Message

json
{
  "id": 1,
  "role": "user",
  "content": "Hello!",
  "metadata": {
    "source": "web"
  }
}

Assistant Message

json
{
  "id": 2,
  "role": "assistant",
  "content": "Hi there!",
  "tool_calls": null
}

Tool Call

json
{
  "id": 3,
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_123",
      "type": "function",
      "function": {
        "name": "search_database",
        "arguments": "{\"query\": \"recent orders\"}"
      }
    }
  ]
}

Tool Result

json
{
  "id": 4,
  "role": "tool",
  "tool_call_id": "call_123",
  "content": "{\"results\": [...]}"
}

Streaming

For real-time responses:

bash
POST /api/agents/{id}/chat
Content-Type: application/json

{
  "message": "Tell me a story",
  "stream": true
}

SSE Response

data: {"type": "start", "id": 5}

data: {"type": "delta", "content": "Once"}

data: {"type": "delta", "content": " upon"}

data: {"type": "delta", "content": " a time"}

data: {"type": "done", "message": {...}}

Context Management

Set Context

bash
POST /api/agents/{id}/chat
Content-Type: application/json

{
  "message": "Hello",
  "context": {
    "user": {
      "name": "John",
      "plan": "premium"
    }
  }
}

System Prompt Override

json
{
  "message": "Hello",
  "system_prompt": "You are a technical support agent."
}

Conversation Threading

Start New Conversation

Omit conversation_id:

json
{
  "message": "Hello"
}

Response includes new conversation_id:

json
{
  "id": 1,
  "conversation_id": "conv-new-123",
  "content": "Hi!"
}

Continue Conversation

Include conversation_id:

json
{
  "message": "Follow-up question",
  "conversation_id": "conv-123"
}

Errors

CodeDescription
400Invalid message
429Rate limited
500AI service error

Examples

Simple Chat

bash
curl -X POST https://your-domain.com/api/agents/{id}/chat \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello!"}'

With Context

bash
curl -X POST https://your-domain.com/api/agents/{id}/chat \
  -H "Content-Type: application/json" \
  -d '{
    "message": "What is my order status?",
    "context": {
      "customer_id": "cust-123",
      "order_id": "order-456"
    }
  }'

Streaming Response

javascript
const response = await fetch('/api/agents/{id}/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    message: 'Tell me a story',
    stream: true
  })
})

const reader = response.body.getReader()
const decoder = new TextDecoder()

while (true) {
  const { done, value } = await reader.read()
  if (done) break

  const lines = decoder.decode(value).split('\n')
  for (const line of lines) {
    if (line.startsWith('data: ')) {
      const data = JSON.parse(line.slice(6))
      if (data.type === 'delta') {
        process.stdout.write(data.content)
      }
    }
  }
}

Released under the MIT License.