完成语音生成与转录
正确处理二进制语音、异步语音任务和 multipart 转录。语音合成完整流程
提交 input 或 prompt 中的一个,以及模型需要的其他字段;两者不能同时传入。响应为 audio/* 时直接保存;响应为 application/json 时读取任务 ID 并轮询。
curl
Python
Node.js
curl
#!/usr/bin/env bash
set -euo pipefail
headers=$(mktemp)
body=$(mktemp)
trap 'rm -f "$headers" "$body"' EXIT
curl -fsS -D "$headers" -o "$body" https://api.noxgate.net/v1/audio/speech \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{"model":"YOUR_MODEL_ID","input":"Hello from NoxGate","voice":"YOUR_VOICE_ID"}'
content_type=$(awk -F': *' 'tolower($1) == "content-type" {print tolower($2)}' "$headers" | tail -1 | tr -d '\r')
if [[ "$content_type" != application/json* ]]; then
mv "$body" speech.mp3
echo "Saved speech.mp3"
exit 0
fi
response=$(cat "$body")
task_id=$(printf '%s' "$response" | jq -r '.id // empty')
status=$(printf '%s' "$response" | jq -r '.status // empty')
deadline=$((SECONDS + 300))
delay=2
while [[ -n "$task_id" && "$status" != "succeeded" ]]; do
case "$status" in failed|canceled|cancelled) printf '%s\n' "$response" >&2; exit 1 ;; esac
[[ $SECONDS -ge $deadline ]] && { echo "Speech generation timed out" >&2; exit 1; }
sleep "$delay"
response=$(curl -fsS "https://api.noxgate.net/v1/audio/speech/$task_id" \
-H "Authorization: Bearer sk-your-api-key")
status=$(printf '%s' "$response" | jq -r '.status // empty')
(( delay < 10 )) && delay=$((delay + 2))
done
result_url=$(printf '%s' "$response" | jq -r '.url // empty')
[[ -n "$result_url" ]] || { printf '%s\n' "$response" >&2; exit 1; }
if [[ "$result_url" == /* ]]; then result_url="https://api.noxgate.net$result_url"; fi
curl -fL "$result_url" -o speech.mp3
echo "Saved speech.mp3"音频转录完整流程
转录必须使用 multipart/form-data 上传真实文件,不能在 JSON 中传本地路径。
curl
Python
Node.js
curl
curl https://api.noxgate.net/v1/audio/transcriptions \
-H "Authorization: Bearer sk-your-api-key" \
-F "model=YOUR_MODEL_ID" \
-F "file=@./audio.mp3"结果与错误处理
- voice、格式、文件类型与可选参数都受模型能力限制。
- 异步任务必须处理 failed、canceled 和 cancelled,并设置总等待时间。
- 异步成功返回的 URL 应及时下载,不要假设永久有效。