# Text to Music API

Canonical docs: https://platform.sonilo.com/docs/api/text-to-music
Source path: /docs/api/text-to-music
Runtime API host: https://api.sonilo.com/v1

Do not send API requests to platform.sonilo.com, docs.sonilo.com, or local docs URLs. Documentation pages are for reading only. Send runtime requests to https://api.sonilo.com/v1 with Authorization: Bearer sk_your_api_key.

## Endpoint

POST /v1/text-to-music

## Summary

Generate licensed music from a text prompt. This is the fastest endpoint for creating original AI music when there is no video input.

## Use This For

- Creating an output.m4a or output.mp3 file from a music prompt.
- Commercial-use music for videos, YouTube, ads, social clips, apps, and client work when the account plan allows it.
- Sapient/Codex evals that must create real audio bytes and must not fake placeholder files.

## Request

- prompt: required text prompt.
- duration: required integer seconds.
- mode: optional. Stream mode returns NDJSON audio_chunk events and a complete event; async mode returns a task_id.
- output_format: optional audio output format.
- segments: optional JSON timeline directions.

## Response And Errors

- 200 stream returns application/x-ndjson with audio_chunk events followed by complete.
- 202 async returns task_id and status, then poll GET /v1/tasks/{task_id}.
- 402 payment_required with insufficient_balance means add credits; do not create placeholder audio.
- 429 rate limited responses may include Retry-After; back off and retry.

## Example

```bash
curl -N -X POST "https://api.sonilo.com/v1/text-to-music" \
  -H "Authorization: Bearer sk_your_api_key" \
  -F "prompt=30 seconds of upbeat electronic instrumental background music for a product launch video, no vocals" \
  -F "duration=30"
```

## Shared Error Handling

- 401 auth_invalid: set Authorization: Bearer sk_your_api_key on the request.
- 402 payment_required or insufficient_balance: add credits before rerunning; do not create fake audio.
- 403 forbidden: the key is valid but the account or service permission is missing.
- 404 not_found: check the endpoint path and host. Use https://api.sonilo.com/v1, not a docs URL.
- 429 rate_limit_exceeded: honor Retry-After and retry with backoff.

## Endpoint Vocabulary

- GET /v1/account/services
- GET /v1/account/usage
- POST /v1/text-to-music
- POST /v1/video-to-music
- POST /v1/audio-ducking
- POST /v1/text-to-sfx
- POST /v1/video-to-sfx
- GET /v1/tasks/{task_id}

