跳到主要内容

生成音乐

POST 

https://api.sunoapi.org/api/v1/generate

使用AI模型生成带有或不带歌词的音乐。

参数使用指南

  • 当 customMode 为 true(自定义模式)时:

    • 如果 instrumental 为 true:需要提供 style 和 title
    • 如果 instrumental 为 false:需要提供 style、prompt 和 title
    • Prompt 长度限制:
      • 对于 V3_5 和 V4 模型:3000字符
      • 对于 V4_5 模型:5000字符
    • Style 长度限制:
      • 对于 V3_5 和 V4 模型:200字符
      • 对于 V4_5 模型:1000字符
    • title 长度限制:80字符
  • 当 customMode 为 false(非自定义模式)时:

    • 无论 instrumental 设置如何,只需要提供 prompt
    • prompt 长度限制:400字符
    • 其他参数应留空

开发者注意事项

  1. 新用户推荐设置:将 customMode 设为 false,instrumental 设为 false,只提供 prompt。这是最简单的配置,可以快速测试API并体验结果。
  2. 生成的文件将保留15天后删除
  3. 请根据 customMode 和 instrumental 设置确保提供所有必要参数,避免出错
  4. 请注意 prompt、style 和 title 的字符长度限制,确保成功处理
  5. 回调过程有三个阶段:text(文本生成完成)、first(第一首完成)、complete(全部完成)
  6. 您可以使用音乐生成详情查询接口主动检查任务状态,而不必等待回调

Request

Bodyrequired

    promptstring

    描述所需音频内容的提示词。

    • 自定义模式下(customMode: true):当 instrumentalfalse 时必填。提示词将严格作为歌词使用并在生成的音轨中演唱。不同模型的字符限制:

      • V3_5 和 V4:最多3000字符
      • V4_5:最多5000字符
        示例:"一段平静舒缓的钢琴曲,带有柔和的旋律"
    • 非自定义模式下(customMode: false):始终必填。提示词作为核心创意,歌词将根据它自动生成(不严格匹配输入),最多400字符。

      示例:"一段简短放松的钢琴曲"

    Example: 一段平静舒缓的钢琴曲,带有柔和的旋律
    stylestring

    音乐风格或流派。

    • 在自定义模式下(customMode: true)必填。示例:"爵士"、"古典"、"电子"。
      • 对于 V3_5 和 V4 模型:最大长度:200字符。
      • 对于 V4_5 模型:最大长度:1000字符。 示例:"古典"
    • 在非自定义模式下(customMode: false):留空。
    Example: 古典
    titlestring

    生成音乐的标题。

    • 在自定义模式下(customMode: true)必填。最大长度:80字符。
      示例:"宁静钢琴冥想"
    • 在非自定义模式下(customMode: false):留空。
    Example: 宁静钢琴冥想
    customModebooleanrequired

    启用自定义模式进行高级音频生成设置。

    • 设为 true 使用自定义模式(需要提供 styletitle;如果 instrumentalfalse,则需要提供 prompt)。如果 instrumentalfalse,提示词将严格用作歌词。
    • 设为 false 使用非自定义模式(只需要提供 prompt)。歌词将根据提示词自动生成。
    Example: true
    instrumentalbooleanrequired

    决定音频是否为纯音乐(无歌词)。

    • 在自定义模式下(customMode: true):
      • 如果为 true:只需提供 styletitle
      • 如果为 false:需要提供 styletitlepromptprompt 将作为精确歌词使用)。
    • 在非自定义模式下(customMode: false):不影响必填字段(只需 prompt)。如果为 false,将自动生成歌词。
    Example: true
    modelstringrequired

    用于音频生成的模型版本。

    • 可选择:
      • V4_5: 更优秀的风格融合能力,更智能的提示词处理和更快的输出速度,最长可达8分钟。
      • V4: 最佳音频质量,优化的歌曲结构,最长可达4分钟。
      • V3_5: 稳定的编曲和创意多样性,最长可达4分钟。

    Possible values: [V3_5, V4, V4_5]

    Example: V3_5
    negativeTagsstring

    需要在生成的音频中排除的音乐风格或特征。

    • 可选。用于避免特定风格。
      示例:"重金属, 强节奏鼓点"
    Example: 重金属, 强节奏鼓点
    callBackUrlurirequired

    接收任务完成通知的URL。回调过程有三个阶段:text(文本生成)、first(第一首完成)、complete(全部完成)。注意:某些情况下可能会跳过 textfirst 阶段,直接返回 complete

    Example: https://api.example.com/callback

Responses

请求成功

