Endpoint
This endpoint runs on Vercel’s Edge Runtime for optimal performance and low latency.
Request Body
The ID of the chat session to retrieve messages from. Must be a valid chat ID created via
/api/create-chat.Response
Returns an array of message objects.Array of message objects representing the conversation history
Message Object
Unique identifier for the message
The chat session this message belongs to
The text content of the message
The role of the message sender. Either:
"user"- Message sent by the human user"system"- Response generated by the AI assistant
ISO 8601 timestamp of when the message was created
Example Request
Example Response
Response Structure
Messages are returned as a JSON array, ordered by creation time (oldest first):- Empty array
[]is returned if no messages exist for the chat - Each message includes complete metadata
- Timestamps are in ISO 8601 format (UTC)
Use Cases
Load Conversation History
When a user opens an existing chat, retrieve all previous messages:Sync Messages
Periodically sync messages to ensure clients have the latest data:Export Conversation
Export the full conversation history for backup or analysis:Message Roles
Therole field uses the user_system_enum database enum:
| Role | Description |
|---|---|
user | Messages sent by the human user |
system | Responses generated by the AI assistant |
The role
"system" refers to AI-generated messages, not system administration messages. All AI responses use the "system" role.Database Schema
Messages are stored with the following schema:| Field | Type | Description |
|---|---|---|
id | serial | Primary key (auto-increment) |
chatId | integer | Foreign key to chats table |
content | text | Message content (unlimited length) |
role | enum | Either "user" or "system" |
createdAt | timestamp | Creation timestamp (auto-generated) |
Performance Considerations
- The endpoint runs on Edge Runtime for low latency
- Messages are retrieved with a single database query
- No pagination is implemented - all messages are returned
- For chats with 1000+ messages, consider implementing pagination
Error Handling
The endpoint returns appropriate HTTP status codes:- 200: Success - returns message array (may be empty)
- 500: Internal server error - database query failed
Best Practices
- Cache messages locally to reduce API calls
- Implement optimistic UI updates when sending new messages
- Use this endpoint when loading a chat, not after every message
- Handle empty message arrays gracefully
- Display timestamps in the user’s local timezone
- Show message status indicators (sending, sent, delivered)
Related Operations
- Create new messages via
/api/chat - Create chat sessions via
/api/create-chat - Messages are automatically saved during chat streaming