> 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

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

***

## 请求体

| 名称                                              | 类型  | 说明                                                                                                                                                                                           |
| ----------------------------------------------- | --- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **transcript**<mark style="color:红色;">\*</mark> | 字符串 | 要转换为音频的文本。                                                                                                                                                                                   |
| **voice**<mark style="color:红色;">\*</mark>      | 字符串 | <p>指定音频响应所使用的语音类型。可以使用 <a href="/pages/ca26d73f8d2e97a80021565ab030a9f12904f637">语音列表 API</a>，从所需语音的 <code>voice\_value</code> 属性中检索。<br><br><strong>注意：</strong> 独立的 TTS API 端点不支持实时语音。</p> |
| filename                                        | 字符串 | 音频文件的名称。                                                                                                                                                                                     |
| encoding                                        | 字符串 | <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': '<Your Convai API Key>',
  '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 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/text-to-speech-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.
