curl --request POST 'https://api.voicecheap.ai/v1/actions/tts_generate' \
--header "x-api-key: $VOICECHEAP_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"requestId":"284a861a-4da8-4d67-ad48-8123e76c8e91","text":"Hello world.","voice":"933563129e564b19a115bedd57b7406a","inputType":"text","instructionPreset":"narrator","instructions":"Speak clearly and leave a short pause at the end.","speed":1,"sourceFileName":"launch-script.txt"}'
const input = {
requestId: '284a861a-4da8-4d67-ad48-8123e76c8e91',
text: 'Hello world.',
voice: '933563129e564b19a115bedd57b7406a',
inputType: 'text',
instructionPreset: 'narrator',
instructions: 'Speak clearly and leave a short pause at the end.',
speed: 1,
sourceFileName: 'launch-script.txt',
};
const response = await fetch('https://api.voicecheap.ai/v1/actions/tts_generate', {
method: 'POST',
headers: {
'x-api-key': process.env.VOICECHEAP_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
});
const body = await response.json();
if (!response.ok) throw new Error(body.code + ': ' + body.message);
console.log(body);
import os
import requests
response = requests.post(
"https://api.voicecheap.ai/v1/actions/tts_generate",
headers={"x-api-key": os.environ["VOICECHEAP_API_KEY"]},
json={"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91", "text": "Hello world.", "voice": "933563129e564b19a115bedd57b7406a", "inputType": "text", "instructionPreset": "narrator", "instructions": "Speak clearly and leave a short pause at the end.", "speed": 1, "sourceFileName": "launch-script.txt"},
timeout=120,
)
body = response.json()
if not response.ok:
raise RuntimeError(f"{body.get('code')}: {body.get('message')}")
print(body)
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"status": "running",
"pollAction": "action_request_get",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"historyId": "722c80c6412edd4c3dcd625061e1c1feafbb0d54274d95fd8b5ca047f8cdd23d",
"status": "completed",
"characterCount": 12,
"previewEndIndex": null,
"downloadUrl": "https://files.example.com/launch-script-speech.mp3"
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "running",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "completed",
"resultStored": false,
"message": "The action completed. Retrieve its output from the corresponding history or resource action."
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
文本转语音
生成语音
使用音色库音色或当前账户拥有的自定义音色,将文本转换为语音。采用与应用相同的字符额度和套餐限制。返回历史记录 ID 和临时音频下载 URL。请复用 requestId,避免第二次生成。
POST
/
v1
/
actions
/
tts_generate
curl --request POST 'https://api.voicecheap.ai/v1/actions/tts_generate' \
--header "x-api-key: $VOICECHEAP_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"requestId":"284a861a-4da8-4d67-ad48-8123e76c8e91","text":"Hello world.","voice":"933563129e564b19a115bedd57b7406a","inputType":"text","instructionPreset":"narrator","instructions":"Speak clearly and leave a short pause at the end.","speed":1,"sourceFileName":"launch-script.txt"}'
const input = {
requestId: '284a861a-4da8-4d67-ad48-8123e76c8e91',
text: 'Hello world.',
voice: '933563129e564b19a115bedd57b7406a',
inputType: 'text',
instructionPreset: 'narrator',
instructions: 'Speak clearly and leave a short pause at the end.',
speed: 1,
sourceFileName: 'launch-script.txt',
};
const response = await fetch('https://api.voicecheap.ai/v1/actions/tts_generate', {
method: 'POST',
headers: {
'x-api-key': process.env.VOICECHEAP_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
});
const body = await response.json();
if (!response.ok) throw new Error(body.code + ': ' + body.message);
console.log(body);
import os
import requests
response = requests.post(
"https://api.voicecheap.ai/v1/actions/tts_generate",
headers={"x-api-key": os.environ["VOICECHEAP_API_KEY"]},
json={"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91", "text": "Hello world.", "voice": "933563129e564b19a115bedd57b7406a", "inputType": "text", "instructionPreset": "narrator", "instructions": "Speak clearly and leave a short pause at the end.", "speed": 1, "sourceFileName": "launch-script.txt"},
timeout=120,
)
body = response.json()
if not response.ok:
raise RuntimeError(f"{body.get('code')}: {body.get('message')}")
print(body)
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"status": "running",
"pollAction": "action_request_get",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"historyId": "722c80c6412edd4c3dcd625061e1c1feafbb0d54274d95fd8b5ca047f8cdd23d",
"status": "completed",
"characterCount": 12,
"previewEndIndex": null,
"downloadUrl": "https://files.example.com/launch-script-speech.mp3"
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "running",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "completed",
"resultStored": false,
"message": "The action completed. Retrieve its output from the corresponding history or resource action."
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
使用音色库音色或当前账户拥有的自定义音色,将文本转换为语音。采用与应用相同的字符额度和套餐限制。返回历史记录 ID 和临时音频下载 URL。请复用 requestId,避免第二次生成。
所需权限:
POST https://api.voicecheap.ai/v1/actions/tts_generate
身份验证和权限
string
必填
在此请求头中发送 VoiceCheap API 密钥。也支持通过 Authorization: Bearer 发送同一 API 密钥。OAuth 访问令牌仅适用于 MCP,此 REST 端点不接受 OAuth
访问令牌。
tts:write.
需要有效的付费订阅。权限不会改变团队角色、资源归属、媒体额度或套餐限制。
参数
string
在传输层可选:如果省略,服务器会在验证前注入标识符。请自行提供稳定的 UUID v4,以便在响应丢失后安全恢复。格式: “uuid”. 匹配模式:
^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$.string
必填
文本值。最小长度: 1. 最大长度: 128000.
string
必填
来自 voices_list 的音色选择 ID,包括已保存自定义音色的 clone: ID。最小长度: 1. 最大长度: 255.
string
文本值。允许的值: “text”, “pdf”. 默认值: “text”.
string
文本值。允许的值: “default”, “cheerful”, “calm”, “narrator”, “dramatic”, “whisper”, “energetic”. 默认值: “default”.
string
文本值。最大长度: 300.
number
数值。最小值: 0.5. 最大值: 2. 默认值: 1.
string
文本值。最大长度: 255.
完整输入架构
完整输入架构
以下 JSON Schema 保留了发现接口公开的确切字段名、嵌套类型、默认值、范围和可接受值。其他跨字段规则列于操作说明中。
{
"type": "object",
"properties": {
"requestId": {
"description": "UUID v4 for this action. Reuse the same ID with identical arguments on retries.",
"type": "string",
"format": "uuid",
"pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
},
"text": {
"type": "string",
"minLength": 1,
"maxLength": 128000
},
"voice": {
"type": "string",
"minLength": 1,
"maxLength": 255,
"description": "A voice selection ID from voices_list, including clone: IDs for saved custom voices."
},
"inputType": {
"default": "text",
"type": "string",
"enum": ["text", "pdf"]
},
"instructionPreset": {
"default": "default",
"type": "string",
"enum": ["default", "cheerful", "calm", "narrator", "dramatic", "whisper", "energetic"]
},
"instructions": {
"type": "string",
"maxLength": 300
},
"speed": {
"default": 1,
"type": "number",
"minimum": 0.5,
"maximum": 2
},
"sourceFileName": {
"type": "string",
"maxLength": 255
}
},
"required": ["text", "voice"],
"additionalProperties": false
}
幂等性
为每次修改操作在 requestId 或 Idempotency-Key HTTP 请求头中提供 UUID v4。网络故障后,请使用相同标识符和完全相同的参数重试。如果同时提供这两个值,它们必须一致。重放已保存的请求不会重复执行操作,也不会再次扣除月度调用次数。失败或中断的请求不会自动重新执行。 对于每次修改操作,如果重试时发现同一请求仍在运行,即使首次执行通常为同步操作,也可能返回 HTTP 202。如果执行已完成但 resultStored 为 false,重试会返回 HTTP 200 和已保存的请求状态,而非原始结果。请通过对应领域的历史记录、结果或下载操作获取输出。请求示例
请将示例资源标识符和媒体键替换为账户返回的值。重试同一次修改操作时,请保持请求标识符不变。curl --request POST 'https://api.voicecheap.ai/v1/actions/tts_generate' \
--header "x-api-key: $VOICECHEAP_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"requestId":"284a861a-4da8-4d67-ad48-8123e76c8e91","text":"Hello world.","voice":"933563129e564b19a115bedd57b7406a","inputType":"text","instructionPreset":"narrator","instructions":"Speak clearly and leave a short pause at the end.","speed":1,"sourceFileName":"launch-script.txt"}'
const input = {
requestId: '284a861a-4da8-4d67-ad48-8123e76c8e91',
text: 'Hello world.',
voice: '933563129e564b19a115bedd57b7406a',
inputType: 'text',
instructionPreset: 'narrator',
instructions: 'Speak clearly and leave a short pause at the end.',
speed: 1,
sourceFileName: 'launch-script.txt',
};
const response = await fetch('https://api.voicecheap.ai/v1/actions/tts_generate', {
method: 'POST',
headers: {
'x-api-key': process.env.VOICECHEAP_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify(input),
});
const body = await response.json();
if (!response.ok) throw new Error(body.code + ': ' + body.message);
console.log(body);
import os
import requests
response = requests.post(
"https://api.voicecheap.ai/v1/actions/tts_generate",
headers={"x-api-key": os.environ["VOICECHEAP_API_KEY"]},
json={"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91", "text": "Hello world.", "voice": "933563129e564b19a115bedd57b7406a", "inputType": "text", "instructionPreset": "narrator", "instructions": "Speak clearly and leave a short pause at the end.", "speed": 1, "sourceFileName": "launch-script.txt"},
timeout=120,
)
body = response.json()
if not response.ok:
raise RuntimeError(f"{body.get('code')}: {body.get('message')}")
print(body)
响应
此操作在运行期间返回 HTTP 202。请保存 requestId,并使用该标识符和原始操作名称轮询 action_request_get。轮询状态包括 running、completed、failed 和 interrupted。已完成的结果可能指向独立的媒体任务;请继续使用该操作对应的状态或输出操作。 已完成的语音生成元数据和临时 MP3 下载 URL。处理程序不返回音频字节。{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"status": "running",
"pollAction": "action_request_get",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"historyId": "722c80c6412edd4c3dcd625061e1c1feafbb0d54274d95fd8b5ca047f8cdd23d",
"status": "completed",
"characterCount": 12,
"previewEndIndex": null,
"downloadUrl": "https://files.example.com/launch-script-speech.mp3"
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "running",
"pollAfterSeconds": 5
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"status": "completed",
"resultStored": false,
"message": "The action completed. Retrieve its output from the corresponding history or resource action."
},
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91"
}
结果字段
| 字段 | 说明 |
|---|---|
data.requestId | 为生成操作提供的 UUID v4。 |
data.historyId | 当前账户拥有的语音历史记录 ID:64 个小写十六进制字符,应用内预览可带 free- 前缀。 |
data.status | 语音结果就绪时为 completed。 |
data.characterCount | 去除首尾空白后的完整输入文本字符数。 |
data.previewEndIndex | 部分预览音频的字符边界;完整音频则为 null。付费集成生成会返回完整音频。 |
data.downloadUrl | 临时 MP3 下载 URL。通过 tts_download 请求新的 URL。 |
恢复并轮询此请求
即使连接丢失,也应使用同一标识符。如果 resultStored 为 false,请通过相应的历史记录、结果或下载操作获取输出。切勿仅为查看进度而启动第二次生成。curl --request POST 'https://api.voicecheap.ai/v1/actions/action_request_get' \
--header "x-api-key: $VOICECHEAP_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"requestId":"284a861a-4da8-4d67-ad48-8123e76c8e91","operation":"tts_generate"}'
HTTP 200
{
"data": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"operation": "tts_generate",
"status": "completed",
"createdAt": 1792144800000,
"updatedAt": 1792144820000,
"resultStored": true,
"result": {
"requestId": "284a861a-4da8-4d67-ad48-8123e76c8e91",
"historyId": "722c80c6412edd4c3dcd625061e1c1feafbb0d54274d95fd8b5ca047f8cdd23d",
"status": "completed",
"characterCount": 12,
"previewEndIndex": null,
"downloadUrl": "https://files.example.com/launch-script-speech.mp3"
}
}
}
操作说明
- 为此写入操作使用 UUID v4 格式的 requestId,重试时使用相同参数复用该 ID。长时间运行的操作首先返回 running 请求;此示例是可通过 action_request_get 获取的已完成处理程序结果。
- 请从 voices_list 中选择 voice。dubbing_voices_list 中的配音音色 ID 属于独立的音色库。
- 文本会去除首尾空白。架构最多接受 128,000 个字符;当前 Beginner 和 Starter 套餐每次生成允许 64,000 个字符,Creator 及更高套餐则允许 128,000 个字符。生成前会再次检查当前套餐和可用的 TTS 字符额度。
- inputType 默认为 text,可以为 text 或 pdf。即使使用 pdf,也必须在 text 中提供提取的文本;此操作不接受 PDF 上传或 URL。
- instructionPreset 默认为 default,支持 default、cheerful、calm、narrator、dramatic、whisper 和 energetic。instructions 为可选项,去除首尾空白后最多 300 个字符。speed 默认为 1,范围为 0.5 至 2。
- sourceFileName 为可选元数据,去除首尾空白后最多 255 个字符,用于确定下载文件名。
- 重复的成功请求会返回已保存的音频,不会再次生成或收费。URL 是临时地址;请使用 tts_download 获取新的下载地址。
- 语音历史记录和自定义音色归已认证账户所有。使用团队计费并不授予访问其他成员语音或音色的权限。
用量
每次成功调用消耗一次月度 API/MCP 调用额度。媒体额度和操作本身的生成限额也适用。错误
| HTTP 状态 | 代码 | 含义或恢复方式 |
|---|---|---|
| 400 | CREDENTIAL_IN_QUERY | 凭证通过 access_token 或 api_key 查询参数提供。请在标头中发送凭证。 |
| 400 | IDEMPOTENCY_CONFLICT | Idempotency-Key 请求头必须与请求体中的 requestId 字段一致。 |
| 400 | INVALID_REQUEST_ID | 请在 requestId 或 Idempotency-Key 中提供 UUID v4。 |
| 400 | INVALID_VOICE | 所选音色无效、不属于此账户或尚未就绪。 |
| 400 | TEXT_REQUIRED | 移除语音表达控制标签后,脚本为空。 |
| 400 | TEXT_TOO_LONG | 脚本超过当前套餐每次生成的字符数限额。 |
| 400 | VALIDATION_ERROR | 输入不符合操作架构;请查看 details 了解字段错误。 |
| 400 | VOICE_UNAVAILABLE | 所选音色库音色已不可用;请选择 voices_list 返回的 ID。 |
| 401 | API_KEY_EXPIRED | API 密钥已过期。 |
| 401 | INVALID_API_KEY | 请提供有效且未过期的 API 密钥。 |
| 401 | INVALID_CREDENTIAL | Authorization 标头格式错误或与 x-api-key 冲突。 |
| 401 | MISSING_CREDENTIAL | 未提供可用的请求头凭据,或其长度超过 4096 个字符。 |
| 401 | USER_NOT_FOUND | 该凭据对应的账户已不存在。 |
| 403 | ACCOUNT_UNAVAILABLE | 调用者账户或计费账户不可用,或正在删除。 |
| 403 | AMBIGUOUS_TEAM_API_KEY | 此凭据对应的用户属于多个团队,因此计费上下文不明确。 |
| 403 | INSUFFICIENT_SCOPE | 请授予全部所需权限,或选择其他凭据。 |
| 403 | INTEGRATION_PLAN_UNSUPPORTED | 无法验证该账户的集成限制。 |
| 403 | PAID_PLAN_REQUIRED | 当前账户已不再具备可通过集成生成语音的付费套餐。 |
| 403 | SUBSCRIPTION_REQUIRED | 请使用有效的付费订阅。 |
| 403 | TEAM_OWNER_NOT_FOUND | 找不到实际生效的团队所有者账户。 |
| 409 | CONCURRENT_ACTION_LIMIT_REACHED | 此账户已有 10 个集成操作正在运行。请先轮询现有请求,再启动另一个操作。 |
| 409 | IDEMPOTENCY_CONFLICT | 只有操作和参数完全相同时,才能复用标识符。 |
| 409 | TEXT_TO_SPEECH_GENERATION_IN_PROGRESS | 完全相同的语音请求仍在处理中。 |
| 409 | TEXT_TO_SPEECH_REQUEST_CONFLICT | 此 requestId 已用于不同的语音输入。 |
| 429 | INTEGRATION_MONTHLY_LIMIT_REACHED | 请等待月度额度重置,或更改套餐。 |
| 429 | INTEGRATION_RATE_LIMIT_REACHED | 如果提供了 Retry-After,请等待其指定的时间,并采用退避重试。 |
| 429 | TEXT_TO_SPEECH_CHARACTER_LIMIT_REACHED | 脚本文本超出了已包含及已购买的可用 TTS 字符总量。 |
| 500 | INTERNAL_ERROR | 在决定是否重试修改操作前,请检查已保存的请求状态和资源历史记录。 |
| 502 | INTERNAL_ERROR | 无法完成操作或确认其结果。启动另一次生成前,请检查请求状态和资源历史记录。 |
| 503 | ACTION_RESULT_UNAVAILABLE | 操作已执行,但未能保存结果。在启动新的生成前,请检查已保存的请求状态和资源历史记录。 |
| 503 | SERVER_RESTARTING | 服务器正在重启。请稍后重试;对于修改操作,请保持请求标识符不变。 |
{
"code": "INSUFFICIENT_SCOPE",
"message": "This credential does not permit the requested action.",
"status": 403,
"details": {
"requiredScopes": ["tts:write"]
}
}
此页面对您有帮助吗?

