API Documentation for Frontend Integration

This document provides a comprehensive guide to the MNotify-Copianto API, including authentication, endpoints, request/response formats, and WebSocket integration. The API provides functionalities for chat interactions, content management, vector database training, and conversation history.

Base URL

Authentication

All API requests require authentication using an API key.

API Key Authentication

Authentication Errors

WebSocket Chat Integration

Connection

Connection Lifecycle

  1. Connect: Open a WebSocket connection to the endpoint

  2. Handshake:

  3. Sending Messages:

    {
      "type": "message",
      "message": "Your question here",
      "client_id": "optional-client-id",
      "chat_group_id": "optional-group-id"
    }

    With Category Filtering:

    {
      "type": "message",
      "message": "What policies do you know?",
      "category_ids": ["Policy", "Regulation"],  // Optional: filter by category names
      "client_id": "optional-client-id",
      "chat_group_id": "optional-group-id"
    }
  4. Receiving Streaming Responses:

  5. Handling Connection Issues:

WebSocket Message Types

Type Description Direction
connection_established Initial connection confirmation Server → Client
chat_group_assigned Assigns conversation grouping Server → Client
message User question or bot response Bidirectional
token Streaming partial response Server → Client
streaming_complete Indicates streaming is complete Server → Client
typing Bot typing indicator Server → Client
ping Connection heartbeat Server → Client
message_received Confirms receipt of user message Server → Client
error Error information Server → Client
history_request Request for conversation history Client → Server
chat_history Conversation history data Server → Client
message_delivered Confirms final message delivery Server → Client

RESTful API Endpoints

Category Management

List Categories

GET /api/chat/categories/

Response: List of categories

[
  {
    "id": 1,
    "name": "Tax Policy",
    "description": "Tax-related policies and regulations",
    "created_at": "2025-06-07T10:22:23.016Z",
    "updated_at": "2025-06-07T10:22:23.016Z"
  }
]

Create Category

POST /api/chat/categories/

Request Body:

{
  "name": "New Category",
  "description": "Description of the category"  // Optional
}

Response: Created category object

Update Category

PUT/PATCH /api/chat/categories/{id}/

Request Body:

{
  "name": "Updated Category Name",
  "description": "Updated description"
}

Response: Updated category object

Delete Category

DELETE /api/chat/categories/{id}/

Response: 204 No Content

File Management

List Files

GET /api/chat/files/

Query Parameters:

Response:

[
  {
    "id": 1,
    "name": "File name",
    "file": "/media/path/to/file.pdf",
    "is_trained": false,
    "show_as_reference": true,
    "category": 1,
    "category_name": "Category Name",
    "created_at": "2025-06-07T10:22:23.016Z",
    "updated_at": "2025-06-07T10:22:23.016Z"
  }
]

Upload File

POST /api/chat/files/

Content-Type: multipart/form-data

Form Fields:

Response: Created file object

Batch Upload Files

POST /api/chat/files/batch/

Content-Type: multipart/form-data

Form Fields:

Response: Array of created file objects

Delete File

DELETE /api/chat/files/{id}/

Response: 204 No Content

Text Content Management

List Texts

GET /api/chat/texts/

Query Parameters:

Response: List of text objects

Create Text

POST /api/chat/texts/

Request Body:

{
  "text": "Text content",
  "name": "Text title",
  "category": 1,
  "show_as_reference": true
}

Response: Created text object

Batch Create Texts

POST /api/chat/texts/batch/

Request Body:

{
  "texts": ["Text content 1", "Text content 2"],
  "category": 1,
  "show_as_reference": true
}

Response: Array of created text objects

Delete Text

DELETE /api/chat/texts/{id}/

Response: 204 No Content

Website Management

List Websites

GET /api/chat/websites/

Query Parameters:

Response: List of website objects

Add Website

POST /api/chat/websites/

Request Body:

{
  "url": "https://example.com",
  "category": 1,
  "show_as_reference": true,
  "follow_links": false,
  "is_js_heavy": false,
  "wait_for_page_load": 15,
  "wait_for_id": "",
  "wait_for_class": "",
  "ignore_urls": ""
}

