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

# Speech-to-Text Streaming

## Endpoint

`WS /websocket/speech-to-text`

## Authentication

The server accepts either:

| Field           | Type   | Required | Description                             |
| --------------- | ------ | -------- | --------------------------------------- |
| `x-api-key`     | string | No       | API key in header or query              |
| `Authorization` | string | No       | Bearer token header                     |
| `token`         | string | No       | Token query param fallback for browsers |

At least one auth method is required. If auth is invalid, the connection is rejected or closed.

## Query Parameters

| Field   | Type   | Required | Description                                                                                       |
| ------- | ------ | -------- | ------------------------------------------------------------------------------------------------- |
| `model` | string | No       | ASR model to use: `munsit` (default) or `munsit-en-ar` (mixed Arabic-English with code-switching) |

### Connection example

```text theme={null}
wss://api.munsit.com/api/v1/websocket/speech-to-text?x-api-key=YOUR_MUNSIT_API_KEY&model=munsit
```

## Supported audio input

* First chunk must be WAV (with headers)
* Subsequent chunks can be WAV or raw PCM

## Client message format

Primary format (`event` + `data`):

```json theme={null}
{
  "event": "audio_chunk",
  "data": {
    "audioBuffer": [1, 2, 3]
  }
}
```

Compatibility format also accepted:

```json theme={null}
{
  "audioBuffer": [1, 2, 3]
}
```

`audioBuffer` must be an array of byte values (`0-255`).

## Events to emit

### `audio_chunk`

* **Direction**: client -> server
* **Payload**: `audioBuffer` as `Array<Uint8>`
* **Guidance**:
  * send approximately every \~1 second
  * first chunk should include full WAV headers
  * after first chunk, PCM chunks are accepted

## Events to listen to

### `transcription`

* **Direction**: server -> client
* **Type**: `string`
* **Meaning**: cumulative Arabic transcript generated from all received chunks

### `transcription_error`

* **Direction**: server -> client
* **Type**: `string`
* **Meaning**: error details during streaming transcription

### Connection/auth notes

* **`connect`**: client-level WebSocket open event (successful handshake)
* **`authentication_error`**: not emitted as a dedicated WS event in current backend; invalid auth is handled by rejecting/closing the connection

## Recommended flow

1. Connect to `WS /websocket/speech-to-text` with auth
2. Confirm the socket is open on the client side
3. Emit `audio_chunk` payloads (roughly every \~1s)
4. Listen for `transcription_error` and handle failures
5. Listen for `transcription` and render live text updates
6. Safely disconnect when transcription is complete


## AsyncAPI

````yaml api-reference/asyncapi.json speech-to-text
id: speech-to-text
title: Speech-to-text
description: ''
servers:
  - id: production
    protocol: wss
    host: api.munsit.com
    bindings: []
    variables: []
address: wss://api.munsit.com/api/v1/websocket/speech-to-text
parameters: []
bindings:
  - protocol: ws
    version: latest
    value:
      headers:
        type: object
        properties:
          x-api-key:
            type: string
            description: API key header
          Authorization:
            type: string
            description: Bearer token header
      query:
        type: object
        properties:
          x-api-key:
            type: string
            description: Your Munsit API key
          token:
            type: string
            description: Optional auth token for browser WebSocket fallback
    schemaProperties:
      - name: headers
        type: object
        required: false
        properties:
          - name: x-api-key
            type: string
            description: API key header
            required: false
          - name: Authorization
            type: string
            description: Bearer token header
            required: false
      - name: query
        type: object
        required: false
        properties:
          - name: x-api-key
            type: string
            description: Your Munsit API key
            required: false
          - name: token
            type: string
            description: Optional auth token for browser WebSocket fallback
            required: false
operations:
  - &ref_3
    id: sendSttAudioChunk
    title: Send stt audio chunk
    type: send
    messages:
      - &ref_6
        id: audio_chunk
        payload:
          - oneOf: &ref_0
              - type: object
                required:
                  - event
                  - data
                properties:
                  event:
                    type: string
                    const: audio_chunk
                    x-parser-schema-id: <anonymous-schema-31>
                  data:
                    type: object
                    required:
                      - audioBuffer
                    properties:
                      audioBuffer:
                        type: array
                        items:
                          type: integer
                          minimum: 0
                          maximum: 255
                          x-parser-schema-id: <anonymous-schema-34>
                        description: >-
                          Audio bytes as Uint8 values. First chunk should be
                          WAV.
                        x-parser-schema-id: <anonymous-schema-33>
                    x-parser-schema-id: <anonymous-schema-32>
                x-parser-schema-id: <anonymous-schema-30>
              - type: object
                required:
                  - audioBuffer
                properties:
                  audioBuffer:
                    type: array
                    items:
                      type: integer
                      minimum: 0
                      maximum: 255
                      x-parser-schema-id: <anonymous-schema-37>
                    description: Compatibility format accepted by server.
                    x-parser-schema-id: <anonymous-schema-36>
                x-parser-schema-id: <anonymous-schema-35>
            x-parser-schema-id: <anonymous-schema-29>
            name: Audio Chunk
            description: Send a chunk of audio bytes for transcription
        headers: []
        jsonPayloadSchema:
          oneOf: *ref_0
          x-parser-schema-id: <anonymous-schema-29>
        title: Audio Chunk
        description: Send a chunk of audio bytes for transcription
        example: '{}'
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: audio_chunk
    bindings: []
    extensions: &ref_1
      - id: x-parser-unique-object-id
        value: speech-to-text
  - &ref_2
    id: receiveSttEvents
    title: Receive stt events
    type: receive
    messages:
      - &ref_4
        id: transcription
        payload:
          - name: Transcription
            description: Transcription text event from server
            type: object
            properties:
              - name: event
                type: string
                description: transcription
                required: true
              - name: data
                type: string
                description: Cumulative transcription text
                required: true
        headers: []
        jsonPayloadSchema:
          type: object
          required:
            - event
            - data
          properties:
            event:
              type: string
              const: transcription
              x-parser-schema-id: <anonymous-schema-39>
            data:
              type: string
              description: Cumulative transcription text
              x-parser-schema-id: <anonymous-schema-40>
          x-parser-schema-id: <anonymous-schema-38>
        title: Transcription
        description: Transcription text event from server
        example: |-
          {
            "event": "<string>",
            "data": "<string>"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: transcription
      - &ref_5
        id: transcription_error
        payload:
          - name: Transcription Error
            description: Error event from server
            type: object
            properties:
              - name: event
                type: string
                description: transcription_error
                required: true
              - name: data
                type: string
                description: Error message
                required: true
        headers: []
        jsonPayloadSchema:
          type: object
          required:
            - event
            - data
          properties:
            event:
              type: string
              const: transcription_error
              x-parser-schema-id: <anonymous-schema-42>
            data:
              type: string
              description: Error message
              x-parser-schema-id: <anonymous-schema-43>
          x-parser-schema-id: <anonymous-schema-41>
        title: Transcription Error
        description: Error event from server
        example: |-
          {
            "event": "<string>",
            "data": "<string>"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: transcription_error
    bindings: []
    extensions: *ref_1
sendOperations:
  - *ref_2
receiveOperations:
  - *ref_3
sendMessages:
  - *ref_4
  - *ref_5
receiveMessages:
  - *ref_6
extensions:
  - id: x-parser-unique-object-id
    value: speech-to-text
securitySchemes: []

````