Docs
Chat API
Chat API
API endpoint for interactive SQL chat with execution
The Chat API allows you to have interactive conversations with an AI that can generate and execute SQL queries based on natural language input. This endpoint maintains chat history and provides both the SQL query and its execution results.
Endpoint
POST /faas/api/chat
Authentication
All requests must include an API key in the Authorization header:
Authorization: Bearer sk_your_api_key
API keys must start with the prefix sk_
and can be generated from your account dashboard.
Request Format
Parameter | Type | Description |
---|---|---|
chatId | string | (Optional) ID of an existing chat to continue. If not provided, a new chat will be created |
userInput | string | The natural language query to convert to SQL and execute |
modelId | string | The ID of the AI model to use for generation |
databaseId | string | The ID of the database to generate SQL for and execute against |
Example Request
{
"userInput": "How many users registered last month?",
"modelId": "gpt-4",
"databaseId": "db_12345"
}
Response Format
Field | Type | Description |
---|---|---|
chatId | string | The ID of the chat (new or existing) |
userMessageId | string | The ID of the user message that was saved |
assistantMessageId | string | The ID of the assistant message that was saved |
query | string | The generated SQL query |
results | array | The results of executing the SQL query |
error | string | null | Error message if generation or execution failed |
usage | object | Token usage information |
Example Response
{
"chatId": "chat_67890",
"userMessageId": "msg_12345",
"assistantMessageId": "msg_67890",
"query": "SELECT COUNT(*) as user_count FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);",
"results": [
{
"user_count": 157
}
],
"error": null,
"usage": {
"promptTokens": 0,
"completionTokens": 0,
"totalTokens": 0
}
}
Error Codes
Status Code | Description |
---|---|
400 | Invalid request format or no user message found |
401 | Missing or invalid API key |
404 | Model or database not found |
500 | Server error during SQL generation or execution |
Limitations
- The API has a maximum duration of 60 seconds per request
- The chat history is stored and can be retrieved using the chatId
- Each message is associated with the model and database connection used to generate it
Example Usage
// Starting a new chat
const response = await fetch('https://app.chatdb.live/faas/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk_your_api_key'
},
body: JSON.stringify({
userInput: 'How many active users do we have?',
modelId: 'gpt-4',
databaseId: 'db_12345'
})
});
const data = await response.json();
const chatId = data.chatId;
// Continuing the same chat
const followUpResponse = await fetch('https://app.chatdb.live/faas/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk_your_api_key'
},
body: JSON.stringify({
chatId: chatId,
userInput: 'How many of them registered in the last week?',
modelId: 'gpt-4',
databaseId: 'db_12345'
})
});
const followUpData = await followUpResponse.json();
console.log(followUpData.query);
console.log(followUpData.results);