Open History to see the last commit
1---
2title: Soniox
3id: soniox-adapter
4order: 3
5description: "Transcribe audio with Soniox speech-to-text models in TanStack AI, with speaker labels, language detection, and translation, through the Soniox community adapter."
6keywords:
7 - tanstack ai
8 - soniox
9 - transcription
10 - speech-to-text
11 - asr
12 - community adapter
13---
14
15The Soniox community adapter calls Soniox speech-to-text from `generateTranscription()`. It supports speaker labels, language detection, custom terms, and translation. Soniox maintains it.
16
17## 1. Install
18
19<!-- ::start:tabs variant="package-manager" mode="install" -->
20
21react: @soniox/tanstack-ai-adapter
22vue: @soniox/tanstack-ai-adapter
23solid: @soniox/tanstack-ai-adapter
24svelte: @soniox/tanstack-ai-adapter
25preact: @soniox/tanstack-ai-adapter
26angular: @soniox/tanstack-ai-adapter
27vanilla: @soniox/tanstack-ai-adapter
28octane: @soniox/tanstack-ai-adapter
29
30<!-- ::end:tabs -->
31
32## 2. Set the API key
33
34Get your API key from the [Soniox Console](https://console.soniox.com). Put it in the environment of your server:
35
36```bash
37SONIOX_API_KEY=your-api-key
38```
39
40`sonioxTranscription` reads `SONIOX_API_KEY`. To pass the key in code, use `createSonioxTranscription`:
41
42```typescript
43import { generateTranscription } from "@tanstack/ai";
44import { createSonioxTranscription } from "@soniox/tanstack-ai-adapter";
45import { audioFile } from "./audio";
46
47const adapter = createSonioxTranscription("stt-async-v3", process.env.SONIOX_API_KEY!);
48
49const result = await generateTranscription({
50 adapter,
51 audio: audioFile,
52 threadId: "thread-1",
53});
54```
55
56## 3. Transcribe audio
57
58```typescript
59import { generateTranscription } from "@tanstack/ai";
60import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
61import { audioFile } from "./audio";
62
63const result = await generateTranscription({
64 adapter: sonioxTranscription("stt-async-v3"),
65 audio: audioFile,
66 modelOptions: {
67 enableLanguageIdentification: true,
68 enableSpeakerDiarization: true,
69 },
70 threadId: "thread-1",
71});
72
73console.log(result.text);
74console.log(result.segments);
75```
76
77You get the full text in `result.text`, and the timed segments with speaker labels in `result.segments`. See [Transcription](../media/transcription).
78
79## Language hints
80
81Soniox detects and transcribes speech in [60+ languages](https://soniox.com/docs/stt/concepts/supported-languages). Do you know which languages the audio has? Pass them in `languageHints` to improve accuracy:
82
83```typescript
84import { generateTranscription } from "@tanstack/ai";
85import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
86import { audio } from "./audio";
87
88const result = await generateTranscription({
89 adapter: sonioxTranscription("stt-async-v3"),
90 audio,
91 modelOptions: {
92 languageHints: ["en", "es"],
93 },
94 threadId: "thread-1",
95});
96```
97
98- Hints do not limit recognition. They only bias it.
99- If you pass the TanStack `language` option, the adapter adds it to `languageHints`.
100
101See the [Soniox language hints docs](https://soniox.com/docs/stt/concepts/language-hints).
102
103## Context
104
105Context helps the model know your domain and your important terms. It improves transcription and translation:
106
107```typescript
108import { generateTranscription } from "@tanstack/ai";
109import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
110import { audio } from "./audio";
111
112const result = await generateTranscription({
113 adapter: sonioxTranscription("stt-async-v3"),
114 audio,
115 modelOptions: {
116 context: {
117 general: [
118 { key: "domain", value: "Healthcare" },
119 { key: "topic", value: "Diabetes management consultation" },
120 { key: "doctor", value: "Dr. Martha Smith" },
121 ],
122 text: "The patient has a history of...",
123 terms: ["Celebrex", "Zyrtec", "Xanax"],
124 translationTerms: [
125 { source: "Mr. Smith", target: "Sr. Smith" },
126 { source: "MRI", target: "RM" },
127 ],
128 },
129 },
130 threadId: "thread-1",
131});
132```
133
134All four sections are optional:
135
136- `general`: key and value pairs about the audio.
137- `text`: free text about the audio.
138- `terms`: words to recognize.
139- `translationTerms`: fixed translations for terms.
140
141See the [Soniox context docs](https://soniox.com/docs/stt/concepts/context).
142
143## Translation
144
145One-way translation:
146
147```typescript
148import { generateTranscription } from "@tanstack/ai";
149import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
150import { audio } from "./audio";
151
152const result = await generateTranscription({
153 adapter: sonioxTranscription("stt-async-v3"),
154 audio,
155 modelOptions: {
156 translation: {
157 type: "one_way",
158 targetLanguage: "es",
159 },
160 },
161 threadId: "thread-1",
162});
163```
164
165Two-way translation:
166
167```typescript
168import { generateTranscription } from "@tanstack/ai";
169import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
170import { audio } from "./audio";
171
172const result = await generateTranscription({
173 adapter: sonioxTranscription("stt-async-v3"),
174 audio,
175 modelOptions: {
176 translation: {
177 type: "two_way",
178 languageA: "en",
179 languageB: "es",
180 },
181 },
182 threadId: "thread-1",
183});
184```
185
186With translation, the API returns transcription tokens and translation tokens. `segments` holds only the transcription tokens. To get the translation tokens, read the raw tokens and keep the ones where `translation_status === "translation"`. See [Raw tokens](#raw-tokens).
187
188## Models and options
189
190### Transcription options
191
192Pass these in `modelOptions`:
193
194| Option | What it does |
195| --- | --- |
196| `languageHints` | An array of ISO language codes that bias recognition. |
197| `languageHintsStrict` | When `true`, rely more on the language hints. Not all models support it. |
198| `enableLanguageIdentification` | Detect the spoken language. |
199| `enableSpeakerDiarization` | Find and label the different speakers. |
200| `context` | More context, to improve accuracy. See [Context](#context). |
201| `translation` | The translation settings. See [Translation](#translation). |
202| `clientReferenceId` | An optional reference ID of your own. |
203| `webhookUrl` | A webhook URL that gets a call when the transcription is done. |
204| `webhookAuthHeaderName` | The name of the auth header for the webhook. |
205| `webhookAuthHeaderValue` | The value of the auth header for the webhook. |
206
207See the [Soniox API reference](https://soniox.com/docs/stt/api-reference/transcriptions/create_transcription).
208
209### Adapter options
210
211Pass these to `createSonioxTranscription`:
212
213```typescript
214import { createSonioxTranscription } from "@soniox/tanstack-ai-adapter";
215
216const adapter = createSonioxTranscription("stt-async-v3", process.env.SONIOX_API_KEY!, {
217 baseUrl: "https://api.soniox.com",
218 pollingIntervalMs: 1000,
219 timeout: 180000,
220});
221```
222
223| Option | What it does |
224| --- | --- |
225| `apiKey` | Overrides `SONIOX_API_KEY`. Required with `createSonioxTranscription`. |
226| `baseUrl` | A custom API base URL. Default `https://api.soniox.com`. |
227| `headers` | More request headers. |
228| `timeout` | The transcription timeout in milliseconds. Default `180000`. |
229| `pollingIntervalMs` | How often the adapter checks the transcription status, in milliseconds. Default `1000`. |
230
231For data residency, see the [Soniox regional endpoints](https://soniox.com/docs/stt/data-residency#regional-endpoints).
232
233## Advanced
234
235### Raw tokens
236
237For translation or audio with many languages, you can need the raw tokens. Each one has its language and its translation status. The adapter adds a non-standard `providerMetadata` field at run time. It is not on the `TranscriptionResult` type, so narrow it with `in` checks:
238
239```typescript
240import { generateTranscription } from "@tanstack/ai";
241import { sonioxTranscription } from "@soniox/tanstack-ai-adapter";
242import { audio } from "./audio";
243
244function sonioxTokens(result: object): Array<unknown> {
245 if (!("providerMetadata" in result)) return [];
246 const metadata = result.providerMetadata;
247 if (typeof metadata !== "object" || metadata === null) return [];
248 if (!("soniox" in metadata)) return [];
249 const soniox = metadata.soniox;
250 if (typeof soniox !== "object" || soniox === null) return [];
251 if (!("tokens" in soniox) || !Array.isArray(soniox.tokens)) return [];
252 return soniox.tokens;
253}
254
255const result = await generateTranscription({
256 adapter: sonioxTranscription("stt-async-v3"),
257 audio,
258 modelOptions: {
259 translation: { type: "one_way", targetLanguage: "es" },
260 },
261 threadId: "thread-1",
262});
263
264for (const token of sonioxTokens(result)) {
265 console.log(token);
266}
267```
268
269Each token has these fields:
270
271- `text`: the token text.
272- `start_ms` and `end_ms`: the start and end time in milliseconds.
273- `language`: the detected language of the token.
274- `translation_status`: the translation status, when translation is on.
275- `speaker`: the speaker id.
276- `confidence`: the confidence score.
277
278For all of the Soniox API, see the [Soniox docs](https://soniox.com/docs).