> 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/text-to-speech-api.md).

# 文本转语音 API

本页解释了如何使用独立的文本转语音（TTS）API，通过从可用语音列表中选择的语音，基于给定转录生成音频。

{% hint style="danger" %}
此 API 仅适用于企业版方案。
{% endhint %}

## 从文本生成音频

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

***

## **标头**

| **CONVAI-API\_KEY**<mark style="color:红色;">**\***</mark> | 字符串 | 您的 Convai API 密钥。 |
| -------------------------------------------------------- | --- | ----------------- |
| **Content-Type**<mark style="color:红色;">**\***</mark>    | 字符串 | application/json  |

***

## 请求体

| 名称                                        | 类型  | 说明                                                                                                                                                                                                                                |
| ----------------------------------------- | --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **文本内容**<mark style="color:红色;">\*</mark> | 字符串 | 要转换为音频的文本。                                                                                                                                                                                                                        |
| **语音**<mark style="color:红色;">\*</mark>   | 字符串 | <p>指定音频响应所使用的语音类型。可通过 <a href="/api-docs/zh/api-can-kao/core-api-reference/character-crafting-apis/voice-list-api.md">语音列表 API</a>，从所需语音的 <code>voice\_value</code> 属性中获取。<br><br><strong>注意：</strong> 独立的 TTS API 端点不支持实时语音。</p> |
| 文件名                                       | 字符串 | 音频文件的名称。                                                                                                                                                                                                                          |
| 编码格式                                      | 字符串 | <p>这是音频文件的格式。我们目前支持用于 STT（语音转文本）音频的 WAV 和 MP3 格式。默认设置为 WAV。<br><br><strong>注意：</strong> 并非所有语音都支持 MP3。</p>                                                                                                                        |

***

## 响应

如果 API 调用成功，它将返回生成的音频文件作为响应。

***

## 示例代码片段

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
import requests
import json

url = "https://api.convai.com/tts/"

payload = json.dumps({
  "transcript": "在宁静的清晨，天空逐渐染上粉色和橙色时，她沿着两旁开满鲜花的小径行走，感受着脸上的凉风。终于到了晚餐时间。",
  "voice": "fable_OpenAi_c4d30a32-bcaf-4048-b7b0-0f2bb293205a",
  "encoding": "wav"
})

headers = {
  'CONVAI-API-KEY': '<您的 Convai API 密钥>',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

if response.status_code == 200:
    with open('audio.wav','wb') as f:
      f.write(response.content)
else:
    print(response.text)
```

{% 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/text-to-speech-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.
