DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

Build a Voicemail Generator with ElevenLabs API

Why a Voicemail Generator?

Ever wanted to give your phone system a personal touch without spending hours in a recording studio? With modern text‑to‑speech (TTS) services you can generate natural‑sounding voicemails on the fly. In this tutorial we’ll walk through building a voicemail generator that takes a piece of text (or a template) and produces an audio file ready to be dropped into any telephony platform (Twilio, Plivo, etc.). The star of the show is the ElevenLabs API, a powerful TTS engine that supports voice cloning and expressive speech.

TL;DR – By the end of this article you’ll have a small Python script (and a curl example) that calls ElevenLabs, saves an MP3, and can be hooked into a voicemail workflow.


What Makes ElevenLabs a Good Fit?

ElevenLabs offers:

  • High‑quality neural voices that sound surprisingly human.
  • Voice cloning – upload a few minutes of your own voice and let the API speak for you.
  • Simple REST endpoints that work with any language that can make HTTP calls.

If you’re ready to try it, sign up through this affiliate link: https://try.elevenlabs.io/kr07zfuqn1bp. It’ll give you a free credit tier and a personal API key.


Prerequisites

Item Reason
Python 3.8+ We'll use requests for the HTTP calls.
ffmpeg (optional) Convert the returned MP3 to WAV if your telephony provider needs it.
An ElevenLabs account To obtain an API key.
A telephony sandbox (e.g., Twilio trial) To test the generated voicemail.

Step 1 – Get Your API Key

  1. Sign up at the link above.
  2. Navigate to Dashboard → API Keys.
  3. Create a new key and copy the string – you’ll need it in the code.

Security tip: Store the key in an environment variable (ELEVENLABS_API_KEY) instead of hard‑coding it.


Step 2 – Choose a Voice

ElevenLabs ships with several pre‑made voices (e.g., “Rachel”, “Elli”). You can also upload your own voice clone. For this demo we’ll use the default voice ID EXAVITQu4vr4xnSDxMaL (the “Rachel” voice). You can fetch the list of voices with:

curl -H "xi-api-key: $ELEVENLABS_API_KEY" \
     https://api.elevenlabs.io/v1/voices
Enter fullscreen mode Exit fullscreen mode

Copy the voice_id you like and keep it handy.


Step 3 – The Core Python Script

Below is a minimal script that:

  1. Sends a text payload to ElevenLabs.
  2. Saves the returned audio as voicemail.mp3.
  3. (Optional) Converts it to WAV for telephony compatibility.
#!/usr/bin/env python3
import os
import requests
import subprocess

# -------------------------------------------------
# Configuration – keep these in env vars for safety
# -------------------------------------------------
API_KEY = os.getenv("ELEVENLABS_API_KEY")
VOICE_ID = os.getenv("ELEVENLABS_VOICE_ID", "EXAVITQu4vr4xnSDxMaL")
OUTPUT_MP3 = "voicemail.mp3"
OUTPUT_WAV = "voicemail.wav"

# -------------------------------------------------
# Helper: call ElevenLabs TTS endpoint
# -------------------------------------------------
def generate_voicemail(text: str) -> bytes:
    url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
    headers = {
        "xi-api-key": API_KEY,
        "Content-Type": "application/json"
    }
    payload = {
        "text": text,
        "model_id": "eleven_monolingual_v1",
        "voice_settings": {
            "stability": 0.75,
            "similarity_boost": 0.85
        }
    }
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    return response.content  # MP3 bytes

# -------------------------------------------------
# Main – generate and store the audio
# -------------------------------------------------
if __name__ == "__main__":
    if not API_KEY:
        raise RuntimeError("Set ELEVENLABS_API_KEY environment variable")

    # Example voicemail text – you could pull this from a DB or template engine
    voicemail_text = (
        "Hi, this is Alex from Acme Corp. I’m currently away from my desk, "
        "but I’ll get back to you as soon as possible. Please leave a message "
        "after the tone, or email me at alex@example.com. Thanks!"
    )

    print("Generating voicemail audio...")
    audio_mp3 = generate_voicemail(voicemail_text)

    # Save MP3
    with open(OUTPUT_MP3, "wb") as f:
        f.write(audio_mp3)
    print(f"Saved MP3 to {OUTPUT_MP3}")

    # Optional: convert to WAV (many IVR systems prefer WAV PCM)
    if shutil.which("ffmpeg"):
        subprocess.run([
            "ffmpeg", "-y", "-i", OUTPUT_MP3,
            "-ar", "8000", "-ac", "1", OUTPUT_WAV
        ], check=True)
        print(f"Converted to WAV: {OUTPUT_WAV}")
    else:
        print("ffmpeg not found – skipping WAV conversion")
