> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/chat-history-api.md).

# 聊天历史 API

{% hint style="danger" %}
此 API 仅适用于 Scale 方案及以上。
{% endhint %}

## 列出角色的会话

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/chatHistory/list`

列出该角色的所有会话。该 API 接受一个 limit 参数，可用于限制要获取的会话数量。如果该值设置为 "-1"，则返回所有会话。

#### 请求头

| 名称                                              | 类型  | 说明                                             |
| ----------------------------------------------- | --- | ---------------------------------------------- |
| CONVAI-API-KEY<mark style="color:红色;">\*</mark> | 字符串 | 每个用户提供的唯一 api-key。在登录 Convai 账户后，可在 Key 图标下找到。 |

#### 请求体

| 名称     | 类型  | 说明       |
| ------ | --- | -------- |
| charID | 字符串 | 你的角色 ID。 |
| limit  | 字符串 | 要返回的会话数量 |

{% tabs %}
{% tab title="200：OK 返回会话列表" %}

```json
[
    {
        "sessionID": "c1234567890aba1111222233334447862",
        "date": "25-09-2024",
        "time": "20:52:57",
        "is_added_to_memory": false
    },
    {
        "sessionID": "8c123456789011111222223333344455",
        "date": "25-09-2024",
        "time": "19:54:56",
        "is_added_to_memory": false
    }
]

```

{% endtab %}

{% tab title="401 API 密钥验证失败" %}

```json
{
    "ERROR": "提供的 API 密钥无效。"
}
```

{% endtab %}
{% endtabs %}

以下是一些示例代码，用于演示该端点的请求格式 -->

{% tabs %}
{% tab title="Python" %}
{% code overflow="wrap" %}

```python
import requests
import json

url = "https://api.convai.com/character/chatHistory/list"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Content-Type': 'application/json'
}

# 为 JSON 负载创建一个字典
payload = { 
    "charID": "<Your-Character-Id>",
    "limit": "-1"
}

# 将负载转换为 JSON
json_payload = json.dumps(payload)

response = requests.post(url, headers=headers, data=json_payload)

print(response.text)
```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}
{% code overflow="wrap" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/list" \
     -H "CONVAI-API-KEY: <Your-API-Key>" \\
     -H "Content-Type: application/json" \
     -d '{"charID": "<Your-Character-Id>", "limit": "-1"}'
```

{% endcode %}
{% endtab %}
{% endtabs %}

## 某个会话的聊天历史

<mark style="color:绿色;">`POST`</mark> `https://api.convai.com/character/chatHistory/details`

列出给定 session-id 的所有交互。输出中还包含一个布尔参数，用于说明用户输入是否为触发器。如果你在与角色交互时曾发送过触发器，则该值将设为 true。

#### 请求头

| 名称                                              | 类型  | 说明                                             |
| ----------------------------------------------- | --- | ---------------------------------------------- |
| CONVAI-API-KEY<mark style="color:红色;">\*</mark> | 字符串 | 每个用户提供的唯一 api-key。在登录 Convai 账户后，可在 Key 图标下找到。 |

#### 请求体

| 名称        | 类型  | 说明                  |
| --------- | --- | ------------------- |
| charID    | 字符串 | 你的角色 ID。            |
| sessionID | 字符串 | 你要获取其交互记录的角色的会话 ID。 |

{% tabs %}
{% tab title="200：OK 返回会话交互记录" %}

```json
[
    {
        "timestamp": "2024-09-25 19:54:59.124733",
        "interaction": [
            {
                "speaker": "User",
                "message": "你好"
            },
            {
                "speaker": "Character",
                "message": "你好，欢迎来到历史博物馆。我们这里有有史以来发现的最古老化石。你有兴趣参观一下吗？"
            }
         ],
        "is_trigger_input": false
    }
]
```

{% endtab %}

{% tab title="401 API 密钥验证失败" %}

```json
{
    "ERROR": "提供的 API 密钥无效。"
}
```

{% endtab %}
{% endtabs %}

以下是一些示例代码，用于演示该端点的请求格式 -->

{% tabs %}
{% tab title="Python" %}
{% code overflow="wrap" %}

```python
import requests
import json

url = "https://api.convai.com/character/chatHistory/details"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Content-Type': 'application/json'
}

# 为 JSON 负载创建一个字典
payload = { 
    "charID": "<Your-Character-Id>",
    "sessionID": "<Your-Session-ID>"
}

# 将负载转换为 JSON
json_payload = json.dumps(payload)

response = requests.post(url, headers=headers, data=json_payload)

print(response.text)
```

{% endcode %}
{% endtab %}

{% tab title="cURL" %}
{% code overflow="wrap" %}

```shell
curl -X POST "https://api.convai.com/character/chatHistory/list" \
     -H "CONVAI-API-KEY: <Your-API-Key>" \\
     -H "Content-Type: application/json" \
     -d '{"charID": "<Your-Character-Id>", "sessionID": "<Your-Session-ID>"}'
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/chat-history-api.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.
