> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mka1.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Text to speech

> Synthesize speech or stream audio as it is generated.

This standalone API speaks text supplied by your application. For speech generated as part of a model reply, see [Audio responses](/docs/audio-responses).

## Generate speech

Use the standard text-to-speech endpoint when you want a complete WAV file.
The response body is binary audio, and the response headers include `X-Language-Code`.

To save the generated audio to history, set `store=true`. When stored, the response also includes `X-Tts-Id`, and persistence completes before the response returns.

<CodeGroup>
  ```ts TypeScript SDK theme={null}
  import { SDK } from '@meetkai/mka1';
  import { writeFileSync } from 'node:fs';

  const sdk = new SDK({ bearerAuth: 'Bearer <mka1-api-key>' });

  const result = await sdk.llm.speech.speak({
    xOnBehalfOf: '<end-user-id>',
    textToSpeechRequest: {
      text: 'Welcome to the MKA1 API speech guide.',
      language: 'en',
      store: true,
    },
  });

  const audioBuffer = Buffer.from(await new Response(result.result).arrayBuffer());
  const languageCode = result.headers['x-language-code']?.[0];
  const ttsId = result.headers['x-tts-id']?.[0];

  writeFileSync('speech.wav', audioBuffer);
  console.log(languageCode);
  console.log(ttsId);
  ```

  ```python Python SDK theme={null}
  from meetkai_mka1 import SDK

  sdk = SDK(bearer_auth="Bearer <mka1-api-key>")

  result = sdk.llm.speech.speak(
      text="Welcome to the MKA1 API speech guide.",
      language="en",
      store=True,
  )

  with open("speech.wav", "wb") as f:
      f.write(result.body)
  ```

  ```csharp C# SDK theme={null}
  using MeetKai.MKA1;
  using MeetKai.MKA1.Types.Components;
  using MeetKai.MKA1.Types.Requests;

  var sdk = new SDK(bearerAuth: "Bearer <mka1-api-key>");

  var result = await sdk.Llm.Speech.SpeakAsync(new MeetKai.MKA1.Types.Components.TextToSpeechRequest()
  {
      Text = "Welcome to the MKA1 API speech guide.",
      Language = TextToSpeechRequestLanguage.En,
      Store = true,
  });

  System.IO.File.WriteAllBytes("speech.wav", result.Bytes!);
  ```

  ```bash CLI theme={null}
  mka1 llm speech speak \
    --text 'Welcome to the MKA1 API speech guide.' \
    --language en \
    --store \
    --output-file speech.wav
  ```

  ```bash Bash theme={null}
  curl https://apigw.mka1.com/api/v1/llm/speech/tts \
    --request POST \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer <mka1-api-key>' \
    --header 'X-On-Behalf-Of: <end-user-id>' \
    --data '{
      "text": "Welcome to the MKA1 API speech guide.",
      "language": "en",
      "store": true
    }' \
    --output speech.wav
  ```
</CodeGroup>

## Stream speech for lower latency

Use streaming text-to-speech when you want playback to start before the full audio file is ready.
Choose `mp3` for smaller payloads or `pcm` for uncompressed audio (returned as `audio/wav`).

To save the generated audio to history, set `store=true`. When stored, the response includes `X-Tts-Id`. History persistence for streaming is best-effort and completes shortly after the stream ends, so an immediate lookup by `X-Tts-Id` can briefly return `404`. If persistence fails or is skipped (for example, if the audio exceeds the 25MB history cap), the id is never assigned and lookups keep returning `404`.

<CodeGroup>
  ```ts TypeScript SDK theme={null}
  const result = await sdk.llm.speech.speakStreaming({
    xOnBehalfOf: '<end-user-id>',
    textToSpeechStreamingRequest: {
      text: 'Start speaking this response as soon as audio is ready.',
      language: 'en',
      format: 'mp3',
      store: true,
    },
  });

  const contentType = result.headers['content-type']?.[0];
  const languageCode = result.headers['x-language-code']?.[0];
  const ttsId = result.headers['x-tts-id']?.[0];

  console.log(contentType);
  console.log(languageCode);
  console.log(ttsId);
  ```

  ```python Python SDK theme={null}
  result = sdk.llm.speech.speak_streaming(
      text="Start speaking this response as soon as audio is ready.",
      language="en",
      format_="mp3",
      store=True,
  )

  with open("speech.mp3", "wb") as f:
      f.write(result.body)
  ```

  ```csharp C# SDK theme={null}
  var result = await sdk.Llm.Speech.SpeakStreamingAsync(new MeetKai.MKA1.Types.Components.TextToSpeechStreamingRequest()
  {
      Text = "Start speaking this response as soon as audio is ready.",
      Language = TextToSpeechStreamingRequestLanguage.En,
      Format = TextToSpeechStreamingRequestFormat.Mp3,
      Store = true,
  });

  // Response contains either MP3 or WAV bytes depending on format
  var audioBytes = result.TwoHundredAudioMpegBytes ?? result.TwoHundredAudioWavBytes;
  System.IO.File.WriteAllBytes("speech.mp3", audioBytes!);
  ```

  ```bash CLI theme={null}
  mka1 llm speech speak-streaming \
    --text 'Start speaking this response as soon as audio is ready.' \
    --language en \
    --format mp3 \
    --store \
    --output-file speech.mp3
  ```

  ```bash Bash theme={null}
  curl https://apigw.mka1.com/api/v1/llm/speech/tts/stream \
    --request POST \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer <mka1-api-key>' \
    --header 'X-On-Behalf-Of: <end-user-id>' \
    --data '{
      "text": "Start speaking this response as soon as audio is ready.",
      "language": "en",
      "format": "mp3",
      "store": true
    }' \
    --output speech.mp3
  ```
</CodeGroup>
