# How to Add AI Voice to Your Django App

> Source: <https://dev.to/voice_developer/how-to-add-ai-voice-to-your-django-app-315o>
> Published: 2026-10-23 00:30:49+00:00

Voice has become the new “screen” for many users. From hands‑free navigation to accessibility, integrating AI‑generated speech can make your Django project feel more natural and inclusive. Whether you’re building a customer‑support chatbot, an audiobook generator, or a language‑learning platform, adding text‑to‑speech (TTS) is a quick way to boost engagement.

When you need a reliable, high‑quality TTS service that supports voice cloning, **ElevenLabs** is a popular choice. Their API offers dozens of realistic voices, fine‑grained control over speaking rate, pitch, and more—all wrapped in a developer‑friendly REST interface.

If you’re looking for a service that’s easy to set up and scales with your Django app, try ElevenLabs: [https://try.elevenlabs.io/kr07zfuqn1bp](https://try.elevenlabs.io/kr07zfuqn1bp)

`.env` file:

```
ELEVENLABS_API_KEY=sk_your_key_here
```

While you can use `curl` or any HTTP library, Django developers often prefer `httpx` for async support.

```
pip install httpx
```

Create a new module, e.g., `voice.py`, and add:

``` python
import os
import httpx
from django.conf import settings

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

async def synthesize(text: str, voice_id: str = "Bella", output_path: str = None):
    """
    Convert text to speech using ElevenLabs.
    """
    headers = {
        "accept": "audio/mpeg",
        "xi-api-key": settings.ELEVENLABS_API_KEY,
        "Content-Type": "application/json",
    }

    payload = {
        "text": text,
        "voice_settings": {
            "stability": 0.5,
            "similarity_boost": 0.75,
        },
    }

    async with httpx.AsyncClient() as client:
        resp = await client.post(
            f"{ELEVENLABS_API_URL}/{voice_id}", json=payload, headers=headers
        )
        resp.raise_for_status()

    audio_bytes = resp.content

    if output_path:
        with open(output_path, "wb") as f:
            f.write(audio_bytes)
    return audio_bytes
```

The `voice_id` parameter can be any voice you’ve enabled in your ElevenLabs dashboard. “Bella” is a default, but you can list all voices with another endpoint if you need to present options to users.

Here’s a quick example of an endpoint that returns an audio file directly:

``` python
from django.http import HttpResponse
from .voice import synthesize

async def speak(request):
    """
    API endpoint: /speak/?text=Hello%20world
    Returns an MP3 file generated by ElevenLabs.
    """
    text = request.GET.get("text", "Hello, world!")
    audio = await synthesize(text, voice_id="Bella")
    return HttpResponse(audio, content_type="audio/mpeg")
```

Add the URL in `urls.py`:

``` python
from django.urls import path
from .views import speak

urlpatterns = [
    path("speak/", speak, name="speak"),
]
```

Now, visiting `/speak/?text=Good%20morning%20everyone` will stream a polished voice directly in the browser.

If you want your users to speak in a personalized voice, ElevenLabs supports cloning with just a few samples. The process:

`voice_id` you’ll use in subsequent requests.
`synthesize` function.

``` python
# Example: cloning a user’s voice
async def clone_voice(uploaded_audio):
    headers = {"xi-api-key": settings.ELEVENLABS_API_KEY}
    files = {"audio_file": ("sample.wav", uploaded_audio, "audio/wav")}

    async with httpx.AsyncClient() as client:
        resp = await client.post(
            "https://api.elevenlabs.io/v1/voices",
            headers=headers,
            files=files,
        )
        resp.raise_for_status()
        return resp.json()["voice_id"]
```

You can expose this as a Django view that accepts file uploads. Once the voice is cloned, store the `voice_id` in your user model for later use.

For real‑time applications (e.g., a chatbot that speaks instantly), you’ll want to stream the audio rather than buffering the whole file. ElevenLabs supports streaming via WebSockets or by returning raw `audio/mpeg` chunks. In Django, you can use `StreamingHttpResponse`:

``` python
from django.http import StreamingHttpResponse

async def stream_speak(request):
    text = request.GET.get("text", "Hello")
    async def generator():
        # Simulate streaming by yielding chunks
        audio = await synthesize(text, voice_id="Bella")
        yield audio

    return StreamingHttpResponse(generator(), content_type="audio/mpeg")
```

For production deployments, consider:

`curl` Example
If you’re debugging or want to try it from the terminal:

```
curl -X POST "https://api.elevenlabs.io/v1/text-to-speech/Bella" \
     -H "Content-Type: application/json" \
     -H "xi-api-key: $ELEVENLABS_API_KEY" \
     -d '{"text":"Hello from ElevenLabs!","voice_settings":{"stability":0.5,"similarity_boost":0.75}}' \
     --output hello.mp3
```

Open `hello.mp3` to hear the result.

Adding AI voice to a Django app is surprisingly straightforward:

By following the snippets above, you’ll have a functional TTS endpoint in minutes, ready to be integrated into your UI, API, or background workers.

Ready to give your Django app a voice? Dive into ElevenLabs today and transform text into natural, expressive speech: [https://try.elevenlabs.io/kr07zfuqn1bp](https://try.elevenlabs.io/kr07zfuqn1bp)

Happy coding!
