[ { "id": "msg_001", "type": "user", "content": "Hello, I need help with my order", "createdAt": "2024-01-20T10:30:00Z" }, { "id": "msg_002", "type": "assistant", "content": "I'd be happy to help you with your order! Could you please provide your order number?", "createdAt": "2024-01-20T10:30:05Z", "metadata": { "processingTime": 1.2, "modelUsed": "gpt-4" } }, { "id": "msg_003", "type": "user", "content": "Sure, it's #12345", "createdAt": "2024-01-20T10:30:30Z" }, { "id": "msg_004", "type": "tool", "content": "Order #12345 found: Status - Shipped, Tracking: 1Z999AA1012345678", "toolCallId": "call_abc123", "createdAt": "2024-01-20T10:30:32Z" }, { "id": "msg_005", "type": "assistant", "content": "Great! I found your order #12345. It has been shipped and is currently in transit. Your tracking number is 1Z999AA1012345678.", "createdAt": "2024-01-20T10:30:35Z" }]
Endpoints
List Messages
Retrieve messages from a chat session with pagination support
GET
/
api
/
public
/
thread
/
{id}
/
messages
List Messages
curl --request GET \ --url https://api.nexusgpt.io/api/public/thread/{id}/messages \ --header 'api-key: <api-key>'
[ { "id": "msg_001", "type": "user", "content": "Hello, I need help with my order", "createdAt": "2024-01-20T10:30:00Z" }, { "id": "msg_002", "type": "assistant", "content": "I'd be happy to help you with your order! Could you please provide your order number?", "createdAt": "2024-01-20T10:30:05Z", "metadata": { "processingTime": 1.2, "modelUsed": "gpt-4" } }, { "id": "msg_003", "type": "user", "content": "Sure, it's #12345", "createdAt": "2024-01-20T10:30:30Z" }, { "id": "msg_004", "type": "tool", "content": "Order #12345 found: Status - Shipped, Tracking: 1Z999AA1012345678", "toolCallId": "call_abc123", "createdAt": "2024-01-20T10:30:32Z" }, { "id": "msg_005", "type": "assistant", "content": "Great! I found your order #12345. It has been shipped and is currently in transit. Your tracking number is 1Z999AA1012345678.", "createdAt": "2024-01-20T10:30:35Z" }]
Retrieves messages from a chat session with support for pagination, filtering, and sorting. This endpoint is essential for displaying conversation history and implementing real-time chat interfaces.
Cursor for pagination. Returns messages before this message ID.
[ { "id": "msg_001", "type": "user", "content": "Hello, I need help with my order", "createdAt": "2024-01-20T10:30:00Z" }, { "id": "msg_002", "type": "assistant", "content": "I'd be happy to help you with your order! Could you please provide your order number?", "createdAt": "2024-01-20T10:30:05Z", "metadata": { "processingTime": 1.2, "modelUsed": "gpt-4" } }, { "id": "msg_003", "type": "user", "content": "Sure, it's #12345", "createdAt": "2024-01-20T10:30:30Z" }, { "id": "msg_004", "type": "tool", "content": "Order #12345 found: Status - Shipped, Tracking: 1Z999AA1012345678", "toolCallId": "call_abc123", "createdAt": "2024-01-20T10:30:32Z" }, { "id": "msg_005", "type": "assistant", "content": "Great! I found your order #12345. It has been shipped and is currently in transit. Your tracking number is 1Z999AA1012345678.", "createdAt": "2024-01-20T10:30:35Z" }]
# Get latest 10 messagescurl -X GET "https://api.nexusgpt.io/api/public/thread/550e8400-e29b-41d4-a716-446655440000/messages?limit=10&order=desc" \ -H "api-key: YOUR_API_KEY"# Get messages with paginationcurl -X GET "https://api.nexusgpt.io/api/public/thread/550e8400-e29b-41d4-a716-446655440000/messages?limit=20&after=msg_100" \ -H "api-key: YOUR_API_KEY"
const axios = require('axios');async function getMessages(sessionId, options = {}) { const { limit = 20, order = 'asc', after = null, before = null } = options; const params = new URLSearchParams({ limit: limit.toString(), order }); if (after) params.append('after', after); if (before) params.append('before', before); try { const response = await axios.get( `https://api.nexusgpt.io/api/public/thread/${sessionId}/messages?${params}`, { headers: { 'api-key': process.env.NEXUS_API_KEY } } ); return response.data; } catch (error) { console.error('Error fetching messages:', error.response?.data); throw error; }}// Example: Get conversation historyasync function displayConversation(sessionId) { const messages = await getMessages(sessionId, { limit: 50, order: 'asc' }); messages.forEach(msg => { const timestamp = new Date(msg.createdAt).toLocaleTimeString(); console.log(`[${timestamp}] ${msg.type.toUpperCase()}: ${msg.content}`); });}// Example: Implement paginationasync function getAllMessages(sessionId) { const allMessages = []; let after = null; while (true) { const messages = await getMessages(sessionId, { limit: 100, order: 'asc', after }); if (messages.length === 0) break; allMessages.push(...messages); after = messages[messages.length - 1].id; if (messages.length < 100) break; // Last page } return allMessages;}
import osimport requestsfrom typing import Optional, List, Dict, Anyfrom datetime import datetimedef get_messages( session_id: str, limit: int = 20, order: str = 'asc', after: Optional[str] = None, before: Optional[str] = None) -> List[Dict[str, Any]]: """Get messages from a session with pagination""" url = f"https://api.nexusgpt.io/api/public/thread/{session_id}/messages" headers = { "api-key": os.environ.get("NEXUS_API_KEY") } params = { "limit": limit, "order": order } if after: params["after"] = after if before: params["before"] = before try: response = requests.get(url, headers=headers, params=params) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"Error fetching messages: {e}") if hasattr(e, 'response'): print(f"Response: {e.response.text}") raise# Example: Display conversationdef display_conversation(session_id: str): messages = get_messages(session_id, limit=50, order='asc') for msg in messages: timestamp = datetime.fromisoformat(msg['createdAt'].replace('Z', '+00:00')) time_str = timestamp.strftime('%H:%M:%S') print(f"[{time_str}] {msg['type'].upper()}: {msg['content']}")# Example: Get all messages with paginationdef get_all_messages(session_id: str) -> List[Dict[str, Any]]: all_messages = [] after = None while True: messages = get_messages( session_id, limit=100, order='asc', after=after ) if not messages: break all_messages.extend(messages) after = messages[-1]['id'] if len(messages) < 100: # Last page break return all_messages# Example: Get latest messages for pollingdef get_new_messages(session_id: str, last_message_id: str) -> List[Dict[str, Any]]: """Get messages newer than the last known message""" return get_messages( session_id, limit=10, order='asc', after=last_message_id )
// Get newest messages firstasync function backwardPagination(sessionId) { let messages = []; let before = null; do { const page = await getMessages(sessionId, { limit: 20, order: 'desc', before }); messages = page.concat(messages); // Prepend to maintain order before = page.length > 0 ? page[page.length - 1].id : null; } while (before); return messages;}
{ "id": "msg_asst_001", "type": "assistant", "content": "I'd be happy to help you with weather information...", "createdAt": "2024-01-20T10:30:05Z", "metadata": { "processingTime": 1.5, "tokensUsed": 150 }}