How to convert text to speech with an API
Generate spoken audio from text with a TTS model.
Call the speech endpoint
Send text and a voice to the audio speech endpoint and save the returned audio file.
Example
from openai import OpenAI
client = OpenAI(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")
audio = client.audio.speech.create(model="gpt-4o-mini-tts", voice="alloy", input="Hello from TokenRex")
audio.write_to_file("hello.mp3")Questions
- Do I need a separate key for each provider?
- No. One TokenRex API key works for every enabled model; change only the model ID.
Related
- How to use the OpenAI SDK with a custom base URL
- How to call Claude with the OpenAI SDK
- How to call Gemini with the OpenAI SDK
- How to use the Anthropic Messages API on TokenRex
- How to stream LLM responses
- How to use function calling (tools) with an LLM API
- How to send images to a vision model
- How to generate images with an API