# Chat

The chat feature is organized by chat threads and messages in the chat threads.

## Endpoints

## Get Chat Threads

<mark style="color:blue;">`GET`</mark> `https://cubicl.io/api/v1/chat/threads`

{% tabs %}
{% tab title="200: OK Chat thread list" %}

```javascript
ChatThread[]
```

{% endtab %}
{% endtabs %}

## Get Messages

<mark style="color:blue;">`GET`</mark> `https://cubicl.io/api/v1/chat/threads/:threadId/messages`

Gets messages in a chat thread.

#### Path Parameters

| Name                                       | Type   | Description                                                            |
| ------------------------------------------ | ------ | ---------------------------------------------------------------------- |
| threadId<mark style="color:red;">\*</mark> | string | Chat thread id                                                         |
| after                                      | string | Message id. Messages sent after this message will be returned.         |
| before                                     | string | Message id. Latest messages sent before this message will be returned. |
| search                                     | string | A text to be searched in message content.                              |
| limit                                      | number |                                                                        |

{% tabs %}
{% tab title="200: OK Message list" %}

```javascript
ChatMessage[]
```

{% endtab %}
{% endtabs %}

## Send Message

<mark style="color:green;">`POST`</mark> `https://cubicl.io/api/v1/chat/messages`

Messages are sent to threads or users. When there is not a thread between two users yet, you need to set `to` parameter with the receiver user's id. After the first message, a thread will be created. You should set `thread` parameter for existing threads.

Either `thread` or `to` parameter must be set.

Either `content` or `files` parameter must be set.

#### Request Body

| Name    | Type      | Description                                          |
| ------- | --------- | ---------------------------------------------------- |
| thread  | string    | Thread id                                            |
| to      | string    | User id                                              |
| content | string    | Message content                                      |
| files   | string\[] | File id list                                         |
| replyTo | string    | Message id. If message is a reply to another message |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
    //  Newly created message ID
    message: string,
    // This exists if a new thread is created with this request.
    // New thread is created when a user sends a message to another user
    // for the first time.
    thread?: ChatThread
}
```

{% endtab %}
{% endtabs %}

## Data

#### Chat Threads

Chat messages are sent in a thread. Threads have 3 types:

1. personal: Between 2 users. Created when a user sends a message to another user.
2. group: Created for each project. All project members are members of these threads.
3. custom: Created among a group of users by users themselves. This is a private chat group.

```typescript
type ChatThread = {
    _id: string;
    type: 'personal' | 'group' | 'custom';
    // Project id. Exists when type is 'group'
    group?: string;
    // Name of the thread. Exists when type is 'custom'
    name?: string;
    // Ids of users in the chat thread. Exists when type is 'personal' or 'custom'
    users: string[];
    // Date of last activity (message) in the thread
    activity: number;
    // Date at which current user viewed the thread. 0 if not accessed before.
    access: number;
    lastMessage: ChatMessage | null;
    // Number of new messages since last access.
    newActivityCount: number;
    createdAt: number;
}
```

#### Chat Messages

```typescript
type ChatMessage = {
    _id: string;
    // Thread id
    thread: string;
    // Id of the user who sent the message
    from: string;
    content: string;
    // File attachments in the message. Check Files page for data format.
    files: File[];
    // Ids of the users who liked the message
    likes: string[];
    dislikes: string[];
    // If a message is a reply to another message, this will be the referenced
    // message id
    replyTo: string | null;
    createdAt: number;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cubicl.io/api-integration/chat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