Enter fullscreen mode Exit fullscreen mode

What’s happening?

  • The model_id eleven_monolingual_v1 is the default high‑quality English model.
  • stability controls how consistent the voice sounds; similarity_boost nudges the output toward the chosen voice’s characteristics.
  • The script writes the MP3 directly to disk; you can stream it to a telephony API instead of saving.

Step 4 – Curl Alternative (Great for Quick Tests)

If you just want to verify the endpoint, fire off a curl request:

curl -X POST "https://api.elevenlabs.io/v1/text-to-speech/EXAVITQu4vr4xnSDxMaL" \
     -H "xi-api-key: $ELEVENLABS_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{
           "text": "Your voicemail message goes here.",
           "model_id": "eleven_monolingual_v1",
           "voice_settings": { "stability": 0.7, "similarity_boost": 0.9 }
         }' \
     --output voicemail.mp3
Enter fullscreen mode Exit fullscreen mode

Replace the text field with whatever you need. The file voicemail.mp3 will be created in the current directory.


Step 5 – Hooking Into a Telephony Provider

Using Twilio

Twilio’s <Play> verb can serve an MP3 directly from a public URL. The simplest approach is to upload the generated file to a static bucket (e.g., AWS S3) and point Twilio at it.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Play>https://your-bucket.s3.amazonaws.com/voicemail.mp3</Play>
</Response>
Enter fullscreen mode Exit fullscreen mode

If you prefer to keep everything server‑side, you can return the MP3 as a binary response from a Flask endpoint:

from flask import Flask, send_file
app = Flask(__name__)

@app.route("/voicemail")
def serve_voicemail():
    return send_file("voicemail.mp3", mimetype="audio/mpeg")
Enter fullscreen mode Exit fullscreen mode

Configure your Twilio webhook to point to https://your-domain.com/voicemail. When a call hits the webhook, Twilio will fetch the audio and play it as the caller’s voicemail greeting.

Using Plivo or Nexmo

Both platforms accept a url parameter in their “Speak” or “Play” actions, so the workflow is identical—just host the MP3 somewhere reachable.


Step 6 – Going Further

Idea How to implement
Dynamic templates Use Jinja2 (Python) or Handlebars (JS) to fill placeholders like {caller_name} before sending to ElevenLabs.
Voice cloning Record 5‑10 minutes of your own voice, upload via the ElevenLabs dashboard, and replace VOICE_ID with the new clone ID.
Multilingual voicemails Switch model_id to eleven_multilingual_v1 and send text in the target language.
Batch generation Loop over a list of contacts, generate personalized greetings, and store them in a CDN for instant playback.

Common Pitfalls & Debugging Tips

Symptom Likely cause Fix
403 Unauthorized API key missing or malformed Verify ELEVENLABS_API_KEY env var and that it’s passed as xi-api-key.
422 Unprocessable Entity Text too long (> 5000 chars) or invalid JSON Trim the message or split into multiple calls.
Audio is garbled ffmpeg conversion parameters wrong for your telephony provider Check provider docs – most expect 8 kHz, mono PCM.
Rate‑limit errors Exceeding free tier limits Upgrade the plan or add a simple retry/back‑off loop.

Wrapping Up

You now have a functional voicemail generator powered by ElevenLabs. The core idea is simple: feed a string to the TTS endpoint, grab the MP3, and hand it off to your phone system. From here you can experiment with voice clones, multilingual greetings, or even real‑time generation during a call.

Ready to give your callers a more human experience? Try ElevenLabs today via this link: https://try.elevenlabs.io/kr07zfuqn1bp. The free tier lets you experiment without any upfront cost, and the API’s quality will make your voicemail sound like a real person—not a robot. Happy coding!

Top comments (0)