Guides

Capture audio in the browser

Stream a microphone and a meeting tab from a web page, and get turns and moments back.

Sparkle can transcribe audio for you. A web page streams the microphone, and optionally a meeting running in another browser tab, and Sparkle turns what it hears into turns on the conversation. The moments arrive the same way as from any other source.

When to use it

You haveUse
A web app people talk into, or a meeting open in another tabBrowser audio: this guide.
Your own speech-to-textPost turns: you send the text, not the audio.
A bot that joins Zoom, Meet or TeamsThe Recall.ai receiver.
Audio on a server, a phone bridge or a mobile appThe audio streaming protocol, without the SDK.

Audio is never stored

No audio is stored, on disk, in Redis or in logs; only the turns transcribed from it are kept. There is no recording to download or delete later.

How it works

  1. Your page calls recordAudio from @sparkle/sdk/browser.
  2. For each source, the SDK asks your backend for an audio session.
  3. Your backend calls POST /conversations/{id}/audio-sessions with its API key and returns the session's url.
  4. The page streams audio to that URL. Interim text and finished turns come back on the same connection, and moments arrive on the conversation's stream and webhooks.

The API key never reaches the browser. The page only ever holds a session URL, whose token works once, within 60 seconds.

@sparkle/sdk is in private beta

The browser code and the TypeScript tab use @sparkle/sdk, which isn't on npm yet. To try it, ask us for access. The cURL, Python and Go tabs call the HTTP API directly.

1. Mint sessions on your backend

Add a route that the page can call. It should check that the signed-in user may use the conversation, then mint a session and return its url. The SDK posts this body to it:

FieldValue
conversationIdThe conversation passed to recordAudio.
sourcelocal for the microphone, remote for the shared tab.
encodinglinear16.
sample_rate16000, 24000, 44100 or 48000: the rate the browser records at.
languageOnly when you passed language to recordAudio, such as en.

Forward everything but conversationId as the body of the API call. Each call mints a new session, so send a fresh Idempotency-Key every time:

curl https://api.sparkle.ai/public/v1/conversations/$CONVERSATION_ID/audio-sessions \
  -H "X-API-Key: $SPARKLE_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"source": "local", "encoding": "linear16", "sample_rate": 48000}'

Sparkle answers 201 Created:

{
  "url": "wss://api.sparkle.ai/public/v1/conversations/cnv_2kd9c3f6j0m4q8u2y5bg/audio?token=…",
  "token": "…",
  "expires_at": "2026-12-03T17:01:00Z"
}

2. Record in the browser

Call recordAudio from a click handler: browsers only let a page ask to share a tab in response to one.

import { recordAudio, SparkleAudioError } from "@sparkle/sdk/browser";

const getSession = (request) =>
  fetch("/api/sparkle/audio-session", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(request),
  }).then(async (response) => {
    const body = await response.json();
    if (!response.ok) throw body; // the API's problem: its status and code tell the SDK what to do
    return body;
  });

startButton.addEventListener("click", async () => {
  try {
    const recording = await recordAudio(conversationId, {
      getSession,
      sources: { mic: true, tab: includeMeetingTab },
      onInterim: (text, source) => showInterim(source, text),
      onTurn: (turn) => showTurn(turn.speaker.key, turn.text),
      onError: (error) => console.warn(error.code, error.message),
      onState: (state) => showState(state), // connecting, live, reconnecting, stopped
    });
    stopButton.onclick = () => recording.stop();
  } catch (error) {
    // No microphone, permission refused, or a shared tab without audio.
    if (error instanceof SparkleAudioError) showError(error.code);
  }
});
OptionWhat it does
getSessionRequired. Mints a session on your backend and resolves with { url }. Called once per source, and again for every reconnect.
sourcesmic streams as local, tab as remote. true asks the browser for it; pass a MediaStream of your own to use it as it is. The microphone alone when absent.
languagePassed through to getSession.
onInterimWhat a source is hearing before the turn is final. Each call replaces the last for that source.
onTurnA finished turn. Sparkle has already stored it: don't post it again as a turn of your own.
onErrorA source that stopped on an error. The other source keeps streaming.
onStateThe capture's state: connecting, live, reconnecting or stopped.

