API reference

@speechineer/react

Speechineer for React. Wrap your app in SpeechineerProvider once (your API key, or a token your server signs), then call one hook per capability: useSpeechToForm to fill a form by voice, useTextToForm to fill it from text. Each hook gives you the session state as plain values (values, transcript, isListening, error, …) and the controls (start, stop, end / extract). The client, the form definition, the callbacks, and the state types are shared by every package and documented once in the Core reference.

Setup

The provider: give it your credentials (or a client you created) and every hook below it uses the same client.

SpeechineerProviderProps

What the provider takes: either a client you created yourself, or the client options (apiKey / token, account, baseUrl) — the provider creates the client for you and keeps it for as long as the options stay the same.

Extends

  • ClientOptions

Properties

PropertyTypeDescription
baseUrl?stringThe Speechineer API root, without a trailing slash. Defaults to the production API — set it only to target another environment.
apiKey?stringThe API key of the workspace this app belongs to. Unsigned mode only — with a signed token the key travels inside the token.
token?string | TokenProviderA token your server signed, or a function that returns one. Takes precedence over apiKey, and is the only option a Signed API key accepts. With a function the SDK asks for a fresh token on every session start, so expiry is never your problem.
account?AccountThe end user sessions are for, unless a session says otherwise. Required with apiKey; ignored with token (the token carries the account).
client?SpeechineerClientA client you created with createClient. When given, the other options are ignored.
children?ReactNodeThe part of your app that uses Speechineer.

SpeechineerProvider()

ts
function SpeechineerProvider(__namedParameters): Element;

Wrap your app (or the part of it that talks to Speechineer) once. Every hook below it uses the same client.

Parameters

ParameterType
__namedParametersSpeechineerProviderProps

Returns

Element

Example

tsx
import { SpeechineerProvider } from "@speechineer/react";

<SpeechineerProvider apiKey={import.meta.env.VITE_SPEECHINEER_KEY} account={{ key: user.id }}>
  <App />
</SpeechineerProvider>

useSpeechineer()

ts
function useSpeechineer(): SpeechineerClient;

The client the nearest SpeechineerProvider created — for the rare case where you want to create a session yourself (client.speechToForm(...)) instead of using a hook.

Returns

SpeechineerClient

Capability: Speech to form

Fill a form by voice: values stream into values (and onFieldValue) as the user speaks; add transcript: true to also receive the spoken text.

UseSpeechToFormOptions

The options of useSpeechToForm: the session options every framework shares, plus an optional client to use instead of the provider's.

Extends

  • SpeechToFormOptions

Properties

PropertyTypeDescription
onSessionStart?(sessionId) => voidFires once, as soon as the session exists and work can begin. Receives the session id — keep it if you want to correlate it with your own logs.
onStateChange?(state) => voidThe session state changed — its lifecycle, its id, a value, the transcript, an error, or the status of one of its connections. Receives the whole new state; render from it.
onEvent?(event) => voidEvery status event Speechineer emits for this session — progress, warnings, and failures alike. Use it for logging or a live status display.
onError?(error) => voidSomething failed in a way you should handle: a rejected request, a denied microphone permission, a lost connection, or a failure Speechineer reported while the session ran. Read error.code to branch and error.recoverable to decide whether to offer a retry.
onFieldValue?(fieldId, value) => voidOne recognized field value. Called repeatedly while the user speaks, and more than once for the same fieldId when a value is refined — always apply the latest. fieldId is the id you gave the field; value is passed through as received, so cast or validate it the way your form expects.
onTranscript?(text) => voidThe transcript so far — the full accumulated text, not just the newest words, so you can render it directly without stitching updates together.
formFormDefinitionWhich form to fill: the one configured in Speechineer, or one defined right here.
spokenLanguage?stringThe language the user will speak (for example 'en', 'de'). Detected automatically when omitted; set it when you already know, for slightly faster and more reliable recognition.
transcript?booleanAlso stream the spoken text: transcript in the state (and onTranscript) fills while the user speaks. Fixed for the session — change it by creating a new one.
initialValues?Record<string, unknown>Values you already captured, keyed by field id — for example when a user resumes a form that was partly filled in earlier. They appear in values immediately and Speechineer continues from them.
account?AccountThe end user this session is for, when it differs from the client's default account. Ignored when the client authenticates with a signed token — the token carries the account.
client?SpeechineerClientUse this client instead of the nearest SpeechineerProvider's.

UseSpeechToFormResult

What useSpeechToForm returns: the session state spread out for rendering (values, transcript, isListening, isConnecting, isEnding, error, sessionId, status, connections), the controls, and the full state object for when you want to compare by reference.

Extends

  • SessionState

Properties

PropertyTypeDescription
statusSessionStatusWhere the session is in its life — see SessionStatus.
sessionIdstring | nullThe id of the running session, or null before it exists.
errorSpeechineerError | nullThe last error, or null. Cleared when the next start() succeeds.
connectionsSessionConnectionsThe status of each connection: session always, the others when the capability uses them.
valuesReadonly<Record<string, unknown>>The latest recognized value of every field, keyed by the field id you gave it. Starts with initialValues (if any) and grows as values are recognized; a refined value replaces the earlier one.
transcriptstringThe spoken text so far (the full accumulated transcript), or '' when the session does not stream it.
isListeningbooleanThe microphone is on and audio is being sent.
isConnectingbooleanThe session or one of its connections is being established.
isEndingbooleanThe session is being finished (end()), or the microphone is winding down after stop().
start() => Promise<void>Start listening — see SpeechToFormSession.start.
stop() => voidPause listening — see SpeechToFormSession.stop.
end() => Promise<void>Finish the session — see SpeechToFormSession.end.
stateSessionStateThe whole state object (the same values as the spread fields).

