Suno 歌曲生成 API 对接指南
随着人工智能技术的飞速发展,各类 AI 程序已如雨后春笋般涌现。AI 不再是遥不可及的存在,它的身影深入了人类工作与生活的每一个角落。其应用领域也愈加广泛,从初期的写作,到现如今的医疗、教育,甚至扩展至音乐的创作。
Suno 是一个专注于高质量音乐创作的 AI 平台,用户只需输入简短的文本提示,即可根据不同的流派与风格生成带有歌声的歌曲。这个 AI 音乐生成器由 Meta、TikTok、Kensho 等知名科技公司的精英团队研发,其愿望是使每个人不需任何乐器,就能创造出动人的旋律。
Suno 最新已将音乐生成模型升级至 V3 版本,现可制作长达两分钟的歌曲。
不过,遗憾的是,Suno 官方并未提供 API 接口,AceDataCloud 提供了一套模拟 Suno 官方的 API,便捷地帮助用户生成所需的音乐。
注册试用链接
注册试用链接
申请与使用
要使用 Suno Audios API,请首先访问 Suno Audios Generation API 页面,点击「Acquire」按钮以获取请求所需的凭证:
如果你尚未登录或注册,系统会自动跳转到登录页面邀请你进行注册与登录,完成注册后将自动返回至当前页面。
首次申请时,你将享有免费的额度,以便于体验该 API 的功能。
基本使用
在创作歌曲时,你可以自由输入一段文字,例如,如果你想生成一首关于圣诞节的歌曲,可以输入 a song for Christmas
,如下图所示:
生成的代码如下:
可点击「Try」按钮直接测试 API,稍等 1-2 分钟后,即可获得如下结果:
{"success": true,"data": [{"id": "2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5","title": "Winter Wonderland","image_url": "https://cdn1.suno.ai/image_2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.png","lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near","audio_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp3","video_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp4","created_at": "2024-05-10T16:21:37.624Z","model": "chirp-v3","prompt": "A song for Christmas","style": "holiday"},{"id": "5dca232b-17cc-4896-a2d1-4b59178bf410","title": "Winter Wonderland","image_url": "https://cdn1.suno.ai/image_5dca232b-17cc-4896-a2d1-4b59178bf410.png","lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near","audio_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp3","video_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp4","created_at": "2024-05-10T16:21:37.624Z","model": "chirp-v3","prompt": "A song for Christmas","style": "holiday"}]
}
如你所见,我们得到了两首歌的内容,包括标题、预览图、歌词、音频和视频链接等信息。
字段说明如下:
- success:表示生成是否成功,成功则为
true
,否者为false
- data:包含生成音乐的详细信息列表。
- id:歌曲 ID
- title:歌曲标题
- image_url:封面图片
- lyric:歌词
- audio_url:音频文件链接,打开即为 mp3 格式。
- video_url:视频文件链接,打开即为 mp4 格式。
- created_at:创作时间
- model:使用的模型,通常为最新的 v3 模型
- style:风格
自定义生成
若想要自定义歌词生成,可以直接输入相应的歌词文本:
此时 lyric
字段可以传入如下内容:
[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near
注意,歌词中的
\n
是换行符。如果你不知如何生成歌词,可以使用 AceDataCloud 提供的歌词生成 API,借助 Suno Lyrics Generation API 来生成歌词。
接下来,我们可以根据已有的歌词、标题和风格自定义生成歌曲,相关内容可指定如下:
- lyric:歌词文本
- custom:填写
true
,即进行自定义生成,该参数默认值为 false,代表使用prompt
生成。 - instrumental:无歌词的选项,默认为
false
,当其为true
时将忽略输入的lyric
参数。 - file:歌曲标题。
- style:歌曲风格,选择性填写。
填写示例见下:
完成填写后,系统自动生成代码如下:
对应的代码为:
curl -X POST 'https://api.acedata.cloud/suno/audios' \
-H 'authorization: Bearer {token}' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
"lyric": "[Verse]\\nSnowflakes falling all around\\nGlistening white\\nCovering the ground\\nChildren laughing\\nFull of delight\\nIn this winter wonderland tonight\\nSanta's sleigh\\nUp in the sky\\nRudolph's nose shining bright\\nOh my\\nHear the jingle bells\\nRinging so clear\\nBringing joy and holiday cheer\\n[Verse 2]\\nRoasting chestnuts by the fire's glow\\nChristmas lights\\nThey twinkle and show\\nFamilies gathering with love and cheer\\nSpreading warmth to everyone near",
"custom": true
}'
经过测试,生成的效果类似。
继续生成
若想在已有歌曲的基础上继续生成,可以将参数 action
设置为 extend
,并输入需要继续生成的歌曲 ID,歌曲 ID 可通过基本使用过程得到,如下图所示:
此时可见歌曲的 ID 为:
"id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07"
注意,此处的歌词中
id
是生成后的歌曲 ID。如你对生成歌曲的流程尚不明晰,可以参考上文中的基本使用指南。
接下来,我们必须填上歌词、风格以便自定义生成歌曲,相关内容可指定如下:
- lyric:歌词文本
- custom:填写
true
,即进行自定义生成,该参数默认值为 false,代表使用prompt
生成。 - style:歌曲风格,选择性填写。
- continue_at:以秒为单位继续现有音频的时间。例如,213.5 表示将继续到 3 分 33.5 秒。
填写示例见下:
填写完毕后,系统会自动生成代码如下:
对应的 Python 代码如下:
import requestsurl = "https://api.acedata.cloud/suno/audios"headers = {"accept": "application/json","authorization": "Bearer {token}","content-type": "application/json"
}payload = {"action": "extend","prompt": "A song for Christmas","audio_id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07","continue_at": 10,"style": "cute","lyric": "la la la"
}response = requests.post(url, json=payload, headers=headers)
print(response.text)
点击运行,你将得到如下结果:
{"success": true,"task_id": "baf55c32-1207-4bdf-bd00-32a864f0474d","data": [{"id": "5a3d0054-a6c5-43a9-a348-eae22d1f0efe","title": "","image_url": "https://cdn2.suno.ai/image_5a3d0054-a6c5-43a9-a348-eae22d1f0efe.jpeg","lyric": "la la la","audio_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp3","video_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp4","created_at": "2024-07-25T11:15:49.320Z","model": "chirp-v3.5","prompt": null,"style": "cute","duration": 7.96},{"id": "9ae90c96-adcf-4aad-a591-361485168f13","title": "","image_url": "https://cdn2.suno.ai/image_9ae90c96-adcf-4aad-a591-361485168f13.jpeg","lyric": "la la la","audio_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp3","video_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp4","created_at": "2024-07-25T11:15:49.321Z","model": "chirp-v3.5","prompt": null,"style": "cute","duration": 4.2}]
}
从中可见,结果内容与之前一致,这便实现了歌曲的继续生成功能。
异步回调
鉴于 Suno 生成音乐需时相对较长,大约需 1-2 分钟,若 API 长时间不回应,HTTP 请求将持续保持连接,从而消耗额外的系统资源,因此本 API 提供了异步回调的支持。
整体流程是:在客户端发起请求时,额外指定一个 callback_url
字段。发起 API 请求后,系统将迅速返回一个结果,其中包含一个 task_id
字段,代表当前的任务 ID。当任务完成后,生成音乐的结果将通过 POST JSON 的形式发送至客户端所指定的 callback_url
,其内容也包括 task_id
字段,这样任务结果即可通过 ID 进行关联。
以下,我们通过示例了解具体的操作方法。
首先,Webhook 回调是一个接收 HTTP 请求的服务,开发者应替换为自己搭建的 HTTP 服务器 URL。在此,为便于演示,使用一个公开的 Webhook 示例网站 https://webhook.site/,打开该网站即可获得 Webhook URL,如下图所示:
复制此 URL 作为 Webhook 使用,上述样例为 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2。
接下来,我们可以将 callback_url
字段设置为上述 Webhook URL,同时填入 prompt
,如下图所示:
点击运行后,即可立即获得如下结果:
{"task_id": "44472ab8-783b-4054-b861-5bf14e462f60"
}
稍等片刻,便可在 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2 上观察到生成歌曲的结果,如下图所示:
内容如下:
{"success": true,"task_id": "44472ab8-783b-4054-b861-5bf14e462f60","data": [{"id": "da4324e5-84b2-484b-b0e9-dd261381c594","title": "Winter Whispers","image_url": "https://cdn1.suno.ai/image_da4324e5-84b2-484b-b0e9-dd261381c594.png","lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing","audio_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp3","video_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp4","created_at": "2024-05-11T07:33:05.430Z","model": "chirp-v3","prompt": "A song for Christmas","style": "pop"},{"id": "b878a87b-a0db-4046-8ccd-ecd2fb3d4372","title": "Winter Whispers","image_url": "https://cdn1.suno.ai/image_b878a87b-a0db-4046-8ccd-ecd2fb3d4372.png","lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing","audio_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp3","video_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp4","created_at": "2024-05-11T07:33:05.430Z","model": "chirp-v3","prompt": "A song for Christmas","style": "pop"}]
}
如你所见,结果中包含一个 task_id
字段,其余字段与上文类似,利用此字段便可关联任务。