Quickstart: generate speech

This tutorial will show you how to generate speech, save the bytes as a file, then play that file through your computer's speakers. To play audio continually as it's generated, see Stream speech.

Get an API key

Get an API key

First, you'll need a Mistral API key. If you don't have one, then go to Studio. Navigate to the API keys section and create a new API key.

Store your API key in the MISTRAL_API_KEY environment variable:

export MISTRAL_API_KEY="your-api-key"
Install the SDK

Install the SDK

To use Python or TypeScript, install the Mistral SDK, plus a library to play audio. The curl example uses jq to decode base64-encoded bytes.

pip install mistralai sounddevice soundfile
Generate your audio

Generate your audio

Use this code to turn text into speech. The API returns the audio in base64 encoding, so this example decodes it into bytes. The curl example saves the audio as hello.wav, the Python example plays it right away, and the TypeScript example saves it, then plays it.

import base64
import io
import os

import sounddevice as sd
import soundfile as sf

from mistralai.client import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

response = client.audio.speech.complete(
    model="voxtral-mini-tts-3-0",
    input="Hi there! You are listening to Voxtral.",
    voice_id="gb_jane_neutral",
    response_format="wav"
)

bytes = io.BytesIO(base64.b64decode(response.audio_data))
audio, sample_rate = sf.read(bytes)
sd.play(audio, sample_rate)
sd.wait()
You should hear

You should hear

The Python and TypeScript examples play the audio through your speakers.

The curl example saves hello.wav in the current directory. Open it in any audio player, or play it from the terminal:

afplay hello.wav   # macOS
aplay hello.wav    # Linux

You'll hear the gb_jane_neutral voice say "Hello! This is Voxtral, Mistral's text-to-speech model."

Going further

Going further