Schema
    codeinteger

    状态码说明

    • ✅ 200 - 请求成功
    • ⚠️ 400 - 参数错误
    • ⚠️ 401 - 没有访问权限
    • ⚠️ 404 - 请求方式或者路径错误
    • ⚠️ 405 - 调用超过限制
    • ⚠️ 413 - 主题或者prompt过长
    • ⚠️ 429 - 积分不足
    • ⚠️ 455 - 网站维护
    • ❌ 500 - 服务器异常

    Possible values: [200, 400, 401, 404, 405, 413, 429, 455, 500]

    Example: 200
    msgstring

    当 code != 200 时,展示错误信息

    Example: success
    data object
    taskIdstring

    任务ID,用于后续查询任务状态

    Example: 5c79****be8e

Callbacks

POST 

{request.body#/callBackUrl}

当音频生成完成时,系统会调用此回调通知结果。

回调示例

{
"code": 200,
"msg": "All generated successfully.",
"data": {
"callbackType": "complete",
"task_id": "2fac****9f72",
"data": [
{
"id": "8551****662c",
"audio_url": "https://example.cn/****.mp3",
"source_audio_url": "https://example.cn/****.mp3",
"stream_audio_url": "https://example.cn/****",
"source_stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"source_image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v3-5",
"title": "钢铁侠",
"tags": "electrifying, rock",
"createTime": "2025-01-01 00:00:00",
"duration": 198.44
},
{
"id": "bd15****1873",
"audio_url": "https://example.cn/****.mp3",
"source_audio_url": "https://example.cn/****.mp3",
"stream_audio_url": "https://example.cn/****",
"source_stream_audio_url": "https://example.cn/****",
"image_url": "https://example.cn/****.jpeg",
"source_image_url": "https://example.cn/****.jpeg",
"prompt": "[Verse] 夜晚城市 灯火辉煌",
"model_name": "chirp-v3-5",
"title": "钢铁侠",
"tags": "electrifying, rock",
"createTime": "2025-01-01 00:00:00",
"duration": 228.28
}
]
}
}

Body

    codeinteger

    状态码

    Example: 200
    msgstring

    返回消息

    Example: All generated successfully
    data object
    callbackTypestring

    回调类型:text(文本生成完成)、first(第一首生成完成)、complete(全部生成完成)

    Possible values: [text, first, complete]

    task_idstring

    任务ID

    data object[]
  • Array [
  • idstring

    音频唯一标识

    audio_urlstring

    音频文件URL

    source_audio_urlstring

    原始音频文件URL

    stream_audio_urlstring

    流式音频URL

    source_stream_audio_urlstring

    原始流式音频URL

    image_urlstring

    封面图片URL

    source_image_urlstring

    原始封面图片URL

    promptstring

    生成提示词/歌词

    model_namestring

    使用的模型名称

    titlestring

    音乐标题

    tagsstring

    音乐标签

    createTimedate-time

    创建时间

    durationnumber

    音频时长(秒)

  • ]

Callbacks Responses

回调接收成功

Authorization: http

name: BearerAuthtype: httpscheme: bearerbearerFormat: API Keydescription: # 🔑 API 认证说明

所有接口都需要通过 Bearer Token 方式进行认证。

## 获取 API Key

1. 访问 [API Key 管理页面](https://sunoapi.org/api-key) 获取您的 API Key

## 使用方式

在请求头中添加:

```
Authorization: Bearer YOUR_API_KEY
```

> **⚠️ 注意:**
> - 请妥善保管您的 API Key,不要泄露给他人
> - 如果怀疑 API Key 泄露,请立即在管理页面重置
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.sunoapi.org/api/v1/generate");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", "Bearer <token>");
var content = new StringContent("{\n \"prompt\": \"一段平静舒缓的钢琴曲,带有柔和的旋律\",\n \"style\": \"古典\",\n \"title\": \"宁静钢琴冥想\",\n \"customMode\": true,\n \"instrumental\": true,\n \"model\": \"V3_5\",\n \"negativeTags\": \"重金属, 强节奏鼓点\",\n \"callBackUrl\": \"https://api.example.com/callback\"\n}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
Request Collapse all
Base URL
https://api.sunoapi.org
Auth
Body required
{
  "prompt": "一段平静舒缓的钢琴曲,带有柔和的旋律",
  "style": "古典",
  "title": "宁静钢琴冥想",
  "customMode": true,
  "instrumental": true,
  "model": "V3_5",
  "negativeTags": "重金属, 强节奏鼓点",
  "callBackUrl": "https://api.example.com/callback"
}
ResponseClear

Click the Send API Request button above and see the response here!