Response: Created website object

Batch Add Websites

POST /api/chat/websites/batch/

Request Body:

{
  "urls": ["https://example1.com", "https://example2.com"],
  "category": 1,
  "show_as_reference": true,
  "follow_links": false,
  "is_js_heavy": false,
  "wait_for_page_load": 15,
  "wait_for_id": "",
  "wait_for_class": "",
  "ignore_urls": ""
}

Response: Array of created website objects

Delete Website

DELETE /api/chat/websites/{id}/

Response: 204 No Content

Training and Bot Management

Train Bot

GET /api/chat/train/

Response:

{
  "task_id": "celery-task-id",
  "status": "Training started"
}

Get Bot Status

GET /api/chat/status/

Response:

{
  "id": 1,
  "index_name": "MN20250604123456",
  "is_trained": true,
  "new_data_added": false,
  "last_trained_at": "2025-06-07T10:22:23.016Z",
  "file_count": 10,
  "text_count": 5,
  "website_count": 3,
  "untrained_count": 0,
  "untrained_details": {
    "files": [],
    "texts": [],
    "websites": [],
    "not_ready_websites": [],
    "count": {
      "files": 0,
      "texts": 0,
      "websites": 0,
      "not_ready_websites": 0,
      "total": 0
    }
  },
  "created_at": "2025-06-07T10:22:23.016Z",
  "updated_at": "2025-06-07T10:22:23.016Z"
}

Reset Bot

POST /api/chat/reset/

Request Body:

{
  "delete_weaviate_index": false
}

Response:

{
  "message": "Bot training status reset successfully. All data sources marked for retraining."
}

Check Task Status

GET /api/chat/task-status/{task_id}/

Response:

{
  "task_id": "task-id",
  "status": "PENDING|IN_PROGRESS|COMPLETED|FAILED",
  "result": {},
  "task_type": "TRAINING",
  "is_active": true
}

Conversation Management

Get Conversation History

GET /api/chat/conversations/

Query Parameters:

Response:

{
  "conversations": [
    {
      "id": 1,
      "conversation_id": "uuid-string",
      "client_id": "client-id",
      "user_input": "Question text",
      "chatbot_response": "Answer text",
      "created_at": "2025-06-07T10:22:23.016Z",
      "full_response": {},
      "similar_docs": [],
      "like_or_dislike": null
    }
  ]
}

Delete Conversation

DELETE /api/chat/conversations/delete/{id}/

Response: 204 No Content

Bot Settings

Get Bot Settings

GET /api/chat/settings/

Note: This endpoint now only returns user-configurable fields.

Response:

{
  "id": 1,
  "name": "Ghana Policy Assistant",
  "extra_prompt": "Always cite sources with reference numbers",
  "created_at": "2025-06-07T10:22:23.016Z",
  "updated_at": "2025-06-07T10:22:23.016Z"
}

Update Bot Settings

PUT /api/chat/settings/

Note: Only name and extra_prompt fields can be updated.

Request Body:

{
  "name": "Ghana Policy Bot",
  "extra_prompt": "Focus on Ghana-specific regulations and always mention the source"
}

Response: Updated settings object (only exposed fields)

Reset Bot Settings

POST /api/chat/settings/

Response: Default settings object

Content Extraction

Extract Content

POST /api/chat/extract/content/

Request Body:

{
  "batch_size": 10,
  "force": false
}

Response:

{
  "message": "Content extraction started for 5 files",
  "task_id": "uuid-string",
  "total_files": 5,
  "batch_size": 10,
  "status": "PENDING"
}

Get Extraction Status

GET /api/chat/extract/status/
GET /api/chat/extract/status/?task_id=<task_id>

Response (without task_id):

{
  "total_files": 10,
  "extracted_files": 8,
  "ready_files": 8,
  "pending_files": 2,
  "extraction_progress": 80.0,
  "active_tasks": [
    {
      "task_id": "task-id",
      "status": "IN_PROGRESS",
      "created_at": "2025-06-07T10:22:23.016Z",
      "result": {}
    }
  ],
  "active_task_count": 1
}

