Guides

How to Compose Music with an AI API: A Complete Developer Guide (2026)

Written by
Sonilo Team
Published
How to Compose Music with an AI API: A Complete Developer Guide (2026) cover image

Published on Sonilo — the developer platform for AI-powered music generation.

Quick Summary

  • An AI music compose API accepts a text prompt and structured parameters, then returns a rendered audio file — no musical training required.
  • The most important parameters are: prompt quality, duration, genre/mood conditioning, seed value, and output format.
  • Commercial use rights vary by platform — always review your API provider's terms of service before shipping generated music.
  • The complete workflow runs in five steps: authenticate → build your payload → POST the request → handle the response → store and serve the audio.
  • Sonilo provides a developer-first AI music generation API purpose-built for programmatic, commercial use cases including apps, games, video, and podcasts.

Introduction

In 2024, an indie game developer shipped a fully scored mobile game in under 48 hours — without hiring a composer, without licensing a single stock track, and without writing a note of sheet music. The entire soundtrack was generated by a single API call, iterating on a text prompt until the mood was right.

This is no longer a novelty. It is the new standard workflow.

The shift from licensed stock music to AI-generated, on-demand audio has accelerated faster than most of the industry anticipated. Licensing a 60-second royalty-free track from a stock library can cost anywhere from $30 to several hundred dollars per use. Custom composition from a professional costs thousands. AI music APIs collapse that cost to fractions of a cent per second of generated audio — with no licensing fees, no exclusivity restrictions, and no waiting.

For developers specifically, the appeal goes beyond cost. AI music APIs are composable, automatable, and parameterizable. You can tie the tempo of background music to a user's heart rate data, generate a unique intro track for every podcast episode, or dynamically score a game level based on in-game events — all through code.

This guide covers exactly how AI music compose APIs work: the parameters that matter, the end-to-end developer workflow from API key to rendered audio file, how to choose the right platform for your use case, real-world integration patterns, and what you need to know about licensing before you ship. Whether you are evaluating platforms like ElevenLabs, Suno, Udio, or Sonilo, this is the most comprehensive technical reference available.

What Is an AI Music Compose API and How Does It Work?

An AI music compose API is an HTTP endpoint that accepts a structured request — containing a natural language prompt, generation parameters, and authentication credentials — and returns a generated audio file. The entire interaction follows the same pattern as any REST API: you send a POST request with a JSON payload, and you receive either a binary audio stream or a URL pointing to a rendered file.

Under the hood, these APIs are powered by one of three model architectures:

  • Diffusion-based models (similar to image diffusion models like Stable Diffusion) iteratively denoise a random audio signal, guided by the text prompt, until a coherent musical output emerges. Stability AI's Stable Audio is an example of this approach.
  • Transformer-based autoregressive models generate audio tokens sequentially, predicting each next segment based on the preceding context and the prompt conditioning. Meta's MusicGen (introduced in the 2023 paper Simple and Controllable Music Generation by Copet et al.) and Google's MusicLM are the most-cited academic implementations of this architecture.
  • Hybrid approaches combine both — using a transformer to generate a high-level musical "plan" and a diffusion decoder to render it into high-fidelity audio.

The anatomy of a standard AI music compose request includes:

  • Authorization header — your API key, typically passed as a Bearer token
  • Prompt field — a natural language description of the music you want
  • Duration — requested length of the output in seconds
  • Format — desired output file type (MP3, WAV, FLAC, or OGG)
  • Style/genre tags — optional structured conditioning parameters
  • Seed — optional integer for reproducible outputs

A generalized illustrative API call looks like this:

curl -X POST "https://api.example-music-ai.com/v1/music/compose" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Upbeat acoustic folk, fingerpicked guitar, 90 BPM, warm and nostalgic, suitable for a travel vlog intro",
"duration": 30,
"format": "mp3",
"seed": 42
}'

This is the general pattern implemented by ElevenLabs' /music/compose endpoint (documented at elevenlabs.io/docs/api-reference/music/compose), as well as by Suno, Udio, and Sonilo's own music generation API. The specific parameter names and response structures differ by platform, but the underlying request-response contract is consistent across the ecosystem.

