ai newsDi., 20. Okt.
EilmeldungTrump administration orders ban on DeepSeek in federal agencies

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,

dev.toDienstag 02:55 · 20.10.6 Min. Lesezeit
Bild: dev.to

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.

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

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

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.

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.

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.

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

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.

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

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.

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:

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.

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!

Quelle: dev.to (dev.to). Original öffnen