Back to AI Docs
Chat API
Send messages and receive AI responses with optional streaming support. Include a projectId to enable knowledge-aware responses.
Endpoints
POST
/chatSend a message and receive a complete AI response.
Request Body
{
"messages": [
{ "role": "user", "content": "How is authentication handled?" }
],
"projectId": "project-123", // Optional: enables RAG
"provider": "openai", // "openai" | "anthropic"
"model": "gpt-5", // Optional
"temperature": 0.7, // Optional: 0-2
"maxTokens": 4096, // Optional
"systemPrompt": "You are..." // Optional
}Response
{
"id": "resp_abc123",
"sessionId": "sess_xyz789",
"content": "Authentication is implemented using...",
"role": "assistant",
"model": "gpt-5",
"provider": "openai",
"usage": {
"promptTokens": 150,
"completionTokens": 200,
"totalTokens": 350
},
"createdAt": "2024-01-15T10:30:00Z"
}POST
/chat/streamStream AI responses in real-time via Server-Sent Events (SSE). Supports streams up to 15+ minutes without timeout.
Request Headers
Accept: text/event-stream Content-Type: application/json
Stream Events
data: {"content": "Auth", "sessionId": "sess_xyz"}
data: {"content": "entication", "sessionId": "sess_xyz"}
data: {"content": " is", "sessionId": "sess_xyz"}
data: {"done": true, "sessionId": "sess_xyz"}React Hook: useAIChat
The recommended way to integrate chat in React applications.
import { useAIChat } from '@/hooks/use-ai-chat'
function Chat({ projectId }) {
const {
messages, // Message[]
isStreaming, // boolean
sessionId, // string | null
error, // Error | null
sendMessage, // (content: string) => Promise<void>
stopStream, // () => void
clearMessages, // () => void
} = useAIChat({
projectId,
provider: 'openai',
model: 'gpt-5',
systemPrompt: 'You are a helpful assistant.',
onChunk: (chunk) => console.log('Received:', chunk),
onComplete: (content, sessionId) => console.log('Done'),
onError: (err) => console.error(err),
})
return (
<div>
{messages.map(m => (
<div key={m.id}>
{m.role}: {m.content}
{m.isStreaming && '▊'}
</div>
))}
<button onClick={() => sendMessage('Hello!')}>
Send
</button>
{isStreaming && (
<button onClick={stopStream}>Stop</button>
)}
</div>
)
}Message Type
interface Message {
id: string
role: 'user' | 'assistant' | 'system'
content: string
createdAt: Date
isStreaming?: boolean
}cURL Examples
Simple Chat
curl -X POST https://testing.codmir.com/api/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "What does this project do?"}
],
"projectId": "my-project",
"provider": "openai"
}'Streaming Chat
curl -X POST https://testing.codmir.com/api/chat/reason \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{
"messages": [
{"role": "user", "content": "Explain the architecture"}
],
"projectId": "my-project"
}'