Convert text to speech audio using the OpenDunes API from Go.
The text-to-speech endpoint converts written text into natural-sounding audio. Voices and models that support Arabic and French are available for Algerian and broader North African deployments.
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func main() {
body, _ := json.Marshal(map[string]any{
"model": "x-ai/grok-voice-tts-1.0",
"input": "مرحبا! كيف يمكنني مساعدتك اليوم؟",
"voice": "alloy",
})
req, _ := http.NewRequest("POST", "https://opendunes.com/v1/audio/speech", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENDUNES_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
out, _ := os.Create("output.mp3")
defer out.Close()
io.Copy(out, resp.Body)
}The response body is the raw audio stream. Write it directly to a file or buffer for playback.
| Parameter | Description |
|---|---|
model | TTS model slug |
input | Text to synthesize (max 4096 characters) |
voice | Voice name (model-dependent) |
response_format | mp3 (default), opus, aac, flac |
speed | Playback rate multiplier (0.25–4.0) |
TTS is billed per 1,000 characters of input at the model's DA rate.