AssemblyAI Ships AssemblyAI-TTS-1: A $15 per 1M Character Text-to-Speech API for Voice Agents
Back to Tutorials
techTutorialbeginner

AssemblyAI Ships AssemblyAI-TTS-1: A $15 per 1M Character Text-to-Speech API for Voice Agents

October 28, 202646 views4 min read

Learn how to use AssemblyAI's new TTS-1 text-to-speech API to create voice agents with just a few lines of Python code.

Introduction

In this tutorial, you'll learn how to use AssemblyAI's new TTS-1 text-to-speech API to create voice agents. This is perfect for beginners who want to start building voice-enabled applications without complex setup. We'll walk through the entire process from signing up to making your first voice generation call.

Prerequisites

  • A free AssemblyAI account (sign up at assemblyai.com)
  • Basic understanding of APIs and HTTP requests
  • Python installed on your computer (version 3.6 or higher)
  • Some familiarity with command line tools

Step-by-Step Instructions

1. Get Your AssemblyAI API Key

1.1 Visit the AssemblyAI Website

Go to assemblyai.com and click on "Sign Up" to create a free account. This is where you'll get your API key that allows you to access the TTS-1 service.

1.2 Navigate to Your API Dashboard

After logging in, you'll see a dashboard. Look for the "API Keys" section or click on your profile icon and select "API Keys" from the menu. This is where you'll find your unique key.

1.3 Copy Your API Key

Click on the "Copy" button next to your API key. Keep this key safe because you'll need it in the next steps. Never share your API key publicly.

2. Install Required Python Packages

2.1 Open Your Command Line

Open your terminal or command prompt. This is where you'll run Python commands.

2.2 Install the Requests Library

The requests library makes it easy to send HTTP requests in Python. Run this command:

pip install requests

2.3 Install the Wave Library (Optional)

We'll also install the wave library to handle audio files, though it's built into Python. Run:

pip install wave

3. Create Your First Text-to-Speech Script

3.1 Create a New Python File

Create a new file called tts_demo.py in your preferred code editor.

3.2 Add Your API Key to the Script

Start by adding your API key to the script:

import requests

# Replace 'YOUR_API_KEY' with your actual AssemblyAI API key
API_KEY = 'YOUR_API_KEY'

3.3 Create the Function to Generate Speech

Now add the function that will make the API call:

def text_to_speech(text, output_file='output.wav'):
    url = "https://api.assemblyai.com/v1/tts"
    
    headers = {
        "authorization": API_KEY,
        "content-type": "application/json"
    }
    
    data = {
        "text": text,
        "voice": "samantha"  # You can change this to other voices
    }
    
    response = requests.post(url, json=data, headers=headers)
    
    if response.status_code == 200:
        with open(output_file, 'wb') as f:
            f.write(response.content)
        print(f"Audio saved as {output_file}")
    else:
        print(f"Error: {response.status_code} - {response.text}")

4. Test Your Voice Generation

4.1 Add Sample Text to Your Script

Below your function, add some sample text:

if __name__ == "__main__":
    sample_text = "Hello! This is a test of AssemblyAI's TTS-1 service. You can use this to create voice agents for your applications."
    text_to_speech(sample_text, "welcome_message.wav")

4.2 Run Your Script

Save your file and run it from the command line:

python tts_demo.py

4.3 Listen to Your Generated Voice

After running the script, you should see a message that the audio was saved. Open the generated WAV file with any media player to hear the synthesized voice.

5. Customize Your Voice Agent

5.1 Try Different Voices

AssemblyAI offers different voices. You can change the voice in your script by modifying the "voice" parameter:

data = {
    "text": text,
    "voice": "james"  # Try 'samantha', 'james', 'lisa', 'michael', 'sarah', 'david'
}

5.2 Generate Longer Texts

The TTS-1 service can handle longer texts. Try changing your sample text to something longer:

sample_text = "Welcome to our voice agent system. This is a demonstration of how easy it is to create natural-sounding speech from text. AssemblyAI's TTS-1 service costs only $15 per 1 million characters, making it affordable for developers to build voice applications."

5.3 Add Error Handling

For production use, add better error handling:

def text_to_speech(text, output_file='output.wav'):
    try:
        url = "https://api.assemblyai.com/v1/tts"
        
        headers = {
            "authorization": API_KEY,
            "content-type": "application/json"
        }
        
        data = {
            "text": text,
            "voice": "samantha"
        }
        
        response = requests.post(url, json=data, headers=headers)
        
        if response.status_code == 200:
            with open(output_file, 'wb') as f:
                f.write(response.content)
            print(f"Audio saved as {output_file}")
        else:
            print(f"Error: {response.status_code} - {response.text}")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

Summary

In this tutorial, you've learned how to set up and use AssemblyAI's TTS-1 text-to-speech API. You created a Python script that can convert text into natural-sounding speech, tested different voices, and learned how to handle errors. This is just the beginning - you can now use this technology to build voice agents, chatbots, or any application that needs synthesized speech. Remember that the TTS-1 service costs $15 per 1 million characters, which makes it very affordable for most developers to experiment with voice technology.

The key benefits of this approach are that it's simple to implement, supports multiple languages, and doesn't require complex audio processing knowledge. As you continue to work with this technology, you can explore advanced features like custom voices, different audio formats, and integration with other voice assistant platforms.

Source: MarkTechPost

Related Articles