Messages are individual DMs sent or received within threads. This guide covers sending messages, retrieving message history, and handling common messaging scenarios.
Send a message in a thread you’ve already created or looked up:
Copy
const { data } = await client.post(`/threads/${threadId}/messages`, { text: 'Thanks for your interest! How can I help you today?'});console.log('Message sent:', data.message.id);
Response:
Copy
{ "message": { "id": "msg_abc123", "threadId": "thread_xyz789", "text": "Thanks for your interest! How can I help you today?", "direction": "outbound", "createdAt": "2025-11-23T16:45:00.000Z", "platformId": "1234567890" }}
async function sendFollowUp(threadId: string) { // Check if we sent last message const { data } = await client.get(`/threads/${threadId}/messages`, { params: { limit: 1 } }); const lastMessage = data.messages[0]; if (lastMessage.direction === 'outbound') { // We're still waiting for reply await client.post(`/threads/${threadId}/messages`, { text: 'Just following up on my previous message. Let me know if you have any questions!' }); console.log('Follow-up sent'); }}
// Validate before sendingif (!text.trim()) { throw new Error('Message text cannot be empty');}await client.post(`/threads/${threadId}/messages`, { text });
Check for empty messages, length limits, and appropriate content before sending.
Copy
function validateMessage(text: string) { if (!text?.trim()) throw new Error('Empty message'); if (text.length > 10000) throw new Error('Message too long'); return true;}
Handle rate limiting gracefully
Implement delays and retry logic for bulk operations:
Copy
async function sendWithRetry(threadId: string, text: string, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await client.post(`/threads/${threadId}/messages`, { text }); } catch (error) { if (error.response?.status === 429 && i < maxRetries - 1) { await new Promise(resolve => setTimeout(resolve, 2000 * (i + 1))); } else { throw error; } } }}
Use quick send for simplicity
When you don’t need thread details, quick send reduces API calls and complexity.
Paginate message history
Don’t assume all messages fit in one page. Always implement pagination for threads with many messages.