要合成为语音的文本。
文字转语音
适用于:Chatflow、Workflow、Agent、聊天助手、旧版 Agent、文本生成应用。
将文本转换为语音音频。传入 text 可合成任意文本,或传入 message_id 为已有消息的答案配音。
1 min read
post/text-to-audio
Request example
curl --request POST \
--url https://api.dify.ai/v1/text-to-audio \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"streaming": false,
"text": "Hello, welcome to our service.",
"user": "abc-123",
"voice": "alloy"
}'import requests
url = "https://api.dify.ai/v1/text-to-audio"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json",
}
payload = {
"streaming": False,
"text": "Hello, welcome to our service.",
"user": "abc-123",
"voice": "alloy"
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/text-to-audio', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"streaming": false,
"text": "Hello, welcome to our service.",
"user": "abc-123",
"voice": "alloy"
})
});
const data = await response.json();
console.log(data);<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.dify.ai/v1/text-to-audio',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"streaming": false,
"text": "Hello, welcome to our service.",
"user": "abc-123",
"voice": "alloy"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer <token>',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.dify.ai/v1/text-to-audio"
payload := strings.NewReader("{\n \"streaming\": false,\n \"text\": \"Hello, welcome to our service.\",\n \"user\": \"abc-123\",\n \"voice\": \"alloy\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.dify.ai/v1/text-to-audio"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"streaming\": false,\n \"text\": \"Hello, welcome to our service.\",\n \"user\": \"abc-123\",\n \"voice\": \"alloy\"\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());require 'uri'
require 'net/http'
url = URI('https://api.dify.ai/v1/text-to-audio')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == 'https'
request = Net::HTTP::Post.new(url)
request['Authorization'] = 'Bearer <token>'
request['Content-Type'] = 'application/json'
request.body = '{
"streaming": false,
"text": "Hello, welcome to our service.",
"user": "abc-123",
"voice": "alloy"
}'
response = http.request(request)
puts response.read_bodyResponse
<file><file><file><file><file><file><file>{
"code": "app_unavailable",
"message": "App unavailable, please check your app configurations.",
"status": 400
}{
"code": "completion_request_error",
"message": "Completion request failed.",
"status": 400
}{
"code": "invalid_param",
"message": "TTS is not enabled",
"status": 400
}{
"code": "model_currently_not_support",
"message": "Dify Hosted OpenAI trial currently not support the GPT-4 model.",
"status": 400
}{
"code": "provider_not_initialize",
"message": "No valid model provider credentials found. Please go to Settings -> Model Provider to complete your provider credentials.",
"status": 400
}{
"code": "provider_quota_exceeded",
"message": "Your quota for Dify Hosted OpenAI has been exhausted. Please go to Settings -> Model Provider to complete your own provider credentials.",
"status": 400
}{
"code": "internal_server_error",
"message": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
"status": 500
}post/text-to-audio
Authorization
Body
application/jsonResponses
返回生成的音频。Content-Type 响应头反映供应商实际返回的音频容器格式,可识别时会根据响应字节校验。
响应体可能是 AAC、FLAC、MP4、MP3、Ogg、WAV 或 WebM。无法识别的输出按供应商声明的类型标注;未声明时标注为 audio/mpeg。
供应商流式返回时按分块传输编码交付;请求中的 streaming 字段不控制此行为。