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

# Generate Streamed Text to Speech File Data

> From the user's perspective, this is a standard request. In the response, we include specific headers: 'Transfer-Encoding' is set to 'chunked' to enable streaming, 'Connection' is set to 'keep-alive' to maintain the connection, and 'Content-Type' is set to 'audio/wav' to indicate the media type. These headers allow the client to stream audio data from the server in real time. Important Note: after collecting the chunks, you need to add the wav header manually to the data. If you wish to get a wav header, please use the [Generate TTS File Data API](/api-reference/endpoint/rt-generate-tts)



## OpenAPI

````yaml POST /v1/realtime/tts-stream
openapi: 3.0.1
info:
  title: Hamsa API
  description: Trying out our transcription and media-to-text APIs
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://api.tryhamsa.com
security: []
paths:
  /v1/realtime/tts-stream:
    post:
      summary: Generates a streamed audio file from a text string.
      description: >-
        From the user's perspective, this is a standard request. In the
        response, we include specific headers: 'Transfer-Encoding' is set to
        'chunked' to enable streaming, 'Connection' is set to 'keep-alive' to
        maintain the connection, and 'Content-Type' is set to 'audio/wav' to
        indicate the media type. These headers allow the client to stream audio
        data from the server in real time. Important Note: after collecting the
        chunks, you need to add the wav header manually to the data. If you wish
        to get a wav header, please use the [Generate TTS File Data
        API](/api-reference/endpoint/rt-generate-tts)
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RtTtsRequest'
      responses:
        '200':
          description: Streamed Successful Response - Chunked
          content:
            stream:
              schema:
                type: string
                format: binary
                example: '00000000: 52 49 46 46 FF FF FF FF 57 41 56 45 66 6D 74 20 '
                x-streaming: true
          headers:
            Transfer-Encoding:
              description: >-
                Indicates that the response uses chunked transfer encoding -
                streamed.
              schema:
                type: string
                example: chunked
            Connection:
              description: >-
                Indicates that the server will keep the connection open for
                streaming.
              schema:
                type: string
                example: keep-alive
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
        '500':
          description: Server Side Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorSchema'
      security:
        - ApiKeyAuth: []
      x-codeSamples:
        - lang: JavaScript
          label: Stream and play audio chunks (Browser)
          source: |-
            fetch('https://api.tryhamsa.com/v1/realtime/tts-stream', {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Token YOUR_API_KEY'
              },
              body: JSON.stringify({ speaker: 'Ali', dialect: 'pls', text: 'Hello, world!', mulaw: false })
            })
            .then(response => {
              const reader = response.body.getReader();
              const chunks = [];

              function read() {
                reader.read().then(({ done, value }) => {
                  if (done) {
                    // Concatenate chunks and play entire stream
                    const blob = new Blob(chunks, { type: 'audio/wav' });
                    const url = URL.createObjectURL(blob);
                    const audio = new Audio(url);
                    audio.play();
                    return;
                  }

                  chunks.push(value);
                  read();
                });
              }

              read();
            })
            .catch(console.error);
        - lang: JavaScript
          label: Stream and save audio (Node.js)
          source: |-
            // For Node.js environments
            // npm install node-fetch
            import fetch from 'node-fetch';
            import * as fs from 'fs';

            fetch('https://api.tryhamsa.com/v1/realtime/tts-stream', {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Token YOUR_API_KEY'
              },
              body: JSON.stringify({ speaker: 'Ali', dialect: 'pls', text: 'Hello, world!', mulaw: false })
            })
            .then(response => {
              if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
              }
              
              // Create a write stream for the output file
              const fileStream = fs.createWriteStream('hamsa-output.wav');
              
              // Pipe the response body directly to the file
              response.body.pipe(fileStream);
              
              return new Promise((resolve, reject) => {
                response.body.on('error', reject);
                fileStream.on('finish', resolve);
              });
            })
            .then(() => {
              console.log('Audio saved to hamsa-output.wav');
            })
            .catch(console.error);
components:
  schemas:
    RtTtsRequest:
      type: object
      properties:
        text:
          type: string
          description: The text to be generated as an aduio file.
          example: مرحباً بكم في جميعاً في همسة!
        speaker:
          type: string
          description: >-
            The speaker name, Suggestions: Amjad, Lyali, Salma, Mariam, Dalal,
            Lana, Jasem, Samir, Carla, Nada, or you can provide another values
            from the voice agents dashboard. You can also provide your own
            cloned voice's id here in the speaker field, but you need to preload
            that voice into the system first from
            [here](/api-reference/endpoint/preload-cloned-tts-voice)
          example: Majd
        dialect:
          type: string
          enum:
            - pls
            - egy
            - syr
            - irq
            - jor
            - leb
            - ksa
            - uae
            - bah
            - qat
            - msa
          description: >-
            The dialect of each country by its country code. Recommended voices
            per dialect are: pls (Palestinian): Amjad, Layan, Talin, Rema, Ali;
            leb (Lebanese): Carla, Majd; jor (Jordanian): Lana, Jasem, Nada,
            Sarah; syr (Syrian): Dalal, Mais; ksa (Saudi): Hiba, Khadija, Fahd,
            Jasem; bah (Bahraini): Mazen, Ruba; uae (Emirati): Salma, Dima; egy
            (Egyptian): Mariam, Samir, Nada, Ali, Ahmed; irq (Iraqi): Lyali,
            Fatma.
          example: pls
        mulaw:
          type: boolean
          description: Whether to use 'Mu-Law' algorithm in voice signal processing or not.
          example: false
          default: false
      required:
        - text
        - speaker
    ErrorSchema:
      type: object
      properties:
        code:
          type: integer
        message:
          type: string
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Authorization
      description: >-
        Pass the API key in the Authorization header, You need to put Token
        keyword before the API key. e.g. 'Authorization: Token <api-key>'

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.