---
title: "Streaming · Function API"
description: "Stream audio over a WebSocket while the person speaks, and get the text a moment after they stop."
canonical: https://functionvoice.ai/docs/api/streaming
---

# Streaming

Stream audio while the person speaks, and get the text a moment after they stop. Open a WebSocket to the transcriptions path, with the model in the URL and your key in the Authorization header:

```text
wss://functionvoice.ai/api/v1/audio/transcriptions?model=function/dictate-1.2&language=en
```

The URL also takes `sample_rate` and repeated `keyterms`. A client that can't set headers, as in a browser, sends the key as two subprotocols instead: `function-api` and `bearer.` followed by the key. The server answers with `function-api`. Never put a key in a page you ship to others.

## Flow

1. The server sends `session.created`.
2. Optionally, before any audio, send `session.update`. The server answers `session.updated`.
3. Send audio as it is recorded: binary messages of raw 16-bit little-endian mono PCM, 16 kHz unless `sample_rate` says otherwise. Every 100 to 250 ms is a good pace.
4. When the speaker stops, send `input_audio_buffer.commit`. The server sends `input_audio_buffer.committed`, then `transcript.text.done` with the text and the usage, and closes the socket.

```text title="Messages"
→ {"type": "session.update", "session": {"keyterms": ["Priya"], "sample_rate": 16000}}
← {"type": "session.updated", "session": {"id": "gen-…", "model": "function/dictate-1.2", "sample_rate": 16000, …}}
→ (binary PCM) (binary PCM) …
→ {"type": "input_audio_buffer.commit"}
← {"type": "input_audio_buffer.committed", "audio_seconds": 3.82}
← {"type": "transcript.text.done", "text": "Can you send the deck to Priya by Friday?", "usage": {"seconds": 3.82, "cost": 0.0013611111}}
```

## Events

| Event | From | What it is |
| --- | --- | --- |
| `session.created` | Server | The stream is open. `session` has its id, model, sample rate and language. |
| `session.update` | You | Optional, before any audio: `session` with `keyterms`, `language` or `sample_rate`. |
| `session.updated` | Server | The settings now in force. |
| `input_audio_buffer.append` | You | Audio as base64 16-bit PCM in `audio`. Binary messages of raw PCM work too. |
| `input_audio_buffer.commit` | You | The speaker stopped. No more audio. |
| `input_audio_buffer.committed` | Server | The audio was received: `audio_seconds`. |
| `transcript.text.done` | Server | The `text` and the `usage`. The socket closes after it. |
| `error` | Server | `error` in the same shape as an HTTP error's body. One that ends the stream is followed by a close. |

## Example

```js tab="JavaScript"
import WebSocket from "ws";

const socket = new WebSocket("wss://functionvoice.ai/api/v1/audio/transcriptions?model=function/dictate-1.2", {
  headers: { Authorization: `Bearer ${process.env.FUNCTION_API_KEY}` },
});

socket.on("open", () => {
  microphone.on("data", (pcm) => socket.send(pcm)); // 16-bit mono, 16 kHz
  microphone.on("end", () => socket.send(JSON.stringify({ type: "input_audio_buffer.commit" })));
});

socket.on("message", (data) => {
  const event = JSON.parse(data.toString());
  if (event.type === "transcript.text.done") console.log(event.text);
  if (event.type === "error") console.error(event.error.message);
});
```

```python tab="Python"
import asyncio, json, os
import websockets

async def dictate(chunks):
    async with websockets.connect(
        "wss://functionvoice.ai/api/v1/audio/transcriptions?model=function/dictate-1.2",
        additional_headers={"Authorization": f"Bearer {os.environ['FUNCTION_API_KEY']}"},
    ) as socket:
        async for chunk in chunks:  # bytes of 16-bit mono PCM at 16 kHz
            await socket.send(chunk)
        await socket.send(json.dumps({"type": "input_audio_buffer.commit"}))
        async for message in socket:
            event = json.loads(message)
            if event["type"] == "transcript.text.done":
                return event["text"]
            if event["type"] == "error":
                raise RuntimeError(event["error"]["message"])
```

## Refusals and limits

A stream checks the key and its rules, the model and the payment method before it opens. A refusal is an HTTP error, as for a POST. After it opens, a problem is an `error` event, and one that ends the stream is followed by a close.

- A stream with no message for 20 seconds before its commit is closed with `timeout`.
- One recording a stream, up to 10 minutes and 24 MB.
- A stream is billed like a file: its audio, at least 10 seconds.