A note on output formats: MP3 is the right choice for most web and mobile deployments (compressed, broadly compatible). WAV is preferred for professional post-production or broadcast use (lossless, large file size). FLAC offers lossless compression and is ideal for archival or high-fidelity consumer audio products. OGG/Vorbis is well-supported in game engines like Unity and Godot. Choose your format based on your deployment target, not convenience.

The Parameters That Control Your AI-Generated Music

Parameter quality is the single largest driver of output quality in any AI music API. The model can only produce what your request describes. Here is a breakdown of every parameter category and how to use it effectively.

Prompt / Text Description

The prompt is the most important field. A weak prompt produces generic, undifferentiated output. A strong prompt produces immediately usable audio.

Weak prompt: "happy music"

Strong prompt: "Upbeat acoustic folk, fingerpicked guitar and light percussion, 90 BPM, warm and nostalgic, no vocals, suitable for a travel vlog intro"

The formula that consistently produces the best results across all major AI music APIs is:

  1. Genre — specify the musical genre clearly (acoustic folk, lo-fi hip hop, cinematic orchestral, etc.)
  2. Mood/emotion — describe the emotional register (melancholic, triumphant, tense, playful)
  3. Tempo — provide BPM or a relative descriptor (slow ballad, mid-tempo groove, fast-paced)
  4. Instrumentation — name the specific instruments you want to hear
  5. Vocals — state explicitly whether vocals should be present or absent
  6. Use context — optionally describe where the music will be used (game menu, podcast outro, meditation session)

Duration

Most APIs accept duration in seconds and support outputs between 15 seconds and 3–4 minutes. Longer generations are more computationally expensive and typically have higher latency. For looping background music, generate a longer clip and use your audio engine's loop detection or manual trim — AI-generated tracks rarely end on a perfect musical boundary by default.

Style Tags and Genre Conditioning

Some platforms (including Suno and Sonilo) support structured style tags as a separate parameter field, distinct from the free-text prompt. Style tags offer more deterministic conditioning — the model is more tightly bound to the tagged attributes. Use free-text prompts when you want creative flexibility; use structured tags when you need production consistency.

Seed / Determinism Parameters

The seed parameter is essential for production workflows. By fixing the seed value, you can regenerate the same output identically — critical for debugging, A/B testing, or re-exporting audio at a different bitrate. Store the seed alongside every generated file in your database.

Output Format and Quality Settings

Higher sample rates (44.1 kHz or 48 kHz) and higher bitrates (320 kbps for MP3) produce noticeably better audio quality but increase file size and storage costs. For mobile games, 128–192 kbps MP3 at 44.1 kHz mono is often sufficient. For video scoring or broadcast, always use WAV or FLAC at 48 kHz stereo.

Negative Prompts and Exclusions

Several platforms now support negative prompts — a field that tells the model what not to include. Common exclusions include:

  • "no vocals" — essential for background music use cases
  • "no drums" or "no percussion" — for ambient or lo-fi pads
  • "no electric guitar" — to enforce acoustic or orchestral character

Not all APIs support this feature. It is worth evaluating when output consistency is critical to your product experience.

Step-by-Step: How to Generate Music via API From Scratch

This section walks through the complete developer workflow from zero to a rendered audio file saved to disk.

Step 1 — Authentication

Every AI music API authenticates via API key. Obtain your key from the platform dashboard and store it as an environment variable — never hardcode it in your application code or commit it to version control.

export MUSIC_API_KEY="your_key_here"

In your application, load the key at runtime:

import os
api_key = os.environ.get("MUSIC_API_KEY")

Step 2 — Constructing the Request

Build the JSON payload with all required parameters and any optional ones relevant to your use case.

import requests
import os

api_key = os.environ.get("MUSIC_API_KEY")

payload = {
"prompt": "Cinematic orchestral score, strings and brass, building tension, 120 BPM, no vocals, suitable for an action game level",
"duration": 60,
"format": "mp3",
"seed": 7391,
"style_tags": ["cinematic", "orchestral", "action"],
"negative_prompt": "no jazz, no acoustic guitar"
}

headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}

Step 3 — Handling the Response

AI music APIs use one of two response patterns:

Synchronous response: The API processes the request in real time and returns the audio binary or a CDN URL directly in the response body. Suitable for short clips (under 30 seconds). Typical latency: 2–15 seconds.

Asynchronous response (job-based): For longer generations, the API returns a job ID immediately. You then poll a status endpoint until the job completes and a download URL becomes available. This is the more common pattern for clips over 30 seconds.

The async polling workflow, described as a numbered sequence:

  1. POST your compose request → receive {"jobid": "abc123", "status": "queued"}
  2. Every 2–5 seconds, GET /jobs/abc123/status
  3. When "status": "complete", the response includes "audiourl": "https://cdn.example.com/output/abc123.mp3"
  4. Download the audio file from the URL before it expires (CDN links typically expire in 1–24 hours)
  5. Save to your permanent storage and discard the temporary URL

import time

def poll_for_result(job_id, headers, max_wait=120):
status_url = f"https://api.example-music-ai.com/v1/jobs/{job_id}"
elapsed = 0
while elapsed < max_wait:
response = requests.get(status_url, headers=headers)
data = response.json()
if data["status"] == "complete":
return data["audio_url"]
elif data["status"] == "failed":
raise Exception(f"Generation failed: {data.get('error')}")
time.sleep(3)
elapsed += 3
raise TimeoutError("Job did not complete within the timeout window")

Step 4 — Storing and Serving the Audio

Download the generated audio and upload it to your own cloud storage (AWS S3, Google Cloud Storage, or Cloudflare R2). Generate signed URLs with appropriate expiration windows for serving to end users. Implement caching for frequently reused prompts: hash the prompt + parameters as a cache key and serve from storage if the file already exists.

import boto3

def save_to_s3(audio_url, bucket, key):
audio_data = requests.get(audio_url).content
s3 = boto3.client("s3")
s3.put_object(Bucket=bucket, Key=key, Body=audio_data, ContentType="audio/mpeg")
return f"s3://{bucket}/{key}"

Step 5 — Error Handling and Rate Limits

The most common error codes you will encounter:

  • 401 Unauthorized — API key is missing, malformed, or expired. Check your Authorization header.
  • 422 Unprocessable Entity — Your prompt or parameters failed validation. Read the error message body carefully; most APIs return a descriptive reason.
  • 429 Too Many Requests — You have hit a rate limit. Implement exponential backoff with jitter, starting at a 5-second delay, doubling on each retry up to a maximum of 60 seconds.
  • 503 Service Unavailable — The generation infrastructure is temporarily overloaded. Retry with backoff; escalate to the provider's status page if the error persists.

Sonilo's API includes built-in rate limit headers (X-RateLimit-Remaining, X-RateLimit-Reset) in every response, making it straightforward to implement proactive throttling in your client code before hitting a hard 429 error.

How to Choose the Best AI Music Compose API for Your Project

Not all AI music APIs are the same, and the right platform depends heavily on your use case. Here is a structured framework for making the decision.

Evaluation Criteria

  • Output quality and naturalness — Does the generated audio sound professionally produced, or does it have artifacts, abrupt transitions, or tonal inconsistencies? Test with your specific use cases before committing.
  • Latency — What is the P95 generation time for your typical clip length? Real-time or interactive applications need sub-10-second response times. Batch pipelines can tolerate minutes.
  • Prompt flexibility — Does the API support free-text prompts, structured tags, or both? The best platforms support both, giving you flexibility during creative exploration and determinism in production.
  • Commercial use rights — Can you ship the generated audio in a commercial product without attribution, revenue restrictions, or royalty obligations? This is non-negotiable for production deployments.
  • Pricing model — Per-second generated, per-request, or subscription? Model the cost at your expected generation volume before committing.
  • API reliability and uptime — Does the provider publish SLA commitments? What is their historical uptime?
  • Developer experience — Quality of documentation, availability of SDKs (Python, Node.js, etc.), sandbox/testing environment, and responsiveness of developer support.

