Loading API documentation
Kotoba TTS turns Japanese text into speech over a WebSocket connection. Audio chunks stream back while the speech is generated, so playback can start within a fraction of a second.
Endpoint
wss://fal.run/Kotoba-Technologies/kotoba-tts/v2/tts/wsThe Playground offers a one-shot demo (POST /v2/tts) that returns one WAV file. It is bounded for demos. Production integrations use the WebSocket endpoint described below.
Send JSON messages over the WebSocket. The first message must be open, sent within 10 seconds of connecting. The server answers with session.created, which echoes the applied settings.
{"type": "open", "language": "ja", "speaker_id": "female-01", "format": "pcm16", "sample_rate": 24000}| Field | Type and values | Default | Behavior |
|---|---|---|---|
language | string: "ja" | "ja" | Language of the text. |
speaker_id | string: a preset voice id, for example "male-01" or "female-01" | "male-01" | The voice. An unknown id returns an unknown_speaker_id error. |
format | string: "float32", "pcm16", "mulaw" or "opus" | "float32" | Encoding of the audio chunks. |
sample_rate | integer: 8000, 16000 or 24000 | 24000 | Output sample rate. mulaw is always 8000 and opus is always 24000. |
voice_settings | object, see below | all defaults | Speed, pitch and volume for every response of the session. |
voice_settings fields; an omitted field keeps its default:
| Field | Type and range | Default |
|---|---|---|
speed | number, 0.7 to 1.5 | 1.0 |
pitch | number, -4 to 4 semitones | 0 |
volume | number, 0 to 2.0 | 1.0 |
Audio is mono. float32 and pcm16 are raw little-endian samples; opus is an Ogg/Opus stream.
{"type": "response.create", "text": "おはようございます。今日は水曜日です。"}| Field | Type and values | Default | Behavior |
|---|---|---|---|
text | string | required | Text to speak. Must not be empty. |
response_id | string | a generated id | Returned on every event of this response. |
voice_settings | object, as above | the session's | Overrides the session values for this response, field by field. |
One response runs at a time: send the next response.create after response.done. A response.create sent while a response is running returns an error and is not queued. {"type": "response.cancel"} stops the current response.
A response stops after about two minutes of audio, and its response.done then carries "finish_reason": "length". Send long text as several responses, for example one per paragraph.
Field values below are illustrative.
{"type": "session.created", "format": "pcm16", "sample_rate": 24000, "channels": 1, "language": "ja", "speaker_id": "female-01"}
{"type": "response.created", "response": {"id": "r-1"}}
{"type": "audio.chunk", "response_id": "r-1", "audio": "<base64 audio>", "isFinal": false}
{"type": "audio.chunk", "response_id": "r-1", "audio": "<base64 audio>", "isFinal": true}
{"type": "response.done", "response": {"id": "r-1", "status": "completed"}}audio field and play or append it in order.response.done ends a response. Its status is completed, cancelled or failed.{"type": "error", "code": "invalid_request", "message": "...", "fatal": false, "response_id": "..."}. Fix the request and send it again."fatal": true ends the session, and the server closes the connection.error with "code": "over_max_concurrency" and closes the connection with code 1013. Retry with backoff.GET https://fal.run/Kotoba-Technologies/kotoba-tts/model_and_cuda_availability (same Authorization: Key header) or allow for it in the first connection's timeout.Authenticate the WebSocket upgrade with Authorization: Key <FAL_KEY>. Keep the fal key on your server; do not ship it to browsers or mobile clients.
kotoba-sdk 0.3.2 or newer on PyPI talks to this endpoint and selects the Key scheme for fal.run hosts.
pip install "kotoba-sdk>=0.3.2"
export KOTOBA_API_KEY=<fal key>
export KOTOBA_TTS_URL=wss://fal.run/Kotoba-Technologies/kotoba-tts/v2/tts/wsfrom kotoba import KotobaClient
client = KotobaClient()
# One result
audio = client.tts.synthesize("おはようございます。", language="ja", speaker_id="female-01")
audio.to_wav("hello.wav")
# Streamed playback: 16-bit PCM chunks at 24 kHz
for pcm in client.tts.synthesize_stream("おはようございます。", language="ja", audio_format="pcm16"):
speaker.write(pcm)speaker_id, audio_format, sample_rate and voice_settings are keyword arguments of both calls.
Billing is per 1,000 UTF-8 bytes of input text. See the model page for the current rate.
Any WebSocket client works with this endpoint. The examples use ws for Node.js and websockets for Python.
npm install wsSet FAL_KEY as an environment variable in your runtime.
export FAL_KEY="YOUR_API_KEY"This endpoint speaks its own message protocol over a WebSocket. Open the connection with any WebSocket client and authenticate the upgrade with the Authorization: Key header. Browsers must not hold the API key: connect through your own server, which adds the header. The message protocol itself is documented in the model's About section.
import WebSocket from "ws";
// Keep the API key on your server; a browser connects through your
// own backend, which adds the header. Use the "ws" package: Node's
// global WebSocket ignores the headers option and the upgrade gets 401.
const ws = new WebSocket("wss://fal.run/kotoba-technologies/kotoba-tts/v2/tts/ws", {
headers: { Authorization: "Key " + process.env.FAL_KEY },
});
ws.on("open", () => {
// Send the app's first message here.
});
ws.on("message", (data, isBinary) => {
if (isBinary) return; // binary frame, e.g. audio
console.log(data.toString()); // the app's messages, shown as text
});
ws.on("error", console.error);The API uses an API Key for authentication. Read it from the FAL_KEY environment variable on your server and send it as the Authorization: Key <FAL_KEY> header on the WebSocket upgrade, as the real-time snippet above does.
Some attributes in the API accept file URLs as input. Whenever that's the case you can pass your own URL or a Base64 data URI.
You can pass a Base64 data URI as a file input. The API will handle the file decoding for you. Keep in mind that for large files, this alternative although convenient can impact the request performance.
You can also pass your own URLs as long as they are publicly accessible. Be aware that some hosts might block cross-site requests, rate-limit, or consider the request as a bot.
We provide a convenient file storage that allows you to upload files and use them in your requests. You can upload files using the client API and use the returned URL in your requests.
import { fal } from "@fal-ai/client";
const file = new File(["Hello, World!"], "hello.txt", { type: "text/plain" });
const url = await fal.storage.upload(file);Read more about file handling in our file upload guide.