> 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

与会话或角色聊天历史交互所需的所有相关 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 密钥。登录 Convai 账户后，可在密钥图标下找到。 |

#### 请求正文

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

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

```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": "Invalid API key provided."
}
```

{% 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"
}

# 将 payload 转换为 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 密钥。登录 Convai 账户后，可在密钥图标下找到。 |

#### 请求正文

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

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

```json
[
    {
        "timestamp": "2024-09-25 19:54:59.124733",
        "interaction": [
            {
                "speaker": "User",
                "message": "Hi there"
            },
            {
                "speaker": "Character",
                "message": "Hi there, Welcome to the History Museum. We have the most ancient fossil ever discovered. Would be interesting in taking a tour?"
            }
         ],
        "is_trigger_input": false
    }
]
```

{% endtab %}

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

```json
{
    "ERROR": "Invalid API key provided."
}
```

{% 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>"
}

# 将 payload 转换为 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/chat-history-api.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
