Protected ReadonlyuserThe User-Agent the client sends, or undefined with userAgent: false.
Mints a temporary token for a session or an HTTP request made from a user's device, where the API key must not go. Call this server-side, once per connection (reconnects included) and per HTTP request.
The token is minted on the token host of the client's zone:
https://token.eu.assemblyai.com when ttsBaseUrl is
https://tts.eu.assemblyai.com, https://token.us.assemblyai.com when
it is https://tts.us.assemblyai.com (the default), and
https://token.assemblyai.com for any other host. A token works in
every zone.
OptionaltokenThe token host to use instead.
ProtectedfetchProtectedfetchProtectedfetchCreates a WebSocket session; nothing connects until connect().
The client's API key is used when params has neither token nor
apiKey; the session host follows the client's ttsBaseUrl unless
websocketBaseUrl is given. In a browser, open the session with
new TextToSpeechSession({ token }) instead, so the page needs no client
and no API key.
Speaks request.text and returns the audio as it is generated.
Resolves once the API has sent the first audio. The API commits to 200
then, so a later failure cuts the body and the iteration throws. So does
a body that sends nothing for options.readTimeout (30 000 ms by
default) while the iteration waits for audio; the connection is then
released. options.signal keeps covering the body: aborting it stops
the download. A stream dropped before its end without cancel() keeps
its connection until it is garbage-collected, so cancel one you will
not read.
The audio comes as a web ReadableStream, so the runtime needs a global
ReadableStream (React Native has none without a polyfill). Without
one, stream() rejects before sending anything; use synthesize()
there. A runtime with ReadableStream whose fetch gives no streamed
body gets the whole audio as one chunk once it has all arrived.
TextToSpeechError if the API refuses the request before any audio,
or, with retryable: false and before any request, if the runtime has
no global ReadableStream.
A network failure or a timeout rejects with the runtime's own error
instead: a TypeError, a DOMException named TimeoutError once
options.timeout passes, or the reason of options.signal.
Speaks request.text and returns the complete audio.
Nothing is sent back until the audio is complete, so a long text takes
several seconds and an API failure is always a TextToSpeechError,
never a cut response.
On Node, the built-in fetch waits at most 300 seconds for the response
headers (undici's headersTimeout), whatever options.timeout says. For
longer requests use stream(), or raise that limit with
setGlobalDispatcher(new Agent({ headersTimeout: 610_000 })) from the
undici package.
The Text-to-Speech API: text in, audio out.
synthesize()returns one text's complete audio;stream()returns it as the API generates it;session()opens a WebSocket session that speaks text as it is written, for voice agents. Requests go to the client'sttsBaseUrl(https://tts.us.assemblyai.comby default;https://tts.eu.assemblyai.comkeeps them in the EU).Example