How to Use the Text to Sound Effects API

Learn how to use the text to sound effects API to generate sound effects from text.

Introduction

Our text to sound effects model enables you to create high-quality sound effects from a short description. These sound effects could be used in a variety of applications, including game development and building apps for music production.

In this tutorial, we will use the text to sound effects API to generate a sound effect from a short description using the Python SDK. We’ll then save this sound effect to a file.

For general tips on prompting, see the sound effects product docs. And for information on the API configuration visit the API reference.

How to generate a sound effect with the API

Requirements

Before proceeding, please ensure that you have the following:

  • An ElevenLabs account with an API key (here’s how to find your API key)
  • Python or Node.js installed on your machine

Then, install the ElevenLabs SDK as shown below

Python
$pip install elevenlabs

Install the necessary packages to manage your environmental variables:

Python
$pip install python-dotenv

Next, create a .env file in your project directory and fill it with your credentials like so:

.env
$ELEVENLABS_API_KEY=your_elevenlabs_api_key_here

Using the sound effects SDK

Now we can use the SDK to generate a sound effect from a short description and save it to a file as shown below.

1import os
2from elevenlabs.client import ElevenLabs
3
4from dotenv import load_dotenv
5
6load_dotenv()
7
8elevenlabs = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
9
10
11def generate_sound_effect(text: str, output_path: str):
12 print("Generating sound effects...")
13
14 result = elevenlabs.text_to_sound_effects.convert(
15 text=text,
16 duration_seconds=10, # Optional, if not provided will automatically determine the correct length
17 prompt_influence=0.3, # Optional, if not provided will use the default value of 0.3
18 )
19
20 with open(output_path, "wb") as f:
21 for chunk in result:
22 f.write(chunk)
23
24 print(f"Audio saved to {output_path}")
25
26
27if __name__ == "__main__":
28 generate_sound_effect("Dog barking", "output.mp3")

Configuration

  • duration_seconds: The duration of the sound effect in seconds. If not provided, the API will automatically determine the correct length. The maximum value is 22
  • prompt_influence: The amount of influence the prompt has on the generated sound effect. If not provided, the API will use the default value of 0.3

API pricing

The API is charged at 100 characters per generation with automatic duration or 25 characters per second with a set duration.

Next steps

We’re excited to see what you build with the API. Here are some ideas of what you might want to use it for:

  • Adding sound effect generation to a video editing application
  • Enabling users to create on-demand samples for their music production
  • A new type of video game where every sound is generated dynamically

For higher rate limits of volume based discounts please contact sales.