Docs
BlogHomeStart building

Speech (totalum-sdk/speech)


import … from 'totalum-sdk/speech' — or the same namespace on totalum from 'totalum-sdk'. Install, configuration and errors: Totalum SDK.

Speech to text (transcribe) and text to speech (synthesize): transcribe a recording or a voice note, read a text aloud, store the audio as a project file. Billed in credits per minute of audio or per character.

ts
const { text } = await totalum.speech.transcribe({ url: recording.fileUrl });
Method or typeWhat it does
totalumSpeechCreates the speech namespace (the same object as totalum.speech, scope speech): speech to text and text to speech through Totalum.
speech.transcribeSpeech to text: transcribes a recording, a voice note or a video's audio, from a url (a public one, or a file this project stored) or from audioBase64 plus a filename with its extension.
speech.synthesizeReads a text aloud (text to speech): with store: true the audio is saved as a public project file and you get its key and url; otherwise you get the audio bytes to return from a route or store yourself.
SpeakInputThe input of speech.synthesize().
SpeechAudioThe audio bytes of synthesize without store, and what the call cost (usage.credits, the call's hold).
StoredMediaA generated file stored in the project's files.
TranscribeInputThe input of speech.transcribe(): a url, or audioBase64 with a filename.
TranscribeOutputA transcription, from speech.transcribe().

#totalumSpeech

ts
totalumSpeech(options?: TotalumClientOptions): TotalumSpeech

Creates the speech namespace (the same object as totalum.speech, scope speech): speech to text and text to speech through Totalum. The AI gateway (ai.gatewayConfig()) also offers /audio/speech and /audio/transcriptions for OpenAI-compatible clients; these methods keep working.

Parameters

NameTypeRequiredDescription
optionsTotalumClientOptionsnoThe project key, API URL and environment; each falls back to its environment variable.
options.keystringnoProject key tlm_pk_…; default TOTALUM_PROJECT_KEY. Server-side only.
options.apiUrlstringnoThe Totalum API's URL: default TOTALUM_SDK_API_URL, else https://sdk-api.totalum.app.
options.env'dev' | 'live'noSent as x-totalum-env on every call (the database it targets, and dev vs live usage); default TOTALUM_ENV, else live.

Returns TotalumSpeech — The speech namespace: the methods below.

Example

ts
import { totalumSpeech } from 'totalum-sdk/speech';

const speech = totalumSpeech();
const { text } = await speech.transcribe({ url: 'https://example.com/voice-note.mp3' });

The namespace's type is exported as TotalumSpeech (import type { TotalumSpeech } from 'totalum-sdk/speech').

#speech.transcribe

ts
speech.transcribe(input: TranscribeInput): Promise<TranscribeOutput>

Speech to text: transcribes a recording, a voice note or a video's audio, from a url (a public one, or a file this project stored) or from audioBase64 plus a filename with its extension. Formats: mp3, mp4, m4a, wav, webm, ogg, opus, flac, aac; at most 25 MB. Billed in credits per second of audio.

Parameters

NameTypeRequiredDescription
inputTranscribeInputyesThe audio and the language.
input.audioBase64stringnoThe audio bytes in base64, instead of url.
input.filenamestringnoWith audioBase64: a file name whose extension says the format (note.m4a).
input.urlstringnoThe audio's URL: a public https:// URL, or a file this project stored. The extension says the format. Allowed: a URL.
input.languagestringnoThe spoken language (en, es); default: detected.
input.model'fast' | 'accurate'nofast (default) or accurate (better on hard audio, costs more).
input.diarizebooleannoAccepted for compatibility; has no effect (speakers are not separated).
input.timestamps'word' | 'segment'noAccepted for compatibility; has no effect (no segments or words are returned).
input.keytermsstring[]noAccepted for compatibility; has no effect.

Returns Promise<TranscribeOutput> — The text, the audio's duration and what it cost.

FieldTypeAlways presentDescription
textstringyesThe transcribed text.
durationSecnumberyesThe audio's billed duration in seconds.
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.
languagestringnoNot returned today.
segments({ start, end, text })[]noNot returned today.
segments.startnumberyesNot returned today.
segments.endnumberyesNot returned today.
segments.textstringyesNot returned today.
words({ word, start, end })[]noNot returned today.
words.wordstringyesNot returned today.
words.startnumberyesNot returned today.
words.endnumberyesNot returned today.

Errors — besides the errors every call can throw:

CodeHTTPWhen
AUDIO_FILE_REQUIRED400No audio was given, or the URL could not be read.
INVALID_AUDIO_FORMAT400The file extension is not a supported audio format.
AUDIO_FILE_TOO_LARGE413The audio is over 25 MB.
AI_PROVIDER_UNAVAILABLE502The audio could not be transcribed; retry.

Example

ts
const result = await totalum.speech.transcribe({ url: voiceNote.fileUrl, language: 'en' });

Result:

json
{ "text": "Hello there.", "durationSec": 10, "usage": { "credits": 0.0064 } }

#speech.synthesize

ts
speech.synthesize(input: { text, voice?, language?, model?, format?, speed?, store }): Promise<StoredMedia>
speech.synthesize(input: SpeakInput): Promise<StoredMedia | SpeechAudio>

Reads a text aloud (text to speech): with store: true the audio is saved as a public project file and you get its key and url; otherwise you get the audio bytes to return from a route or store yourself. Billed in credits per character.

Parameters

NameTypeRequiredDescription
inputSpeakInputyesThe text, the voice and the audio format.
input.textstringyesThe text to read aloud. Allowed: at least 1 character, at most 20000 characters.
input.voicestringnoThe voice name. Default: the tts preset's voice.
input.languagestringnoThe text's language (en, es).
input.model'standard' | 'premium'nostandard (default) or premium (a more natural voice, costs more).
input.format'mp3' | 'wav' | 'opus'nomp3 (default), wav or opus.
input.speednumbernoSpeaking speed; 1 is normal. Allowed: > 0.
input.storebooleannotrue saves the audio as a public project file and returns its key and url instead of the bytes.

Returns Promise<StoredMedia \| SpeechAudio> — The stored file { key, url, usage } with store: true; else the audio bytes { audio, contentType, usage }.

Errors — besides the errors every call can throw:

CodeHTTPWhen
VALIDATION_ERROR400text is empty or over 20,000 characters.
AI_PROVIDER_UNAVAILABLE502The voice could not be generated; retry.

Examples

ts
const audio = await totalum.speech.synthesize({ text: 'Your order has shipped.', store: true });

Result:

json
{
  "key": "p/acme-crm/01M4P0SZ4SKYEPG76P3DATMX1C.mp3",
  "url": "https://files.totalum-project.com/p/acme-crm/01M4P0SZ4SKYEPG76P3DATMX1C.mp3",
  "usage": { "credits": 0.01 }
}
ts
// the bytes, returned from a route
const out = await totalum.speech.synthesize({ text: 'Hi Ana', format: 'mp3' });
if ('audio' in out) return new Response(out.audio, { headers: { 'content-type': out.contentType } });

Result:

ts
{ audio: ArrayBuffer(15360), contentType: 'audio/mpeg', usage: { credits: 0.01 } }

#SpeakInput

The input of speech.synthesize().

NameTypeRequiredDescription
textstringyesThe text to read aloud. Allowed: at least 1 character, at most 20000 characters.
voicestringnoThe voice name. Default: the tts preset's voice.
languagestringnoThe text's language (en, es).
model'standard' | 'premium'nostandard (default) or premium (a more natural voice, costs more).
format'mp3' | 'wav' | 'opus'nomp3 (default), wav or opus.
speednumbernoSpeaking speed; 1 is normal. Allowed: > 0.
storebooleannotrue saves the audio as a public project file and returns its key and url instead of the bytes.

#SpeechAudio

The audio bytes of synthesize without store, and what the call cost (usage.credits, the call's hold).

FieldTypeAlways presentDescription
audioArrayBufferyesThe audio file's bytes: return them from a route, or upload them with files.upload.
contentTypestringyesThe audio's MIME type: audio/mpeg (mp3), audio/wav or audio/ogg (opus).
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.
creditsnumberyesDeprecated. Since 1.0.0: use usage.credits (the same number).

#StoredMedia

A generated file stored in the project's files.

FieldTypeAlways presentDescription
keystringyesThe stored file's key (see totalum.files).
urlstringyesIts public URL.
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.

#TranscribeInput

The input of speech.transcribe(): a url, or audioBase64 with a filename.

NameTypeRequiredDescription
audioBase64stringnoThe audio bytes in base64, instead of url.
filenamestringnoWith audioBase64: a file name whose extension says the format (note.m4a).
urlstringnoThe audio's URL: a public https:// URL, or a file this project stored. The extension says the format. Allowed: a URL.
languagestringnoThe spoken language (en, es); default: detected.
model'fast' | 'accurate'nofast (default) or accurate (better on hard audio, costs more).
diarizebooleannoAccepted for compatibility; has no effect (speakers are not separated).
timestamps'word' | 'segment'noAccepted for compatibility; has no effect (no segments or words are returned).
keytermsstring[]noAccepted for compatibility; has no effect.

#TranscribeOutput

A transcription, from speech.transcribe().

FieldTypeAlways presentDescription
textstringyesThe transcribed text.
durationSecnumberyesThe audio's billed duration in seconds.
usage{ credits }yesWhat the call cost.
usage.creditsnumberyesCredits charged for this call.
languagestringnoNot returned today.
segments({ start, end, text })[]noNot returned today.
segments.startnumberyesNot returned today.
segments.endnumberyesNot returned today.
segments.textstringyesNot returned today.
words({ word, start, end })[]noNot returned today.
words.wordstringyesNot returned today.
words.startnumberyesNot returned today.
words.endnumberyesNot returned today.