Text to Music
Generate music from a text prompt with a caller-specified duration. By default (mode=stream) returns a streaming NDJSON response. Pass mode=async to instead get 202 Accepted with a task_id; poll GET /v1/tasks/:task_id until the task reaches a terminal status.
POST
/v1/text-to-musicmultipart/form-dataStreaming (default) — basic prompt
curl -N -X POST https://api.sonilo.com/v1/text-to-music \
-H "Authorization: Bearer sk_your_api_key" \
-F "prompt=lofi hiphop beat" \
-F "duration=60"Streaming (default) — segmented prompt
curl -N -X POST https://api.sonilo.com/v1/text-to-music \
-H "Authorization: Bearer sk_your_api_key" \
-F "prompt=cinematic orchestral score" \
-F "duration=60" \
-F 'segments=[{"start": 0, "label": "intro", "prompt": "soft solo piano"}, {"start": 20, "label": "chorus", "prompt": "full orchestra, soaring strings"}, {"start": 45, "label": "outro", "prompt": "strings fade to silence"}]'{
"type": "title",
"title": "Sunset Drive"
}
{
"type": "audio_chunk",
"sample_rate": 44100,
"channels": 2,
"stream_index": 0,
"num_streams": 1,
"data": "<base64>"
}
{
"type": "complete"
}Authorization
Authenticate via Bearer token. Generate keys at the API Keys page and pass them in the Authorization header on every request.
Authorization: Bearer sk_your_api_keyBody Parameters
promptstringrequiredText prompt describing the music to generate. Max length 2000 characters.
durationintegerrequiredDesired duration of the output track in seconds. Min 5, max 360.
modestringOptional.
stream (default) returns this endpoint's streaming NDJSON response, unchanged. async returns 202 Accepted with a task_id instead of streaming; poll GET /v1/tasks/:task_id for the result.output_formatstringOptional.
m4a (default) returns AAC-encoded audio. wav returns 16-bit PCM WAV instead, transcoded server-side after generation. Requires mode=async (the streaming response always returns m4a — see GET /v1/tasks/:task_id's audio[].content_type).segmentsstringOptional JSON array of timed segment prompts; the music changes style at each boundary. Each item has
start (seconds; the first must be 0, starts strictly increasing and at least 5 seconds apart, the last at least 5 seconds before duration), prompt (1-200 characters), and an optional label that must be exactly one of these values — any other value is rejected with a 422 error, and it defaults to none when omitted: intro, verse, pre-chorus, chorus, bridge, break, silence, outro, none. 1-30 items.Response
titleeventGenerated track title. Appears once near the start of the stream.
audio_chunkeventBase64-encoded AAC/fMP4 audio fragment. Includes
stream_index and num_streams; group by stream_index and append in order.erroreventGeneration failed. Contains
code and message.completeeventStream finished successfully.
Streaming (default) — basic prompt
curl -N -X POST https://api.sonilo.com/v1/text-to-music \
-H "Authorization: Bearer sk_your_api_key" \
-F "prompt=lofi hiphop beat" \
-F "duration=60"Streaming (default) — segmented prompt
curl -N -X POST https://api.sonilo.com/v1/text-to-music \
-H "Authorization: Bearer sk_your_api_key" \
-F "prompt=cinematic orchestral score" \
-F "duration=60" \
-F 'segments=[{"start": 0, "label": "intro", "prompt": "soft solo piano"}, {"start": 20, "label": "chorus", "prompt": "full orchestra, soaring strings"}, {"start": 45, "label": "outro", "prompt": "strings fade to silence"}]'{
"type": "title",
"title": "Sunset Drive"
}
{
"type": "audio_chunk",
"sample_rate": 44100,
"channels": 2,
"stream_index": 0,
"num_streams": 1,
"data": "<base64>"
}
{
"type": "complete"
}