Platform Overview

ElevenLabs Music API — ElevenLabs is primarily a voice synthesis platform that expanded into music generation. Its /music/compose endpoint is well-documented and integrates naturally if you are already using ElevenLabs for voice. Best suited for use cases that combine voice and music in the same pipeline.

Suno — Suno excels at full-song generation with realistic vocals, verse/chorus structure, and lyrics. Strongest choice if you need complete songs with sung content rather than instrumental tracks. As of 2025–2026, Suno's commercial API has been in limited or beta access for third-party developers.

Udio — Udio produces stylistically diverse outputs with strong genre fidelity. Known for high-quality electronic, hip-hop, and pop genre outputs. API access availability for commercial developers has been limited.

Sonilo — Sonilo is built specifically for developers who need programmatic, production-grade AI music generation. It offers a clean REST API with both synchronous and asynchronous generation modes, explicit commercial licensing on all generated output, structured style tags alongside free-text prompts, and seed-based reproducibility. Sonilo is purpose-built for integration patterns — apps, games, video pipelines, and content automation — rather than consumer-facing song generation.

Decision Framework for Developers

Use the following to narrow your platform choice:

  • If you need instrumental background music for apps, games, or video — prioritize Sonilo or a similar developer-first API with clean commercial licensing and reliable latency.
  • If you need full songs with vocals and lyrics — Suno is the strongest current option for output quality in that category.
  • If you are already on the ElevenLabs platform for TTS — the ElevenLabs music endpoint offers the lowest integration overhead.
  • If stylistic diversity and genre accuracy are paramount — evaluate Udio and Sonilo outputs side by side for your specific genre targets.
  • If you are generating at high volume (100+ tracks/day) — prioritize platforms with transparent per-second pricing, high rate limits, and dedicated enterprise tiers.

