# Speech (totalum-sdk/speech)

> 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.

`import … from 'totalum-sdk/speech'`, or the same namespace on `totalum` from `'totalum-sdk'`. Install, configuration and errors: [Totalum SDK](https://www.totalum.app/docs/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 type | What it does |
|---|---|
| [`totalumSpeech`](#totalumspeech) | Creates the `speech` namespace (the same object as `totalum.speech`, scope `speech`): speech to text and text to speech through Totalum. |
| [`speech.transcribe`](#speechtranscribe) | 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. |
| [`speech.synthesize`](#speechsynthesize) | 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. |
| [`SpeakInput`](#speakinput) | The input of `speech.synthesize()`. |
| [`SpeechAudio`](#speechaudio) | The audio bytes of `synthesize` without `store`, and what the call cost (`usage.credits`, the call's hold). |
| [`StoredMedia`](#storedmedia) | A generated file stored in the project's files. |
| [`TranscribeInput`](#transcribeinput) | The input of `speech.transcribe()`: a `url`, or `audioBase64` with a `filename`. |
| [`TranscribeOutput`](#transcribeoutput) | A 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**

| Name | Type | Required | Description |
|---|---|---|---|
| `options` | [`TotalumClientOptions`](https://www.totalum.app/docs/sdk/types#totalumclientoptions) | no | The project key, API URL and environment; each falls back to its environment variable. |
| `options.key` | `string` | no | Project key `tlm_pk_…`; default `TOTALUM_PROJECT_KEY`. Server-side only. |
| `options.apiUrl` | `string` | no | The Totalum API's URL: default `TOTALUM_SDK_API_URL`, else `https://sdk-api.totalum.app`. |
| `options.env` | `'dev' \| 'live'` | no | Sent as `x-totalum-env` on every call (the database it targets, and dev vs live usage); default `TOTALUM_ENV`, else `live`. |

**Returns** [`TotalumSpeech`](#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**

| Name | Type | Required | Description |
|---|---|---|---|
| `input` | [`TranscribeInput`](#transcribeinput) | yes | The audio and the language. |
| `input.audioBase64` | `string` | no | The audio bytes in base64, instead of `url`. |
| `input.filename` | `string` | no | With `audioBase64`: a file name whose extension says the format (`note.m4a`). |
| `input.url` | `string` | no | The audio's URL: a public `https://` URL, or a file this project stored. The extension says the format. Allowed: a URL. |
| `input.language` | `string` | no | The spoken language (`en`, `es`); default: detected. |
| `input.model` | `'fast' \| 'accurate'` | no | `fast` (default) or `accurate` (better on hard audio, costs more). |
| `input.diarize` | `boolean` | no | Accepted for compatibility; has no effect (speakers are not separated). |
| `input.timestamps` | `'word' \| 'segment'` | no | Accepted for compatibility; has no effect (no segments or words are returned). |
| `input.keyterms` | `string[]` | no | Accepted for compatibility; has no effect. |

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

| Field | Type | Always present | Description |
|---|---|---|---|
| `text` | `string` | yes | The transcribed text. |
| `durationSec` | `number` | yes | The audio's billed duration in seconds. |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |
| `language` | `string` | no | Not returned today. |
| `segments` | `({ start, end, text })[]` | no | Not returned today. |
| `segments.start` | `number` | yes | Not returned today. |
| `segments.end` | `number` | yes | Not returned today. |
| `segments.text` | `string` | yes | Not returned today. |
| `words` | `({ word, start, end })[]` | no | Not returned today. |
| `words.word` | `string` | yes | Not returned today. |
| `words.start` | `number` | yes | Not returned today. |
| `words.end` | `number` | yes | Not returned today. |

**Errors** — besides the [errors every call can throw](https://www.totalum.app/docs/sdk#errors):

| Code | HTTP | When |
|---|---|---|
| `AUDIO_FILE_REQUIRED` | 400 | No audio was given, or the URL could not be read. |
| `INVALID_AUDIO_FORMAT` | 400 | The file extension is not a supported audio format. |
| `AUDIO_FILE_TOO_LARGE` | 413 | The audio is over 25 MB. |
| `AI_PROVIDER_UNAVAILABLE` | 502 | The 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**

| Name | Type | Required | Description |
|---|---|---|---|
| `input` | [`SpeakInput`](#speakinput) | yes | The text, the voice and the audio format. |
| `input.text` | `string` | yes | The text to read aloud. Allowed: at least 1 character, at most 20000 characters. |
| `input.voice` | `string` | no | The voice name. Default: the `tts` preset's voice. |
| `input.language` | `string` | no | The text's language (`en`, `es`). |
| `input.model` | `'standard' \| 'premium'` | no | `standard` (default) or `premium` (a more natural voice, costs more). |
| `input.format` | `'mp3' \| 'wav' \| 'opus'` | no | `mp3` (default), `wav` or `opus`. |
| `input.speed` | `number` | no | Speaking speed; 1 is normal. Allowed: > 0. |
| `input.store` | `boolean` | no | `true` 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](https://www.totalum.app/docs/sdk#errors):

| Code | HTTP | When |
|---|---|---|
| `VALIDATION_ERROR` | 400 | `text` is empty or over 20,000 characters. |
| `AI_PROVIDER_UNAVAILABLE` | 502 | The 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()`.

| Name | Type | Required | Description |
|---|---|---|---|
| `text` | `string` | yes | The text to read aloud. Allowed: at least 1 character, at most 20000 characters. |
| `voice` | `string` | no | The voice name. Default: the `tts` preset's voice. |
| `language` | `string` | no | The text's language (`en`, `es`). |
| `model` | `'standard' \| 'premium'` | no | `standard` (default) or `premium` (a more natural voice, costs more). |
| `format` | `'mp3' \| 'wav' \| 'opus'` | no | `mp3` (default), `wav` or `opus`. |
| `speed` | `number` | no | Speaking speed; 1 is normal. Allowed: > 0. |
| `store` | `boolean` | no | `true` 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).

| Field | Type | Always present | Description |
|---|---|---|---|
| `audio` | `ArrayBuffer` | yes | The audio file's bytes: return them from a route, or upload them with `files.upload`. |
| `contentType` | `string` | yes | The audio's MIME type: `audio/mpeg` (mp3), `audio/wav` or `audio/ogg` (opus). |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |
| `credits` | `number` | yes | **Deprecated.** Since 1.0.0: use `usage.credits` (the same number). |

## `StoredMedia`

A generated file stored in the project's files.

| Field | Type | Always present | Description |
|---|---|---|---|
| `key` | `string` | yes | The stored file's key (see `totalum.files`). |
| `url` | `string` | yes | Its public URL. |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |

## `TranscribeInput`

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

| Name | Type | Required | Description |
|---|---|---|---|
| `audioBase64` | `string` | no | The audio bytes in base64, instead of `url`. |
| `filename` | `string` | no | With `audioBase64`: a file name whose extension says the format (`note.m4a`). |
| `url` | `string` | no | The audio's URL: a public `https://` URL, or a file this project stored. The extension says the format. Allowed: a URL. |
| `language` | `string` | no | The spoken language (`en`, `es`); default: detected. |
| `model` | `'fast' \| 'accurate'` | no | `fast` (default) or `accurate` (better on hard audio, costs more). |
| `diarize` | `boolean` | no | Accepted for compatibility; has no effect (speakers are not separated). |
| `timestamps` | `'word' \| 'segment'` | no | Accepted for compatibility; has no effect (no segments or words are returned). |
| `keyterms` | `string[]` | no | Accepted for compatibility; has no effect. |

## `TranscribeOutput`

A transcription, from `speech.transcribe()`.

| Field | Type | Always present | Description |
|---|---|---|---|
| `text` | `string` | yes | The transcribed text. |
| `durationSec` | `number` | yes | The audio's billed duration in seconds. |
| `usage` | `{ credits }` | yes | What the call cost. |
| `usage.credits` | `number` | yes | Credits charged for this call. |
| `language` | `string` | no | Not returned today. |
| `segments` | `({ start, end, text })[]` | no | Not returned today. |
| `segments.start` | `number` | yes | Not returned today. |
| `segments.end` | `number` | yes | Not returned today. |
| `segments.text` | `string` | yes | Not returned today. |
| `words` | `({ word, start, end })[]` | no | Not returned today. |
| `words.word` | `string` | yes | Not returned today. |
| `words.start` | `number` | yes | Not returned today. |
| `words.end` | `number` | yes | Not returned today. |