Import Functions

Import Folder

POST /api/chat/import/folder/

Content-Type: multipart/form-data

Form Fields:

Response:

{
  "message": "Folder import started",
  "task_id": "uuid-string",
  "status": "PENDING"
}

Get Import Status

GET /api/chat/import/status/
GET /api/chat/import/status/?task_id=<task_id>

Response: Similar to extraction status

Category-Based Question Filtering

The API supports filtering bot responses based on specific content categories. This allows you to restrict the bot's knowledge base to specific topics or domains.

How Category Filtering Works

WebSocket Category Filtering

Include category_ids array with category names in your message:

{
  "type": "message",
  "message": "Tell me about tax policies",
  "category_ids": ["Tax", "Finance"]  // Filter by category names
}

Category Validation

If invalid categories are provided, you'll receive a warning:

{
  "type": "warning",
  "message": "Invalid category IDs: ['NonExistent']. Using valid categories only."
}

Client History Management

New endpoints for managing conversation history when WebSocket clients disconnect and need to restore their state.

Get Client Chat History

GET /api/chat/client-history/

Query Parameters:

Response:

{
  "client_id": "abc123",
  "total_count": 25,
  "conversations": [
    {
      "id": 1,
      "conversation_id": "550e8400-e29b-41d4-a716-446655440000",
      "client_id": "abc123",
      "user_input": "What policies do you know?",
      "chatbot_response": "I can help you with various policies...",
      "created_at": "2025-01-15T10:30:00Z",
      "full_response": {},
      "similar_docs": [],
      "like_or_dislike": null
    }
  ],
  "conversations_by_group": {
    "group456": [/* conversations */]
  },
  "filters_applied": {
    "chat_group_id": null,
    "date_from": null,
    "date_to": null,
    "limit": 50
  }
}

Get Client Chat Groups

GET /api/chat/client-groups/

Query Parameters:

Response:

{
  "client_id": "abc123",
  "chat_groups": [
    {
      "chat_group_id": "group456",
      "conversation_count": 15,
      "last_message_at": "2025-01-15T10:30:00Z",
      "first_message_at": "2025-01-14T08:00:00Z"
    }
  ],
  "total_groups": 2
}

Get Specific Conversation

GET /api/chat/conversation/{conversation_id}/

Path Parameters:

Response:

{
  "conversation": {
    "id": 1,
    "conversation_id": "550e8400-e29b-41d4-a716-446655440000",
    "client_id": "abc123",
    "user_input": "What policies do you know?",
    "chatbot_response": "I can help you with various policies...",
    "created_at": "2025-01-15T10:30:00Z",
    "full_response": {},
    "similar_docs": [],
    "like_or_dislike": null
  },
  "metadata": {
    "client_id": "abc123",
    "chat_group_id": "group456",
    "created_at": "2025-01-15T10:30:00Z",
    "has_response": true,
    "has_references": true,
    "user_feedback": null,
    "related_conversations": 24
  }
}

Error Handling

All API endpoints follow a consistent error response format:

Common HTTP Status Codes

Error Response Format

{
  "detail": "Error message"
}

or

{
  "field_name": ["Error message"]
}

Recent API Changes

This section documents recent changes to the API that may require frontend updates.

January 2025 Updates

1. Category-Based Filtering (New Feature)

2. Client History Endpoints (New)

3. Bot Settings API Changes (Breaking)

4. Bot Name Customization (Enhancement)

5. Category Name Changes

Migration Guide

For WebSocket Implementations:

// Old way (if you were using categories)
{
  "type": "message",
  "message": "What policies?",
  "category_ids": [1, 2]  // Numeric IDs
}

// New way
{
  "type": "message", 
  "message": "What policies?",
  "category_ids": ["Tax", "Finance"]  // Category names
}

For Session Recovery:

// Use new client history endpoint after reconnection
const response = await fetch(`/api/chat/client-history/?client_id=${clientId}&limit=20`, {
  headers: { 'X-API-Key': apiKey }
});

const { conversations } = await response.json();
// Restore UI with conversation history

For additional support or questions, please contact the backend development team.