DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

Build a Voicemail Generator with ElevenLabs API

Why a Voicemail Generator?

Ever needed a quick, professional‑sounding voicemail for a product demo, a marketing campaign, or just for fun? Manually recording each line can be tedious, especially when you need multiple languages or a consistent brand voice. With modern text‑to‑speech (TTS) services, you can generate high‑quality audio on the fly. In this post I’ll walk you through building a voicemail generator that takes a text template, plugs in dynamic variables (like a name or appointment time), and spits out an MP3 ready to upload to any phone system.

The secret sauce? ElevenLabs – a state‑of‑the‑art voice AI platform that offers realistic speech synthesis and voice cloning. You can sign up for free and start experimenting right away: https://try.elevenlabs.io/kr07zfuqn1bp.


What You’ll Need

Item Reason
ElevenLabs API key Access to the TTS endpoint and optional voice cloning.
Python 3.8+ (or Node.js) To make HTTP requests and stitch audio together.
ffmpeg (optional) Combine multiple clips into a single voicemail file.
A text template The script you want to turn into a voicemail.

If you’re comfortable with curl you can skip the language‑specific sections, but I’ll include both Python and JavaScript snippets for completeness.


Getting Your ElevenLabs API Key

  1. Head over to the affiliate signup page: https://try.elevenlabs.io/kr07zfuqn1bp.
  2. Create an account (the free tier gives you a generous number of characters per month).
  3. In the dashboard, click API Keys → New Key. Copy the generated token – you’ll need it for every request.

Tip: Store the key in an environment variable (ELEVENLABS_API_KEY) instead of hard‑coding it. This keeps your code portable and secure.


Crafting a Simple Voicemail Template

Let’s start with a classic “appointment reminder” script:

Hi {{name}}, this is a reminder from {{company}}.
Your appointment is scheduled for {{date}} at {{time}}.
If you need to reschedule, please call us at {{phone}}.
Thanks, and see you soon!
Enter fullscreen mode Exit fullscreen mode

We’ll replace the placeholders with actual values at runtime. In Python we can use str.format, and in JavaScript we can use template literals.


Python Implementation

1. Install Dependencies

pip install requests pydub
# pydub requires ffmpeg – make sure it’s in your PATH
Enter fullscreen mode Exit fullscreen mode

2. Helper to Call ElevenLabs

import os
import requests

ELEVEN_API = os.getenv("ELEVENLABS_API_KEY")
BASE_URL = "https://api.elevenlabs.io/v1"

def synthesize(text, voice_id="EXAVITQu4vr4xnSDxMaL"):
    """Send a TTS request to ElevenLabs and return raw audio bytes."""
    url = f"{BASE_URL}/text-to-speech/{voice_id}"
    headers = {
        "xi-api-key": ELEVEN_API,
        "Content-Type": "application/json"
    }
    payload = {
        "text": text,
        "model_id": "eleven_monolingual_v1",   # default high‑quality model
        "voice_settings": {"stability": 0.75, "similarity_boost": 0.8}
    }
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    return response.content
Enter fullscreen mode Exit fullscreen mode

Note: The voice_id above points to the default “Rachel” voice. If you’ve cloned a custom voice, replace it with the ID you receive from the cloning endpoint.

3. Build the Voicemail

from pydub import AudioSegment
from io import BytesIO

def build_voicemail(data):
    # 1️⃣ Fill the template
    template = (
        "Hi {name}, this is a reminder from {company}. "
        "Your appointment is scheduled for {date} at {time}. "
        "If you need to reschedule, please call us at {phone}. "
        "Thanks, and see you soon!"
    )
    filled = template.format(**data)

    # 2️⃣ Synthesize the whole paragraph (ElevenLabs handles punctuation well)
    audio_bytes = synthesize(filled)

    # 3️⃣ Convert to an AudioSegment for optional post‑processing
    audio = AudioSegment.from_file(BytesIO(audio_bytes), format="mp3")
    # Example: add a 1‑second fade‑in/out for a smoother start/end
    audio = audio.fade_in(1000).fade_out(1000)

    # 4️⃣ Export to disk
    output_path = f"voicemail_{data['name'].replace(' ', '_')}.mp3"
    audio.export(output_path, format="mp3")
    print(f"✅ Voicemail saved to {output_path}")

# Example usage
if __name__ == "__main__":
    payload = {
        "name": "Alex Johnson",
        "company": "Acme Dental",
        "date": "Monday, June 24th",
        "time": "2:30 PM",
        "phone": "555‑123‑4567"
    }
    build_voicemail(payload)
Enter fullscreen mode Exit fullscreen mode

Running the script will produce an MP3 file that sounds like a real person speaking your reminder.


