Skip to main content
GET
/
api
/
conversations
/
{id}
/
messages
Get Messages
curl --request GET \
  --url https://api.example.com/api/conversations/{id}/messages
{
  "success": true,
  "messages": [
    {
      "id": 123,
      "sender_type": "<string>",
      "message_text": "<string>",
      "audio_url": "<string>",
      "media_type": "<string>",
      "context_used": "<string>",
      "confidence_score": 123,
      "created_at": "<string>"
    }
  ],
  "has_more": true,
  "total": 123,
  "server_time": "<string>",
  "error": "<string>"
}

Endpoint

GET /api/conversations/{id}/messages
Fetches messages for a specific conversation with pagination support. Messages are returned in chronological order (oldest first).

Path Parameters

id
integer
required
The unique identifier of the conversation

Query Parameters

offset
integer
default:"0"
Number of messages to skip for pagination
limit
integer
default:"20"
Maximum number of messages to return (max: 20)

Response

success
boolean
required
Indicates if the request was successful
messages
array
required
Array of message objects in chronological order
id
integer
Unique message identifier
sender_type
string
Who sent the message: customer, ai, or human
message_text
string
The text content of the message
audio_url
string
URL to audio file if the message is a voice message (null for text messages)
media_type
string
Type of media: text, audio, image, video, or document
context_used
string
RAG context that was used to generate AI response (null for non-AI messages)
confidence_score
float
AI confidence score for the response (0-1, null for non-AI messages)
created_at
string
Timestamp when the message was created in ISO 8601 format
has_more
boolean
required
Indicates if there are more messages available to fetch
total
integer
required
Total number of messages in the conversation
server_time
string
required
Current server time in ISO 8601 format
error
string
Error message if the request failed (only present when success is false)

Examples

curl -X GET 'https://your-domain.com/api/conversations/1/messages'

Response Examples

Success Response

{
  "success": true,
  "messages": [
    {
      "id": 45,
      "sender_type": "customer",
      "message_text": "Hello, I have a question about my order",
      "audio_url": null,
      "media_type": "text",
      "context_used": null,
      "confidence_score": null,
      "created_at": "2024-01-15 10:30:00"
    },
    {
      "id": 46,
      "sender_type": "ai",
      "message_text": "I'd be happy to help you with your order. Could you please provide your order number?",
      "audio_url": null,
      "media_type": "text",
      "context_used": "Order tracking and support documentation",
      "confidence_score": 0.92,
      "created_at": "2024-01-15 10:30:05"
    },
    {
      "id": 47,
      "sender_type": "customer",
      "message_text": "My order number is #12345",
      "audio_url": null,
      "media_type": "text",
      "context_used": null,
      "confidence_score": null,
      "created_at": "2024-01-15 10:31:00"
    }
  ],
  "has_more": false,
  "total": 3,
  "server_time": "2024-01-15 14:45:30"
}

Error Response (Missing ID)

{
  "success": false,
  "error": "Error al obtener mensajes"
}

Implementation Details

The endpoint is implemented in api/get-conversation-messages.php and performs a direct SQL query on the messages table with pagination. Route pattern (from index.php:166):
if ($requestMethod === 'GET' && 
    preg_match('#^/api/conversations/(\d+)/messages$#', $path, $matches)) {
    $_GET['id'] = $matches[1];
    require __DIR__ . '/api/get-conversation-messages.php';
}
SQL Query (from get-conversation-messages.php:28-34):
SELECT id, sender_type, message_text, audio_url, media_type, 
       context_used, confidence_score, created_at 
FROM messages 
WHERE conversation_id = :conversation_id 
ORDER BY created_at DESC 
LIMIT {limit} OFFSET {offset}

Notes

  • Messages are fetched in reverse chronological order (newest first) from the database, then reversed before returning to the client (oldest first)
  • The has_more field helps implement infinite scroll pagination
  • Returns HTTP 500 status code on error
  • The conversation ID is required; missing it will result in an error

Build docs developers (and LLMs) love