Dev.to AI 🤖 Ai 👁 0 📖 6 min read

Build a Voicemail Generator with ElevenLabs API

Why Build a Voicemail Generator? Ever been stuck trying to leave a voicemail for a client, only to realize you don’t have the time or the right voice to get the message across? In a world where automation is king, a pr

Why Build a Voicemail Generator?

Ever been stuck trying to leave a voicemail for a client, only to realize you don’t have the time or the right voice to get the message across? In a world where automation is king, a programmable voicemail generator lets you deliver personalized audio messages at scale—without ever picking up the phone. Whether you’re building a customer‑support bot, a marketing outreach tool, or a reminder system for a healthcare app, a voice‑based interface feels more human and engaging than plain text.

Voice AI makes this possible. Instead of recording dozens of variations manually, you can generate a high‑quality voice file on demand, customize tone, pacing, and even clone a specific voice that matches your brand. The best part? You don’t need a deep background in signal processing—just a few lines of code and an API key.

Enter ElevenLabs

When it comes to text‑to‑speech (TTS) APIs that combine ease of use with studio‑grade audio quality, ElevenLabs is a standout choice. Their platform offers:

  • High‑fidelity neural voices that sound almost indistinguishable from a real person.
  • Voice cloning—upload a short sample and replicate the exact timbre and inflection.
  • Fine‑tuned controls for pitch, speaking rate, and emphasis.
  • A generous free tier and straightforward pricing for production workloads.

If you’re ready to start building, you can sign up right here: https://try.elevenlabs.io/kr07zfuqn1bp

Architecture Overview

Below is a minimal stack to turn a text prompt into a downloadable voicemail:

  1. Trigger – An HTTP endpoint, cron job, or message queue that receives the text and recipient details.
  2. Text‑to‑Speech – Call ElevenLabs to synthesize the audio.
  3. Storage – Store the MP3 in S3 (or any bucket) and keep a reference in a database.
  4. Delivery – Either send the file as an attachment via email or embed it in a voice‑enabled phone call.
[Trigger] → [ElevenLabs API] → [S3] → [Email/Phone Call]

Step‑by‑Step Tutorial (Python)

Below, we’ll walk through a lightweight Flask app that accepts a JSON payload, generates a voicemail, and returns a URL to the stored MP3. You can adapt this to any framework or language.

1. Get Your API Key

After signing up at https://try.elevenlabs.io/kr07zfuqn1bp, navigate to the dashboard and copy your API key. Keep it secret—store it in an environment variable or a secrets manager.

2. Set Up the Project

mkdir voicemail-gen
cd voicemail-gen
python -m venv venv
source venv/bin/activate
pip install flask requests boto3 python-dotenv

Create a .env file:

ELEVENLABS_API_KEY=sk_your_secret_key
AWS_ACCESS_KEY_ID=YOUR_AWS_KEY
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET
AWS_S3_BUCKET=voicemail-bucket

3. Code

# app.py
import os
import uuid
import boto3
import requests
from flask import Flask, request, jsonify
from dotenv import load_dotenv

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
S3_BUCKET = os.getenv("AWS_S3_BUCKET")

s3 = boto3.client(
    "s3",
    aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
)

app = Flask(__name__)

ELEVENLABS_URL = "https://api.elevenlabs.io/v1/text-to-speech"

@app.route("/voicemail", methods=["POST"])
def voicemail():
    data = request.json
    text = data.get("text")
    voice_id = data.get("voice_id", "Rachel")  # Default voice
    if not text:
        return jsonify({"error": "Missing 'text'"}), 400

    # 1. Generate audio
    payload = {
        "model_id": "eleven_monolingual_v1",
        "voice_settings": {
            "stability": 0.75,
            "similarity_boost": 0.85,
        },
        "text": text,
    }
    headers = {
        "xi-api-key": ELEVENLABS_API_KEY,
        "Content-Type": "application/json",
    }

    resp = requests.post(f"{ELEVENLABS_URL}/{voice_id}", json=payload, headers=headers)
    if resp.status_code != 200:
        return jsonify({"error": "ElevenLabs failed", "details": resp.text}), 502

    audio_bytes = resp.content

    # 2. Store in S3
    filename = f"voicemail_{uuid.uuid4()}.mp3"
    s3.put_object(Bucket=S3_BUCKET, Key=filename, Body=audio_bytes, ContentType="audio/mpeg")

    url = f"https://{S3_BUCKET}.s3.amazonaws.com/{filename}"
    return jsonify({"url": url, "duration_ms": resp.json().get("duration")})