Red Flags to Avoid

  • APIs without explicit commercial use grants in their terms of service
  • Platforms that claim "royalty-free" output without clarifying their training data provenance or indemnification position
  • No versioning of model outputs (a silent model update can break your product's audio consistency overnight)
  • Opaque pricing that makes cost modeling impossible before you commit to an integration

Real-World Integration Patterns for AI-Generated Music

Pattern 1 — Static Generation Pipeline

How it works: Music is generated once at content creation time, stored, and served repeatedly. The API is called by a human or an automation script, not by your end user in real time.

Best for: Podcast intros and outros, YouTube video scoring, marketing content, e-learning modules.

Example: A podcast production platform automatically generates a unique 15-second intro track for each new show registration, keyed to the show's genre tags and tone description provided at signup. The track is generated once, approved by the creator, stored in S3, and used for the life of the show.

Pattern 2 — Dynamic In-App Generation

How it works: Music generation is triggered at runtime by user context, behavior, or real-time data signals. The API is called server-side in response to application events.

Best for: Fitness apps, meditation apps, games, interactive storytelling.

Example: A fitness app reads the user's current heart rate zone from a wearable, maps it to a target BPM range, and fires a music generation request mid-workout when the user transitions between heart rate zones. The generated track seamlessly matches the new intensity level. Sonilo's low-latency synchronous endpoint is well-suited for this pattern.

Pattern 3 — Batch Generation for Content at Scale

How it works: Large libraries of unique music variations are pre-generated using programmatic prompt variation. The generation jobs run asynchronously in batches, usually overnight or during off-peak hours.

Best for: Ad agencies, content platforms, social media tools, localization pipelines.

Example: A social media ad platform needs 50 variations of a brand's campaign music — same core mood and instrumentation, but with different tempos, intensities, and durations (15s, 30s, 60s) for different placements. A batch job generates all 150 variants overnight at a cost far below any licensing alternative.

Pattern 4 — Hybrid Human + AI Composition

How it works: AI generates a foundation track — chord progressions, rhythmic bed, ambient layers — and human musicians or producers layer recorded or synthesized elements on top.

Best for: Professional music producers, film and TV composers, advertising agencies, video game audio directors.

Example: A game audio director uses an AI music API to rapidly prototype 20 different emotional directions for a boss fight cue. Three are selected for further development, and a composer records live strings and brass on top of the AI-generated foundation. The result combines the expressive nuance of live performance with the speed and cost-efficiency of AI scaffolding.

What You Need to Know About Licensing AI-Generated Music

Licensing is the most frequently skipped section in developer guides to AI music APIs. It is also the most consequential. A single misunderstood clause in a platform's terms of service can expose you to legal liability after you have already shipped your product.

The Current Copyright Landscape

The US Copyright Office issued formal guidance in 2023 and supplementary policy notes in 2024 clarifying that AI-generated content is not copyrightable by default if it lacks sufficient human authorship. This means:

  • Music generated entirely by an AI system with only a short text prompt is likely not eligible for copyright protection by the person who wrote the prompt.
  • Human-authored elements that are subsequently incorporated into an AI-generated piece (custom lyrics, performed recordings, specific arrangements) may retain copyright protection.
  • The platform or model provider does not automatically hold copyright to outputs either — the legal status is genuinely unsettled in many jurisdictions.

The practical implication for developers is that the output you generate is likely in a copyright-ambiguous zone. This is actually favorable for commercial use — it reduces the risk that someone else can claim ownership of your output — but it also means you cannot assert copyright over your generated music as an exclusive asset.

What "Royalty-Free" Actually Means

"Royalty-free" does not mean "free to use for any purpose." It means you pay a one-time fee (in this case, your API usage cost) rather than ongoing royalties per use. You still need to comply with the platform's terms of service, which may include:

  • Attribution requirements
  • Restrictions on commercial monetization
  • Revenue share above certain thresholds
  • Prohibitions on certain use categories (e.g., political advertising, adult content)

Platform Licensing Comparison

  • ElevenLabs grants commercial use rights on generated audio to paying subscribers, with additional terms for enterprise deployments. Review their current ToS at elevenlabs.io.
  • Suno has historically required attribution for non-commercial use and offers commercial licensing tiers for paid plan subscribers.
  • Udio similarly gates commercial rights behind paid subscription tiers.
  • Sonilo provides explicit commercial licensing on all generated output for API users, with no attribution requirements and no revenue share clauses — making it one of the most developer-friendly licensing positions in the current market.

Training Data Provenance and Indemnification

A significant and unresolved legal risk in the AI music space involves training data. Several major AI music platforms are named in ongoing litigation brought by major labels and rights holders alleging that their models were trained on copyrighted recordings without authorization. The Recording Industry Association of America (RIAA) filed suit against Suno and Udio in 2024, citing unlicensed use of copyrighted music in training data.

Before integrating any AI music API into a commercial product, ask:

  1. Does the provider disclose what data their model was trained on?
  2. Does the provider offer indemnification against third-party copyright claims arising from model outputs?
  3. Has the provider licensed training data from rights holders?

Best practices for legal compliance:

  • Document the prompt, parameters, seed, model version, and timestamp for every generated audio file you ship.
  • Keep your API receipts and generation logs — these demonstrate the provenance of the output.
  • Choose platforms that have published statements on training data licensing or that offer contractual indemnification.
  • Consult legal counsel before deploying AI-generated music in high-profile commercial contexts.

Frequently Asked Questions

What is the ElevenLabs music compose API and how does it work?

The ElevenLabs music compose API is an HTTP endpoint available at elevenlabs.io/docs/api-reference/music/compose that accepts a text prompt and optional parameters — including duration and style conditioning — and returns a generated audio file. It is part of ElevenLabs' broader platform, which is primarily known for voice synthesis. The endpoint follows the standard AI music API pattern: POST a JSON payload with your prompt and generation settings, receive either a direct audio response or a job ID for async retrieval. Multiple platforms, including Sonilo, ElevenLabs, Suno, and Udio, offer similar or more flexible functionality depending on your specific use case and licensing requirements.

How do I write a good prompt for AI music generation?

An effective music generation prompt should specify five elements: genre, mood, tempo, instrumentation, and use context. A strong example is: "Lo-fi hip hop, soft piano and vinyl crackle, 75 BPM, relaxed and focused, no vocals, suitable for a study playlist." A weak prompt like "relaxing music" gives the model insufficient guidance and produces inconsistent, generic output. Prompt quality is the single largest driver of output quality across all AI music APIs — more so than model selection in most practical scenarios. Always specify whether vocals are desired or not, as this is one of the most common sources of unexpected output.

Can I use AI-generated music commercially?

Commercial use rights depend entirely on the specific API provider's terms of service — there is no universal standard. Some platforms, including Sonilo, explicitly grant full commercial use rights to API users with no attribution requirements. Others gate commercial rights behind paid subscription tiers, impose revenue-share clauses, or restrict certain use categories. The copyright status of AI-generated music is also legally unsettled: the US Copyright Office has clarified that purely AI-generated works without sufficient human authorship input are not automatically copyrightable. Always review the provider's terms of service before shipping AI-generated music in any commercial product.

What is the difference between AI music generation and AI music composition via API?

AI music generation typically refers to producing a single audio clip from a prompt — one-shot output with no structural logic. AI music composition implies a more structured output: a piece with defined sections (intro, verse, chorus, outro), potentially with stems (separate instrument tracks), transition awareness, or conditional logic that adapts the music to external inputs. Composition APIs are more complex and more useful for game soundtracks, interactive media, and long-form content. When evaluating platforms, confirm whether the endpoint produces a flat audio clip or a structured, multi-section composition — the distinction significantly affects how you integrate the output into your product.

How does Sonilo's music generation compare to ElevenLabs' music compose API?

Sonilo is purpose-built for developer and API-first music generation workflows, whereas ElevenLabs' music endpoint is an extension of a voice-first platform. Key differences include: Sonilo offers both synchronous and asynchronous generation modes with clear latency SLAs, making it suitable for real-time in-app music generation as well as batch pipelines; Sonilo provides explicit commercial licensing on all API-generated output without attribution requirements; and Sonilo's API supports structured style tags alongside free-text prompts, enabling more deterministic production workflows. For developers building apps, games, or content automation pipelines where music is a core feature rather than a secondary add-on, Sonilo's developer-first design makes it the more appropriate technical choice.

Conclusion

AI music compose APIs have matured from research demos into production-grade developer infrastructure. The fundamental workflow is now stable, well-understood, and accessible to any developer who can construct a REST API call. Here is what you need to remember:

Key Takeaways:

  • An AI music compose API accepts a text prompt and parameters, then returns a generated audio file — the complete workflow runs in five steps: authenticate, construct payload, POST request, handle response, store and serve.
  • Prompt quality is the single most important variable in output quality. Use the genre + mood + tempo + instrumentation + context formula consistently.
  • Always fix a seed value in production to ensure reproducibility and enable consistent debugging.
  • Async response patterns (job ID + polling) are the norm for clips over 30 seconds — implement polling with exponential backoff.
  • Commercial use rights are not uniform across platforms — review each provider's ToS and understand training data provenance before shipping.
  • The choice of API platform matters: latency, prompt flexibility, licensing terms, output quality, and developer experience vary significantly across the current field.
  • Store the prompt, parameters, seed, model version, and generation timestamp for every file you ship — this is your provenance record.

The AI music generation space will continue evolving rapidly through 2026 and beyond. New model architectures, lower latency, stem-level control, and more transparent licensing frameworks are all active areas of development. The developers who build robust, well-documented integration pipelines now will be positioned to take advantage of every capability improvement as it arrives.

If you are ready to integrate AI music generation into your product, Sonilo's API offers developer-first tooling, explicit commercial licensing, and a clean integration experience designed for production use. Start with Sonilo's documentation and sandbox environment to test your use case before committing to a full integration.

This guide is maintained by the Sonilo team. For platform-specific API documentation, integration examples, and licensing details, visit sonilo.com.