The recording it resolves with has pause(), resume() and stop():

  • pause() stops sending audio. The connections stay open on keepalives, so resume() is instant.
  • stop() asks Sparkle to transcribe what it has already received, waits up to five seconds for the last turns, then closes the connections and releases the microphone and the tab.
  • A source stays connecting until Sparkle is ready, which can take up to 10 seconds.
  • If the connection drops, or Sparkle restarts, the SDK reconnects with a new session from getSession, backing off up to 8 seconds between attempts. Audio from while it was down is dropped, not queued.
  • A connection that dropped holds its source for up to 30 seconds, so a new session for it can be refused with conflict.audio_source_streaming until then, after a page reload too. The SDK keeps trying for about 35 seconds before it gives up with that code.
  • When the page is closed, the SDK tells Sparkle to stop, as far as the browser lets it.
  • When the person clicks the browser's own "Stop sharing", the tab source stops; the microphone keeps going.

Speaker keys

Each turn's speaker.key says who spoke:

KeyWho
localThe person at the microphone. Their turns have channel mic.
remote, or remote_{n}Everyone in the shared tab: the first voice heard is remote, each new one after it remote_1, remote_2 and so on. Their turns have channel remote.

Voices are told apart within one connection, so after a reconnect remote_1 may be someone else. speaker.name is null: the audio carries no names.

Browser support

The microphone works in current Chrome, Edge, Firefox and Safari. Sharing a tab's audio depends on the browser:

BrowserTab audio
Chrome, Edge (desktop)Yes. Pick a tab and turn on "Share tab audio". On Windows and ChromeOS, a whole screen too.
FirefoxNo. The shared tab comes back without audio.
SafariNo. The shared tab comes back without audio.
Mobile browsersNo. Phones can't share a tab.

When the shared tab has no audio, recordAudio rejects with audio.no_tab_audio before asking for the microphone, so you can tell the person to share again with tab audio turned on.

Ask people to wear headphones when they share a meeting tab. Through speakers, the microphone also hears the meeting, and the same words come back twice: once as local and once as remote.

Capture needs a secure page (HTTPS, or localhost while you develop). If your site sends a Content Security Policy, allow the SDK's audio worklet, which loads from a blob: URL (script-src blob:, unless your policy already uses 'strict-dynamic'), and the connection (connect-src wss://api.sparkle.ai).

Errors

recordAudio rejects, and onError is called, with a SparkleAudioError. Its source says which source failed, and its code is one of these, or the API's own code when it refused:

codeMeaning
audio.unsupportedThe browser can't capture audio here: an old browser, or a page without HTTPS.
audio.permission_deniedThe person refused the microphone, or cancelled the tab picker.
audio.no_microphoneNo input device was found.
audio.no_tab_audioThe shared tab or screen carries no audio.
audio.capture_failedThe device couldn't be opened for another reason.
audio.session_failedgetSession threw an error that carries no code.
audio.connection_failedThe first connection closed before Sparkle was ready.

When getSession throws the API's problem, its code becomes the error's code. After the source has been live once, a status of 408, 429 or 5xx makes a reconnect try again instead of giving up, except 503 audio.not_configured.

Once live, a source also reconnects when Sparkle ends its connection with audio.session_limit, audio.backpressure, bad_gateway.transcription or conflict.audio_source_streaming. Any other code Sparkle ends it with, such as audio.idle_timeout or conflict.conversation_ended, stops that source and reaches onError. The audio streaming protocol lists them all.

Transcription minutes

Each source is its own stream, and Sparkle meters transcription minutes per stream: an hour with the microphone and a meeting tab is 120 transcription minutes, the microphone alone 60. Pro and Scale pay $0.012 per stream-minute on live keys, billed per second; see Billing. Free includes 60 a month on live keys, and test keys and the dashboard have their own monthly allowance.

Past the allowance, getSession gets a 403 with limit.transcription_minutes or limit.test_transcription_minutes, and the source stops with that code on onError. A source that is already streaming keeps going; a reconnect needs a new session, so after a drop or audio.session_limit it stops with the code instead.

Limits

  • Audio is 16-bit linear PCM, mono, at 16000, 24000, 44100 or 48000 Hz. The SDK uses the rate the browser records at, and resamples to 48000 any other.
  • One connection per source per conversation: a second local stream to the same conversation is refused while the first is open.
  • A connection with neither audio nor keepalives for 30 seconds is closed. The SDK sends keepalives while paused.
  • A session token works once, within 60 seconds of being minted.
  • One connection runs at most as long as your plan allows. The SDK continues on a new session when it ends, as after a drop.

For the wire format, see the audio streaming protocol.

On this page