Inworld’s platform empowers developers to build and deploy real-time, massively scalable AI-driven consumer applications. This quickstart focuses specifically on how to get started with the Text-to-Speech API, walking you through your first request to generate high-quality, ultra-realistic speech from text.

Make your first TTS API request

1

Create an API key

Create an Inworld account.In Inworld Portal, generate a Runtime API key, by clicking the Get API Key shortcut in the Overview tab, or going to Settings > API Keys. Copy the Base64 credentials.Set your API key as an environment variable.
export INWORLD_API_KEY='your-base64-api-key-here'
2

Prepare your first request

For Python or JavaScript, create a new file called inworld_quickstart.py or inworld_quickstart.js. Copy the corresponding code into the file. For a curl request, copy the request.
import requests
import base64
import os

url = "https://api.inworld.ai/tts/v1/voice"

headers = {
    "Authorization": f"Basic {os.getenv('INWORLD_API_KEY')}",
    "Content-Type": "application/json"
}

payload = {
    "text": "What a wonderful day to be a text-to-speech model!",
    "voiceId": "Ashley",
    "modelId": "inworld-tts-1"
}

response = requests.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
audio_content = base64.b64decode(result['audioContent'])

with open("output.mp3", "wb") as f:
    f.write(audio_content)
For Python, you may also have to install requests if not already installed.
pip install requests
3

Run the code

Run the code for Python or JavaScript, or enter the curl command into your terminal.
python inworld_quickstart.py
You should see a saved file called output.mp3. You can play this file with any audio player.

Stream your audio output

Now that you’ve made your first TTS API request, you can try streaming responses as well. Assuming you’ve already followed the instructions above to set up your API key:
1

Prepare your streaming request

Create a new file called inworld_stream_quickstart.py for Python or inworld_stream_quickstart.js for Javascript, and copy the corresponding code into the file.For this streaming example, we’ll use Linear PCM format (instead of MP3), which we specify in the audio_config.
import requests
import base64
import os
import json
import wave
import io

url = "https://api.inworld.ai/tts/v1/voice:stream"

headers = {
    "Authorization": f"Basic {os.getenv('INWORLD_API_KEY')}",
    "Content-Type": "application/json"
}

payload = {
    "text": "What a wonderful day to be a text-to-speech model! I'm super excited to show you how streaming works! It makes it so much easier to generate audio for realtime applications.",
    "voiceId": "Ashley",
    "modelId": "inworld-tts-1",
    "audio_config": {
        "audio_encoding": "LINEAR16",
        "sample_rate_hertz": 48000,
    },
}

response = requests.post(url, json=payload, headers=headers, stream=True)
response.raise_for_status()

raw_audio_data = io.BytesIO()

for line in response.iter_lines():
    chunk = json.loads(line)
    audio_chunk = base64.b64decode(chunk["result"]["audioContent"])
    if len(audio_chunk) > 44: # skip the wav header
        audio_data = audio_chunk[44:]
        raw_audio_data.write(audio_data)
        print(f"Appended {len(audio_data)} bytes to ouput_stream.wav")

with wave.open("ouput_stream.wav", "wb") as wf:
    wf.setnchannels(1)
    wf.setsampwidth(2)
    wf.setframerate(payload["audio_config"]["sample_rate_hertz"])
    wf.writeframes(raw_audio_data.getvalue())

print("Audio file completed!")
2

Run the code

Run the code for Python or JavaScript. The console will print out as streamed bytes are written to the audio file.
python inworld_stream_quickstart.py
You should see a saved file called output_stream.wav. You can play this file with any audio player.

Next Steps

Now that you’ve tried out Inworld’s TTS API, you can explore more of Inworld’s TTS capabilities.