JavaScript (Node) Version

If you prefer Node, the same flow can be achieved with node-fetch and fluent-ffmpeg.

npm install node-fetch@2 fluent-ffmpeg
# Make sure ffmpeg is installed on your system
Enter fullscreen mode Exit fullscreen mode
require('dotenv').config();
const fetch = require('node-fetch');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');

const ELEVEN_API = process.env.ELEVENLABS_API_KEY;
const BASE_URL = 'https://api.elevenlabs.io/v1';

async function synthesize(text, voiceId = 'EXAVITQu4vr4xnSDxMaL') {
  const url = `${BASE_URL}/text-to-speech/${voiceId}`;
  const payload = {
    text,
    model_id: 'eleven_monolingual_v1',
    voice_settings: { stability: 0.75, similarity_boost: 0.8 }
  };

  const res = await fetch(url, {
    method: 'POST',
    headers: {
      'xi-api-key': ELEVEN_API,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(payload)
  });

  if (!res.ok) throw new Error(`ElevenLabs error: ${res.statusText}`);
  return await res.buffer(); // raw MP3 bytes
}

async function buildVoicemail(data) {
  const template = `Hi ${data.name}, this is a reminder from ${data.company}. 
Your appointment is scheduled for ${data.date} at ${data.time}. 
If you need to reschedule, please call us at ${data.phone}. 
Thanks, and see you soon!`;

  const audioBuffer = await synthesize(template);
  const outPath = `voicemail_${data.name.replace(/\s+/g, '_')}.mp3`;

  // Write raw MP3 to disk (you could also pipe to ffmpeg for extra processing)
  fs.writeFileSync(outPath, audioBuffer);
  console.log(`✅ Voicemail saved to ${outPath}`);
}

// Example payload
const payload = {
  name: 'Maria Gomez',
  company: 'Bright Dental',
  date: 'Wednesday, July 5th',
  time: '10:00 AM',
  phone: '555‑987‑6543'
};

buildVoicemail(payload).catch(console.error);
Enter fullscreen mode Exit fullscreen mode

Both snippets illustrate the core idea: fill a template → call ElevenLabs → store the MP3. From here you can integrate the generator into a webhook, a CI pipeline, or a simple CLI tool.


Using Voice Cloning (Optional)

ElevenLabs also lets you upload a few seconds of a real voice and create a clone. The workflow is:

  1. POST the audio file to /v1/voices/add (requires the voice_id you want to create).
  2. Wait for the cloning job to finish (the response contains a voice_id).
  3. Use that voice_id in the synthesize call above.
def clone_voice(sample_path, name="MyClone"):
    url = f"{BASE_URL}/voices/add"
    headers = {"xi-api-key": ELEVEN_API}
    files = {"sample": open(sample_path, "rb")}
    data = {"name": name}
    res = requests.post(url, headers=headers, files=files, data=data)
    res.raise_for_status()
    return res.json()["voice_id"]
Enter fullscreen mode Exit fullscreen mode

Once you have a clone, your voicemail will sound exactly like the person you recorded, which is perfect for brand consistency.


Deploying as a Tiny API

If you want to expose the generator to other services, wrap it in a Flask (Python) or Express (Node) endpoint.

Flask Example

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

@app.route("/voicemail", methods=["POST"])
def voicemail():
    data = request.json  # expects same keys as our template
    audio_bytes = synthesize(
        template.format(**data)
    )
    return send_file(
        BytesIO(audio_bytes),
        mimetype="audio/mpeg",
        as_attachment=True,
        download_name="voicemail.mp3"
    )

if __name__ == "__main__":
    app.run(port=8080)
Enter fullscreen mode Exit fullscreen mode

Deploy this to Render, Fly.io, or any VPS, and you now have a RESTful voicemail generator that any CRM can call.


Wrapping Up

You’ve seen how a few lines of code, paired with ElevenLabs’ ultra‑realistic TTS engine, can turn plain text into a polished voicemail. The building blocks are:

  • Template rendering – fill placeholders with dynamic data.
  • API call – send the final string to ElevenLabs (/text-to-speech).
  • Audio post‑processing (optional) – fade‑ins, volume normalization, concatenation.
  • Delivery – save to disk, stream back via HTTP, or push to a telephony platform.

Because the ElevenLabs API returns high‑fidelity audio out of the box, you rarely need extra DSP tricks. Still, the flexibility to add ffmpeg steps means you can meet any production requirement.


Call to Action

Ready to give your apps a voice that sounds genuinely human? Grab your free ElevenLabs API key and start building today: https://try.elevenlabs.io/kr07zfuqn1bp. Happy coding!

Top comments (0)