The Music Generation API allows you to create original music tracks from text prompts. Generate instrumental or vocal music in various styles, lengths, and genres.
from elevenlabs.client import ElevenLabsclient = ElevenLabs(api_key="YOUR_API_KEY")audio = client.music.compose( prompt="Upbeat electronic dance music with heavy bass", music_length_ms=30000 # 30 seconds)# Save the generated musicwith open("generated_music.mp3", "wb") as f: for chunk in audio: f.write(chunk)
audio = client.music.compose( prompt="Epic orchestral soundtrack with dramatic strings and brass", music_length_ms=45000, force_instrumental=True)with open("instrumental.mp3", "wb") as f: for chunk in audio: f.write(chunk)
response = client.music.compose_detailed( prompt="Rock song with electric guitar", music_length_ms=60000, store_for_inpainting=True)print(f"Song ID for editing: {response.song_id}")# Save the audiowith open("editable_song.mp3", "wb") as f: f.write(response.audio)
# Electronicelectronic = client.music.compose( prompt="Deep house with progressive synths and steady beat", music_length_ms=60000)# Classicalclassical = client.music.compose( prompt="Baroque chamber music with harpsichord and strings", music_length_ms=45000, force_instrumental=True)# Rockrock = client.music.compose( prompt="Heavy metal with distorted guitars and double bass drums", music_length_ms=90000)# Jazzjazz = client.music.compose( prompt="Smooth jazz with saxophone solo and walking bass", music_length_ms=120000)# Ambientambient = client.music.compose( prompt="Ethereal ambient soundscape with pads and nature sounds", music_length_ms=180000, force_instrumental=True)
from elevenlabs.client import ElevenLabsclient = ElevenLabs(api_key="YOUR_API_KEY")prompts = [ "Upbeat pop song with catchy melody", "Dark cinematic trailer music", "Chill lofi hip hop beats", "Energetic workout music", "Peaceful meditation sounds"]for i, prompt in enumerate(prompts): print(f"Generating track {i+1}/{len(prompts)}: {prompt}") audio = client.music.compose( prompt=prompt, music_length_ms=30000 ) filename = f"track_{i+1}.mp3" with open(filename, "wb") as f: for chunk in audio: f.write(chunk) print(f"Saved: {filename}")print("All tracks generated!")
Be specific in your prompts (genre, mood, instruments)
Use force_instrumental=True for background music
Start with shorter lengths (30-60s) for testing
Use store_for_inpainting=True if you might edit later
Try different prompts to get varied results
Music generation is non-deterministic. The same prompt may produce different results each time. For consistent results, save the song_id when using store_for_inpainting.