Docs
Messages API

Messages API

API endpoint for retrieving chat messages

The Messages API allows you to retrieve all messages for a specific chat. This endpoint returns the complete message history for a given chat ID, providing access to the conversation between the user and the AI assistant.

Endpoint

GET /faas/api/messages?chatId={chatId}

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
chatIdstringThe ID of the chat to retrieve messages for

Example Request

GET /faas/api/messages?chatId=chat_12345

Response Format

The response is an array of message objects directly from the database, each containing the following fields:

FieldTypeDescription
idstringThe unique identifier for the message
chatIdstringThe ID of the chat this message belongs to
rolestringThe role of the message sender (user or assistant)
contentobjectThe content of the message (may include various fields depending on message type)
createdAtstringThe timestamp when the message was created
updatedAtstringThe timestamp when the message was last updated

Example Response

[
  {
    "id": "msg_12345",
    "chatId": "chat_12345",
    "role": "user",
    "content": {
      "type": "text",
      "text": "How many users registered last month?"
    },
    "createdAt": "2023-06-15T10:30:00Z",
    "updatedAt": "2023-06-15T10:30:00Z"
  },
  {
    "id": "msg_67890",
    "chatId": "chat_12345",
    "role": "assistant",
    "content": {
      "type": "text",
      "text": "Based on the database, 157 users registered last month. Here's the SQL query I used:\n\n```sql\nSELECT COUNT(*) FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH);\n```\n\nThe results show that 157 new users registered in the past 30 days."
    },
    "createdAt": "2023-06-15T10:30:05Z",
    "updatedAt": "2023-06-15T10:30:05Z"
  }
]

Error Codes

Status CodeDescription
400Missing or invalid chatId parameter
401Missing or invalid API key
403Unauthorized access to chat
404Chat not found
500Server error

Limitations

  • The API returns all messages for the specified chat
  • For private chats, only the chat owner can access the messages
  • Public chats can be accessed by any authenticated user with a valid API key
  • The message content structure may vary depending on the message type

Example Usage

// Fetching messages for a specific chat
const response = await fetch('https://app.chatdb.live/faas/api/messages?chatId=chat_12345', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer sk_your_api_key'
  }
});
 
const messages = await response.json();
console.log(messages);