Music streaming

Learn how to stream music with Eleven Music.

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

1

Create an API key

Create an API key in the dashboard here, which you’ll use to securely access the API.

Store the key as a managed secret and pass it to the SDKs either as a environment variable via an .env file, or directly in your app’s configuration depending on your preference.

.env
1ELEVENLABS_API_KEY=<your_api_key_here>
2

Install the SDK

We’ll also use the dotenv library to load our API key from an environment variable.

1pip install elevenlabs
2pip install python-dotenv
3

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 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)
4

Execute the code

1python example.py

You should hear the generated music playing.

Next steps

Explore the API reference for more information on the Speech to Text API and its options.