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

ParameterTypeDescription
chatIdstring(Optional) ID of an existing chat to continue. If not provided, a new chat will be created
userInputstringThe natural language query to convert to SQL and execute
modelIdstringThe ID of the AI model to use for generation
databaseIdstringThe 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

FieldTypeDescription
chatIdstringThe ID of the chat (new or existing)
userMessageIdstringThe ID of the user message that was saved
assistantMessageIdstringThe ID of the assistant message that was saved
querystringThe generated SQL query
resultsarrayThe results of executing the SQL query
errorstring | nullError message if generation or execution failed
usageobjectToken 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 CodeDescription
400Invalid request format or no user message found
401Missing or invalid API key
404Model or database not found
500Server 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);