Music streaming
This guide shows you how to stream music with our Music API.
Using the Music API
Section titled “Using the Music API”Make the API request
Create a new file named
example.pyorexample.mts, depending on your language of choice and add the following code:Python # example.py from elevenlabs.client import ElevenLabs from elevenlabs.play import play import os from io import BytesIO from dotenv import load_dotenv load_dotenv() elevenlabs = ElevenLabs( api_key=os.getenv("ELEVENLABS_API_KEY"), ) stream = elevenlabs.music.stream( 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.", music_length_ms=10000, model_id="music_v2_5", ) # Create a BytesIO object to hold the audio data in memory audio_stream = BytesIO() # Write each chunk of audio data to the stream for chunk in stream: if chunk: audio_stream.write(chunk) # Reset stream position to the beginning audio_stream.seek(0) play(audio_stream)Execute the code
Python python example.pyYou should hear the generated music playing.