if __name__ == "__main__":
    app.run(debug=True)

How It Works

  1. Request – The endpoint expects a JSON body with text and optional voice_id.
  2. ElevenLabs Call – We post to https://api.elevenlabs.io/v1/text-to-speech/{voice_id} with the text and voice settings.
  3. S3 Upload – The raw MP3 bytes are stored in an S3 bucket, and the public URL is returned.

4. Test It Out

curl -X POST http://localhost:5000/voicemail \
  -H "Content-Type: application/json" \
  -d '{"text":"Hello, this is your friendly reminder that your appointment is tomorrow at 3 PM. Please let us know if you need to reschedule.", "voice_id":"Rachel"}'

You should receive a JSON response with a public URL pointing to your new voicemail.

JavaScript / Node.js Alternative

If you prefer JavaScript, here’s a quick example using node-fetch:

// voicemail.js
import fetch from "node-fetch";
import { v4 as uuidv4 } from "uuid";

const ELEVENLABS_KEY = process.env.ELEVENLABS_API_KEY;
const ELEVENLABS_URL = "https://api.elevenlabs.io/v1/text-to-speech";

async function generateVoicemail(text, voiceId = "Rachel") {
  const response = await fetch(`${ELEVENLABS_URL}/${voiceId}`, {
    method: "POST",
    headers: {
      "xi-api-key": ELEVENLABS_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model_id: "eleven_monolingual_v1",
      voice_settings: {
        stability: 0.75,
        similarity_boost: 0.85,
      },
      text,
    }),
  });

  if (!response.ok) throw new Error("ElevenLabs error: " + response.statusText);
  const audioBuffer = await response.arrayBuffer();
  // Store audioBuffer in S3 or send it directly
  return audioBuffer;
}

Fine‑Tuning Voice Parameters

ElevenLabs lets you tweak a few knobs:

Parameter Range Effect
stability 0–1 Higher = smoother, lower = more natural “human” variations
similarity_boost 0–1 Higher = closer to the chosen voice’s style
pitch -10 to +10 Shift the perceived age/femininity
tempo 0.5–2.0 Slow down or speed up the speech rate

Play around with these in the payload to match your brand voice. For example, a sales bot might use a higher pitch and moderate tempo to sound enthusiastic.

Voice Cloning – Bring Your Own Voice

If your company already has a signature voice (e.g., a CEO or a beloved support rep), ElevenLabs’ voice cloning lets you replicate it. The process is simple:

  1. Record a 60‑second sample in a quiet environment.
  2. Upload it through the dashboard or via the /voices API endpoint.
  3. Use the returned voice_id in subsequent TTS requests.

Cloned voices are perfect for automated callbacks or personalized greetings that still feel “human.”

Best Practices

Tip Why It Matters
Cache the MP3 Reduce API calls and latency. Store generated voicemails in a cache layer (Redis, S3).
Respect Rate Limits ElevenLabs enforces per‑second limits. Queue requests or implement back‑off logic.
Error Handling Always check the status_code and response.body before proceeding.
Security Never expose your API key in client‑side code. Use server‑side proxies or serverless functions.
Compliance If you’re sending voicemails in regulated industries, ensure you have consent and proper data handling.

Scaling for Production

When you move beyond a single Flask instance:

  • Use a task queue (Celery, RabbitMQ, or Cloud Tasks) to offload TTS jobs.
  • Serve via CDN – Store MP3s in an S3 bucket with CloudFront for low‑latency playback.
  • Monitor usage – Track API calls, costs, and latency in Grafana or CloudWatch.
  • Implement retry logic – Network hiccups happen; exponential back‑off can save you from failing voicemails.

Other Use Cases

  • Appointment Reminders – Automatically generate personalized reminders for doctors, lawyers, or fitness trainers.
  • Customer Support – Route missed calls to a bot that reads the last ticket update.
  • Marketing Campaigns – Deliver voice‑rich newsletters or product demos.
  • Accessibility – Convert text content into audio for visually impaired users.

Wrap‑Up

Building a voicemail generator with ElevenLabs is surprisingly straightforward. A handful of API calls, a storage bucket, and a bit of logic, and you have a scalable, human‑like voice system that can be tailored to your brand’s tone.

Ready to try it out? Sign up for ElevenLabs today and start creating voicemails that sound as good as they feel. https://try.elevenlabs.io/kr07zfuqn1bp

Happy coding!

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.