useSpeechToForm()

ts
function useSpeechToForm(options): UseSpeechToFormResult;

Fill a form by voice. The session is created once per component and released when the component goes away; the callbacks you pass are always the latest ones. transcript and form.source are fixed for the component's lifetime; the rest of form (key, version, language) is read each time a session starts, so a change applies to the next session after end(). To change everything at once, render a new component (for example with a key).

Parameters

ParameterType
optionsUseSpeechToFormOptions

Returns

UseSpeechToFormResult

Example

tsx
import { useSpeechToForm, FormField } from "@speechineer/react";

const fields = [
  FormField.text("patientName", "Extract the patient full name"),
  FormField.integer("age", "Extract the age in years"),
];

function TalkToForm() {
  const { start, stop, isListening, values } = useSpeechToForm({
    form: { source: "inline", key: "patient-intake", version: "1", language: "en", fields },
  });
  return (
    <>
      <button type="button" onClick={isListening ? stop : () => void start()}>
        {isListening ? "Stop" : "Talk"}
      </button>
      <input value={String(values.patientName ?? "")} readOnly />
    </>
  );
}

Capability: Text to form

No recording at all — turn text the user typed or pasted into field values, as often as you like.

UseTextToFormOptions

The options of useTextToForm: the session options every framework shares, plus an optional client to use instead of the provider's.

Extends

  • TextToFormOptions

Properties

PropertyTypeDescription
onSessionStart?(sessionId) => voidFires once, as soon as the session exists and work can begin. Receives the session id — keep it if you want to correlate it with your own logs.
onStateChange?(state) => voidThe session state changed — its lifecycle, its id, a value, the transcript, an error, or the status of one of its connections. Receives the whole new state; render from it.
onEvent?(event) => voidEvery status event Speechineer emits for this session — progress, warnings, and failures alike. Use it for logging or a live status display.
onError?(error) => voidSomething failed in a way you should handle: a rejected request, a denied microphone permission, a lost connection, or a failure Speechineer reported while the session ran. Read error.code to branch and error.recoverable to decide whether to offer a retry.
onFieldValue?(fieldId, value) => voidOne recognized field value. Called repeatedly while the user speaks, and more than once for the same fieldId when a value is refined — always apply the latest. fieldId is the id you gave the field; value is passed through as received, so cast or validate it the way your form expects.
formFormDefinitionWhich form to fill: the one configured in Speechineer, or one defined right here.
initialValues?Record<string, unknown>Values you already captured, keyed by field id. They appear in values immediately and Speechineer treats them as the baseline for the next extraction.
account?AccountThe end user this session is for, when it differs from the client's default account. Ignored when the client authenticates with a signed token.
client?SpeechineerClientUse this client instead of the nearest SpeechineerProvider's.

UseTextToFormResult

What useTextToForm returns: the session state spread out for rendering, the controls, isExtracting while a call is in flight, and the full state object.

Extends

  • SessionState

Properties

PropertyTypeDescription
statusSessionStatusWhere the session is in its life — see SessionStatus.
sessionIdstring | nullThe id of the running session, or null before it exists.
errorSpeechineerError | nullThe last error, or null. Cleared when the next start() succeeds.
connectionsSessionConnectionsThe status of each connection: session always, the others when the capability uses them.
valuesReadonly<Record<string, unknown>>The latest recognized value of every field, keyed by the field id you gave it. Starts with initialValues (if any) and grows as values are recognized; a refined value replaces the earlier one.
transcriptstringThe spoken text so far (the full accumulated transcript), or '' when the session does not stream it.
isListeningbooleanThe microphone is on and audio is being sent.
isConnectingbooleanThe session or one of its connections is being established.
isEndingbooleanThe session is being finished (end()), or the microphone is winding down after stop().
extract(text) => Promise<Readonly<Record<string, unknown>>>Extract field values from text — see TextToFormSession.extract.
start() => Promise<void>Open the session ahead of time — see TextToFormSession.start.
end() => Promise<void>Finish the session — see TextToFormSession.end.
isExtractingbooleanAn extract call is in flight.
stateSessionStateThe whole state object (the same values as the spread fields).

useTextToForm()

ts
function useTextToForm(options): UseTextToFormResult;

Extract field values from text the user typed or pasted. extract opens the session on first use and merges every result into values. form.source is fixed for the component's lifetime; the rest of form is read each time a session starts, so a change applies to the next session after end().

Parameters

ParameterType
optionsUseTextToFormOptions

Returns

UseTextToFormResult

Example

tsx
const { extract, values, isExtracting } = useTextToForm({
  form: { source: "workspace", key: "patient-intake", version: "1", language: "en" },
});
await extract("Patient Jane Doe, born 1990-03-28.");