添加伴奏回调
当伴奏生成完成时,系统会调用此回调通知结果。
当您向Suno API提交伴奏生成任务时,可以使用 callBackUrl 参数设置回调URL。任务完成时,系统将自动向您指定的地址推送结果。
回调机制概述
TIP
Webhook 安全性:为确保回调请求的真实性和完整性,我们强烈建议您实现 webhook 签名验证。请参阅我们的 Webhook 校验指南 了解详细实现步骤。
回调时机
系统将在以下情况发送回调通知:
- 文本生成完成(callbackType: "text")
- 第一个音频轨道生成完成(callbackType: "first")
- 所有音频轨道生成完成(callbackType: "complete")
- 伴奏生成任务失败
- 任务处理过程中发生错误
回调可能在 text、first、complete 阶段发送,只有 complete 一定会发送,请以 complete 为最终结果。
回调方式
- HTTP方法: POST
- 内容类型: application/json
- 超时设置: 15秒
回调请求格式
当任务进行或完成时,系统将以下格式向您的 callBackUrl 发送POST请求:
完成成功回调
json
{
"code": 200,
"msg": "All generated successfully.",
"data": {
"callbackType": "complete",
"task_id": "2fac****9f72",
"data": [
{
"id": "e231****-****-****-****-****8cadc7dc",
"audio_url": "https://example.cn/****.mp3",
"stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v4-5",
"title": "钢铁侠",
"createTime": 1786343609818,
"duration": 198.44,
"tags": "electrifying, rock",
"source_audio_url":"https://example.cn/****.mp3",
"source_image_url":"https://example.cn/****.jpeg",
"source_stream_audio_url":"https://example.cn/****"
},
{
"id": "e231****-****-****-****-****8cadc7dc",
"audio_url": "https://example.cn/****.mp3",
"stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v4-5",
"title": "钢铁侠",
"createTime": 1786343609818,
"duration": 198.44,
"tags": "electrifying, rock",
"source_audio_url":"https://example.cn/****.mp3",
"source_image_url":"https://example.cn/****.jpeg",
"source_stream_audio_url":"https://example.cn/****"
}
]
}
}第一首成功回调
json
{
"code": 200,
"msg": "First audio generated successfully.",
"data": {
"callbackType": "first",
"task_id": "2fac****9f72",
"data": [
{
"id": "e231****-****-****-****-****8cadc7dc",
"audio_url": "https://example.cn/****.mp3",
"stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v4-5",
"title": "钢铁侠",
"createTime": 1786343609818,
"duration": 198.44,
"source_audio_url":"https://example.cn/****.mp3",
"source_image_url":"https://example.cn/****.jpeg",
"source_stream_audio_url":"https://example.cn/****"
}
]
}
}文本生成回调
json
{
"code": 200,
"msg": "Text generated successfully.",
"data": {
"callbackType": "text",
"task_id": "2fac****9f72",
"data": [
{
"id": "e231****-****-****-****-****8cadc7dc",
"audio_url": "",
"stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v4-5",
"title": "钢铁侠",
"createTime": 1786343609818,
"tags": "electrifying, rock",
"source_image_url":"https://example.cn/****.jpeg",
"source_stream_audio_url":"https://example.cn/****"
},
{
"id": "e231****-****-****-****-****8cadc7dc",
"audio_url": "",
"stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v4-5",
"title": "钢铁侠",
"createTime": 1786343609818,
"tags": "electrifying, rock",
"source_image_url":"https://example.cn/****.jpeg",
"source_stream_audio_url":"https://example.cn/****"
}
]
}
}失败回调
json
{
"code": 400,
"msg": "Audio generation failed",
"data": {
"callbackType": "complete",
"task_id": "2fac****9f72"
}
}失败时 code 不为 200,callbackType 为 "complete",不含 data.data。请先判断 code 再读取 data.data。
状态码说明
200 表示成功;其他值表示失败(例如 501)。
回调接收示例
以下是常用编程语言的回调接收示例代码:
Node.js
javascript
const express = require('express');
const app = express();
app.use(express.json());
app.post('/instrumental-callback', (req, res) => {
const { code, msg, data } = req.body;
console.log('收到伴奏回调:', {
taskId: data.task_id,
status: code,
message: msg,
callbackType: data.callbackType
});
if (code === 200) {
// ... 处理逻辑 ...
}
res.status(200).json({ status: 'received' });
});
app.listen(3000, () => {
console.log('伴奏回调服务器运行在端口3000');
});最佳实践
TIP
回调URL配置建议
- 使用HTTPS: 确保您的回调URL使用HTTPS协议,保证数据传输安全
- 验证来源: 在回调处理中验证请求来源的合法性
- 幂等处理: 同一个task_id可能会收到多次回调,确保处理逻辑具有幂等性
- 快速响应: 回调处理应尽快返回200状态码,避免超时
- 异步处理: 复杂的业务逻辑应异步处理,避免阻塞回调响应
- 阶段跟踪: 根据callbackType区分不同生成阶段,合理安排业务逻辑
WARNING
重要提醒
- 回调URL必须是公网可访问的地址
- 服务器必须在15秒内响应,否则将被认为超时
- 如果连续3次重试失败,系统将停止发送回调
- 请确保回调处理逻辑的稳定性,避免因异常导致回调失败
- 注意处理不同callbackType的回调,特别是complete类型的最终结果