Skip to main content
The Chat API is deprecated. It will be replaced with a new tutorial using Completions V2 API.
This tutorial demonstrates how to build interactive chat experiences using Gloo AI APIs. You’ll learn to create chat sessions, send messages, continue conversations, and retrieve chat history. The Chat and Message APIs allow for:
  • Persistent conversations with session management
  • Contextual responses that build on previous interactions
  • Source integration for grounded, authoritative answers
  • Streaming responses for real-time conversation flow

Prerequisites

Before starting, ensure you have:
The Chat and Message APIs require an API key for authentication. If you haven’t set up authentication yet, follow the Authentication Tutorial.

Step 1: Creating a Chat Session

The Message API can either create a new chat session or continue an existing one. When you don’t provide a chat_id, it automatically creates a new session.

Step 2: Continuing the Conversation

Once you have a chat session, you can continue the conversation by including the chat_id from the previous response in your next message request. You don’t need to retrieve chat history to continue the conversation - the API maintains context automatically.

Step 3: Retrieving Chat History (Optional)

If you want to view the complete conversation history, you can retrieve it using the Chat API. This is useful for displaying conversation logs or analyzing chat patterns.

Complete Working Examples

Here are complete, runnable examples that demonstrate the full chat flow:

Error Handling Best Practices

When working with the Message API, implement proper error handling for common scenarios:

Authentication Errors

  • 401 Unauthorized: Token expired or invalid
  • 403 Forbidden: Insufficient permissions

Request Errors

  • 400 Bad Request: Invalid request parameters
  • 422 Unprocessable Entity: Validation errors

Rate Limiting

  • 429 Too Many Requests: Implement backoff strategies

Network Issues

  • Connection timeouts: Implement retry logic
  • Network failures: Handle gracefully with user feedback

Advanced Features

Streaming Responses

Set stream: true in your request for real-time response streaming:

Publisher Filtering

Restrict responses to specific content publishers:

Response Customization

Control response length and source count:

Working Code Sample

View Complete Code

Clone or browse the complete working examples for all 6 languages (JavaScript, TypeScript, Python, PHP, Go, Java) with setup instructions.

Next Steps

Now that you understand the Message API, consider exploring:
  1. Authentication Tutorial - For detailed authentication setup
  2. Messages API - For additional API information
  3. Completions Tutorial - For stateless chat interactions
  4. Tool Use - For enhanced AI capabilities
The Message API provides a powerful foundation for building meaningful, contextual conversations that can help users explore deep questions about life, purpose, and human flourishing.