Skip to content
VelosyllOpen studio

Record speech in React

Use the SDK hook to render recording state, provisional speech, final text and the saved recording ID.

Before you start

  • Open your running Velosyll studio and sign in as the workspace owner. The examples use the selected project's processing credits.
  • Allow microphone access on the loopback studio origin. Start capture with a button press in a browser that supports AudioWorklet. Live recognition uses 5 credits per started second of streamed audio (300 per minute).
  • Use Node.js 22.17 or newer, npm and an empty directory for the example React application.

1. Install the browser SDK

Create an example app or use your existing React/Vite project. Fetch the SDK archive from your Velosyll installation. The hook supports React 18 and 19. React and Vite are application dependencies.

Install in your example app · shell
export VELOSYLL_URL=http://127.0.0.1:8464
npm init -y
npm install react@19 react-dom@19
npm install --save-dev vite@8
curl -fSLo "velosyll-speech-0.1.0.tgz" "$VELOSYLL_URL/sdk/downloads/velosyll-speech-0.1.0.tgz"
npm install --offline --no-audit --no-fund "./velosyll-speech-0.1.0.tgz"
mkdir -p public/assets
cp node_modules/@velosyll/speech/capture.js public/assets/capture.js

2. Create the entry point and component

Save both files in the example app. Keep the client stable across component renders. The hook handles repeated starts, stops pending setup, drains the recording when Finish is pressed and cancels on unmount.

index.html · htmlDownload index.html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>React microphone</title>
<div id="root"></div>
<script type="module" src="./main.jsx"></script>
</html>
main.jsx · jsxDownload main.jsx
import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import { SpeechClient } from "@velosyll/speech";
import { useSpeechRecording } from "@velosyll/speech/react";

const client = new SpeechClient({
  baseURL: location.origin,
  async getToken(scope) {
    const response = await fetch("/v1/client-tokens", {
      method: "POST", credentials: "same-origin",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ scope, expires_in: 60 }),
    });
    if (!response.ok) throw Error("Stream authorization failed (" + response.status + ").");
    return (await response.json()).token;
  },
});

function Recorder() {
  const { status, partial, transcript, start, stop, error } = useSpeechRecording(client);
  const [saved, setSaved] = useState("");
  function record() {
    setSaved("");
    void start({
      language: "en", workletURL: "/assets/capture.js",
      onEvent(event) { if (event.type === "done") setSaved(event.id || ""); },
    });
  }
  return <main>
    <h1>Microphone transcript</h1>
    <button disabled={status !== "idle"} onClick={record}>Record</button>
    <button disabled={status === "idle" || status === "finishing"} onClick={stop}>Finish</button>
    <p role="status">{error || (saved ? "Saved recording " + saved : status)}</p>
    <p>{transcript} <em>{partial}</em></p>
  </main>;
}
createRoot(document.getElementById("root")).render(<Recorder />);

3. Run your local React application

Save this Vite configuration beside package.json. Sign in to your studio on 127.0.0.1, then open http://127.0.0.1:5173. The local proxy forwards the token request and the capture connection. A deployed application should use its own authenticated server to issue a scoped stream token.

Loopback development configuration · javascriptDownload vite.config.mjs
import { defineConfig } from "vite";

const studio = process.env.VELOSYLL_URL || "http://127.0.0.1:8464";
export default defineConfig({
  server: {
    host: "127.0.0.1", port: 5173, strictPort: true,
    proxy: {
      "/v1/client-tokens": { target: studio, changeOrigin: false },
      "/v1/listen": { target: studio, ws: true, changeOrigin: false }
    }
  }
});
Start the application · shell
npx --no-install vite

Expected result

The hook replaces provisional text, appends final text and reports completion after the server saves the recording.

Troubleshooting

Authorization returned 401 or 403.
Sign in as the workspace owner on the exact origin serving the example. For a member-only first-party UI, use the SDK's same-origin useStudioSession mode. A separate application needs a protected server-side token issuer.
The app works on the studio but fails on a standalone Vite development port.
Use the loopback hostname 127.0.0.1 for both your studio session and the example. Verify that Vite forwarded the token request and the WebSocket upgrade.

Explore further