Docs menu

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-data

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"
}

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_key

Body Parameters

promptstringrequired
Text prompt describing the music to generate. Max length 2000 characters.
durationintegerrequired
Desired duration of the output track in seconds. Min 5, max 360.
modestring
Optional. 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_formatstring
Optional. 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).
segmentsstring
Optional 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

titleevent
Generated track title. Appears once near the start of the stream.
audio_chunkevent
Base64-encoded AAC/fMP4 audio fragment. Includes stream_index and num_streams; group by stream_index and append in order.
errorevent
Generation failed. Contains code and message.
completeevent
Stream finished successfully.