> 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/backstory-api.md).

# 背景故事 API

{% hint style="danger" %}
此 API 仅适用于专业版及以上套餐。
{% endhint %}

## 为角色生成背景故事

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

Generate Backstory API 采用服务器发送事件（SSE）实现。给定关于角色及其名字的一些初始描述，该 API 可以生成该角色的描述。

#### 请求头

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

#### 请求体

| 名称        | 类型  | 说明             |
| --------- | --- | -------------- |
| inputText | 字符串 | 角色的高层描述。       |
| charName  | 字符串 | 正在为其生成描述的角色名称。 |

{% tabs %}
{% tab title="200：OK 返回多个 SSE 事件。" %}

```json
# 输出格式：
# 响应将以服务器发送事件（SSE）的形式流式传输。
# 每个事件都会有一个 'data' 字段，其中包含一段背景故事字符串。
# 单个事件示例：

data: 你走下火车，迎接你的是熙熙攘攘的城市声响。

# 将接收到多个事件，每个事件包含背景故事的一部分。
# 可以通过拼接所有事件中的 'data' 来组装完整的背景故事。
```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

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

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

```python
import json
from sseclient import SSEClient
import requests

url = "https://api.convai.com/character/generate-backstory"

headers = { 
    'CONVAI-API-KEY': '<Your-API-Key>',
    'Accept': 'text/event-stream'
}

form_data = { 
    "inputText": "纽约市的一名律师。进取。固执。",
    "charName": "Mindy"
}

# 创建一个会话以管理 cookie 和保持长连接
session = requests.Session()

# 使用 form-data 发送 POST 请求并流式接收响应
response = session.post(url, headers=headers, data=form_data, stream=True)

# 从响应创建一个 SSE 客户端
client = SSEClient(response)

# 处理这些事件
full_response = ""
for event in client.events():
    if event.data:
        full_response += event.data

print(full_response)
```

{% endcode %}
{% endtab %}

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

```shell
curl -X POST 'https://api.convai.com/character/generate-backstory' \\
-H 'CONVAI-API-KEY: <Your-API-Key>' \\
-H 'Accept: text/event-stream' \\
-d 'inputText=纽约市的一名律师。进取。固执。' \\
-d 'charName=Mindy'
```

{% 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/backstory-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.
