> 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

使用流式 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 账户后，可在钥匙图标下找到。 |

#### 请求体

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

{% tabs %}
{% tab title="200：成功，返回多个 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"
}

# 创建一个会话以管理 cookies 和保持长连接
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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
