Suno API 的 Go 调用示例
标准库 net/http + encoding/json 足够,不需要第三方 SDK。
准备
Go 1.20+,Key 放环境变量。用 context.WithTimeout 给提交和轮询设置分钟级超时;生成是异步的,提交成功不等于完成。
提交与解析
type createResp struct {
Data struct {
Clips []struct {
SongID string `json:"song_id"`
} `json:"clips"`
} `json:"data"`
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
body := strings.NewReader(`{"description":"city pop, 90s, female vocal","model":"suno-v6"}`)
req, _ := http.NewRequestWithContext(ctx, "POST", base+"/api/music/create", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("SUNO_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { return err }
defer resp.Body.Close()
var out createResp
json.NewDecoder(resp.Body).Decode(&out)
songID := out.Data.Clips[0].SongID
轮询与并发
每 3–5 秒查一次,状态到 complete/completed 再取地址;failed/error 读原因并停止。批量时用 worker pool + 有缓冲 channel,别为每首歌无限起 goroutine;429 时退避并降低并发。
常见问题 / FAQ
需要第三方库吗?
不需要。JSON 用 encoding/json,HTTP 用 net/http。
context 超时设多少?
提交和轮询分别设分钟级;超时后查任务,不要重新提交。
能开很多 goroutine 吗?
可以,但要限制并发数;无上限并发只会更快撞到 429。
申请 Key、第一个请求、接口清单与报错排查
注册只需要邮箱,一次生成 0.6 元出 2 首,MP3 与 WAV 下载不限次。
查看并发与队列建议
注册只需要邮箱,一次生成 0.6 元出 2 首,MP3 与 WAV 下载不限次。