Chat AI API

The Chat API allows for full integration with CapData's AI assistant. You can manage folders, conversations, custom GPTs, and send messages to get responses from the model.

Authentication

Use the unified X-CapData-Token header. The value can be the API Key of an Owner, an Agency, or an Agent's token.

Header Example
X-CapData-Token: your_api_key_or_agent_token

Folder Management

Organize your conversations into folders for better management.

GET /api/chat/folders

Gets a list of all chat folders belonging to the authenticated actor.

Successful Response (200 OK)

application/json
{
    "folders": [
        {
            "id": 1,
            "name": "Personal Trips",
            "conversation_count": 5
        },
        {
            "id": 2,
            "name": "Client Inquiries",
            "conversation_count": 12
        }
    ]
}

POST /api/chat/folders

Creates a new chat folder.

Request Body

application/json
{
    "name": "New Projects"
}

Successful Response (201 Created)

Returns the newly created folder object.

application/json
{
    "folder": {
        "id": 3,
        "name": "New Projects",
        "conversation_count": 0
    }
}

PUT /api/chat/folders/<folder_id>

Renames an existing folder.

Request Body

application/json
{
    "new_name": "Archived Projects"
}

Successful Response (200 OK)

Returns the updated folder object.

DELETE /api/chat/folders/<folder_id>

Deletes a folder. The conversations it contained are not deleted; they simply become unassigned from any folder.

Successful Response (200 OK)

application/json
{
    "message": "Folder ID 3 deleted successfully."
}

GET /api/chat/conversations/unfoldered

Gets a list of conversations that are not assigned to any folder.

Successful Response (200 OK)

Returns an array of conversation objects.

GET /api/chat/folders/<folder_id>/conversations

Gets a list of all conversations within a specific folder.

Successful Response (200 OK)

Returns an array of conversation objects.

PUT /api/chat/conversations/<conversation_id>/move

Moves a conversation to a different folder. To remove a conversation from its folder, use null as the value.

Request Body

application/json
{
    "folder_id": 2
}

Successful Response (200 OK)

Returns the updated conversation object.


Conversation Management

Endpoints to create, query, and modify conversations.

POST /api/chat/conversations

Creates a new conversation. Optionally, it can be assigned to a folder and/or a custom GPT upon creation. If you provide chat_gpt_id, the ID must correspond to an accessible and active GPT for the authenticated actor.

Request Body

application/json
{
    "title": "Planning a trip to Japan",
    "folder_id": 1,
    "chat_gpt_id": 7
}

Successful Response (201 Created)

Returns the newly created conversation object.

GET /api/chat/conversations/<conversation_id>/messages

Retrieves the complete message history of a conversation.

Successful Response (200 OK)

application/json
{
    "messages": [
        {
            "role": "user",
            "content": "What are the best months to visit Kyoto?",
            "timestamp": "..."
        },
        {
            "role": "assistant",
            "content": "The best months to visit Kyoto are...",
            "timestamp": "..."
        }
    ]
}

POST /api/chat/conversations/<conversation_id>/messages

Sends a message to an existing conversation and gets a response from the AI assistant. This action consumes tokens.

Request Body

application/json
{
    "message": "What are the best months to visit Kyoto?"
}

Successful Response (200 OK)

application/json
{
    "reply": "The best months to visit Kyoto are spring (March-May) for the cherry blossoms, and autumn (October-November) for the vibrant colors...",
    "tokens_remaining": 98
}

POST /api/chat/conversations/<conversation_id>/upload

Uploads a document (.pdf, .txt, .md) to a conversation so its content can be used as context in subsequent messages.

Request Body

The request must be of type multipart/form-data. The file must be sent under the key document.

Example (cURL pseudocode)
curl -X POST "https://capdata.es/api/chat/conversations/42/upload" \
     -H "X-CapData-Token: your_api_key_or_token" \
     -F "document=@/path/to/your/hotel_booking.pdf"

Successful Response (200 OK)

application/json
{
    "success": true,
    "message": "File 'hotel_booking.pdf' parsed and added to the context of this conversation. You can now ask questions about it."
}

Custom GPT Management

Create, manage, and import specialized AI assistants with their own knowledge base.

GET /api/chat/gpts

Gets a list of all GPTs accessible to the actor (including those imported from global ones). The response includes metadata such as knowledge_file_count, is_global_origin, and display_type.

Successful Response (200 OK)

application/json
{
  "gpts": [
    {
      "id": 7,
      "name": "Travel Assistant",
      "description": "Specialized in routes and airlines",
      "is_active": true,
      "knowledge_file_count": 3,
      "is_global_origin": false,
      "display_type": "Client"
    }
  ]
}

GET /api/chat/gpts/available-global

Lists the available global GPTs that can be imported by the client.

Successful Response (200 OK)

application/json
{
  "available_global_gpts": [
    {
      "id": 3,
      "name": "Hotel Template",
      "description": "Base for hotel customer service",
      "is_global": true,
      "can_import": true,
      "knowledge_file_count": 5
    }
  ]
}

POST /api/chat/gpts/import/<global_gpt_id>

Imports a global GPT for the authenticated client.

Successful Response (201 Created)

application/json
{
  "message": "GPT 'Hotel Template' imported successfully.",
  "imported_gpt": {
    "id": 12,
    "name": "Hotel Template",
    "is_active": true
  }
}

POST /api/chat/gpts

Creates a new custom GPT with its knowledge files.

Request Body

The request must be multipart/form-data with the following fields:

Example (cURL pseudocode)
curl -X POST "https://capdata.es/api/chat/gpts" \
  -H "X-CapData-Token: your_api_key_or_token" \
  -F "name=Sales Assistant" \
  -F "system_prompt=Respond like an expert sales advisor" \
  -F "description=Trained with product PDFs" \
  -F "is_active=true" \
  -F "knowledge_files=@/path/to/catalog.pdf" \
  -F "knowledge_files=@/path/to/procedures.txt"

Successful Response (201 Created)

application/json
{
  "gpt": {
    "id": 21,
    "name": "Sales Assistant",
    "description": "Trained with product PDFs",
    "is_active": true,
    "knowledge_file_count": 2
  }
}

Common Errors and Token Consumption

Token Consumption

402 Payment Required (insufficient balance)

When the actor does not have enough tokens to complete the operation, the server returns 402 with balance information.

application/json
{
  "error": "Insufficient tokens to send the message.",
  "tokens_remaining": 0
}

404 / 403 (resource not found or no permissions)

Example 404 (JSON)
{
  "error": "Conversation not found."
}
Example 403 (JSON)
{
  "error": "You do not have permission for this action."
}

415 Unsupported Media Type (file upload)

In POST /api/chat/conversations/<id>/upload, only .pdf, .txt, and .md extensions are accepted. If you send another file type, you will receive a 415 error.

application/json
{
  "error": "File type not allowed. Please upload only .pdf, .txt, .md."
}

503 Service Unavailable (AI assistant failure)

If the AI engine cannot generate a response (e.g., a temporary provider error), the API will return 503 for POST /api/chat/conversations/<id>/messages.

application/json
{
  "error": "AI assistant error: service temporarily unavailable."
}
Recommendation: explicitly handle the 402, 403, 404, 415, and 503 statuses in your client to provide clear messages to the end-user.