Music streaming

This guide shows you how to stream music with Eleven Music.

How-to guide · Assumes you have completed the Music quickstart.

This guide will show you how to stream music with Eleven Music.

The Eleven Music API is only available to paid users.

Using the Eleven Music API

This guide assumes you have set up your API key and SDK. Complete the quickstart first if you haven’t.

1

Make the API request

Create a new file named example.py or example.mts, depending on your language of choice and add the following code:

1# example.py
2from elevenlabs.client import ElevenLabs
3from elevenlabs.play import play
4import os
5from io import BytesIO
6from dotenv import load_dotenv
7load_dotenv()
8
9elevenlabs = ElevenLabs(
10 api_key=os.getenv("ELEVENLABS_API_KEY"),
11)
12
13stream = elevenlabs.music.stream(
14 prompt="Create an intense, fast-paced electronic track for a high-adrenaline video game scene. Use driving synth arpeggios, punchy drums, distorted bass, glitch effects, and aggressive rhythmic textures. The tempo should be fast, 130–150 bpm, with rising tension, quick transitions, and dynamic energy bursts.",
15 music_length_ms=10000,
16)
17
18# Create a BytesIO object to hold the audio data in memory
19audio_stream = BytesIO()
20
21# Write each chunk of audio data to the stream
22for chunk in stream:
23 if chunk:
24 audio_stream.write(chunk)
25
26# Reset stream position to the beginning
27audio_stream.seek(0)
28
29play(audio_stream)
2

Execute the code

1python example.py

You should hear the generated music playing.

Next steps