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

# React

> KioskChat, KioskVoice, and useKioskSession — React components and hooks from @humain/kiosk-js/react.

## Install

```bash npm theme={null}
npm install @humain/kiosk-js
```

```bash yarn theme={null}
yarn add @humain/kiosk-js
```

```bash pnpm theme={null}
pnpm add @humain/kiosk-js
```

Import from the React sub-package:

```typescript theme={null}
import { KioskChat, KioskVoice, useKioskSession } from "@humain/kiosk-js/react"
```

> [!TIP]
>
> During development, issue an **`hk_test_`** credential from the admin panel instead of a live
> `hk_live_` one. Sandbox is a property of the credential, not a different base URL — you call
> the same `https://api.humain.ai` host either way. Sandbox sessions don't consume production
> LLM credits and aren't billed. See [Sandbox mode](https://humain.mintlify.site/concepts/sandbox-mode.md).

## Usage

> [tab] Text chat
>
> The `<KioskChat>` component renders a fully-featured chat UI in a single line. Drop it
> anywhere in your component tree.
>
> ```tsx theme={null}
> import { KioskChat } from "@humain/kiosk-js/react"
>
> export function SupportWidget() {
>   return (
>     <KioskChat
>       credential="hk_live_your_credential_here"
>       onMessage={(msg) => console.log("User said:", msg)}
>       onResponse={(res) => console.log("AI responded:", res)}
>     />
>   )
> }
> ```
>
> ### `<KioskChat>` props
>
> **credential** `string` *required*
>
> Device credential token, `hk_live_…` (production) or `hk_test_…` (sandbox — see
> [Sandbox mode](https://humain.mintlify.site/concepts/sandbox-mode.md)). Pass via an environment variable — never
> hardcode in production client bundles.
>
> **placeholder** `string` (default: "Type a message…")
>
> Input field placeholder text.
>
> **initialMessage** `string`
>
> Message shown in the chat window on first render before the user types anything.
>
> **className** `string`
>
> CSS class applied to the outermost container — use for sizing and layout.
>
> **onMessage** `(message: string) =\> void`
>
> Called each time the user submits a message.
>
> **onResponse** `(response: string) =\> void`
>
> Called each time the AI returns a complete response.
>
> **onError** `(error: KioskError) =\> void`
>
> Called on any API or network error. The component shows a generic error state by default.

> [tab] Streaming chat
>
> For full control over the UI, use the `useKioskSession` hook and build your own chat layout.
> Tokens arrive via an async iterator — update state as each chunk lands.
>
> ```tsx theme={null}
> import { useState } from "react"
> import { useKioskSession } from "@humain/kiosk-js/react"
>
> interface Message {
>   role: "user" | "assistant"
>   content: string
>   streaming?: boolean
> }
>
> export function StreamingChat() {
>   const { session, status, openSession, closeSession } = useKioskSession({
>     credential: "hk_live_your_credential_here",
>   })
>   const [messages, setMessages] = useState<Message[]>([])
>   const [input, setInput] = useState("")
>
>   const sendMessage = async (text: string) => {
>     if (!session) return
>     setMessages(prev => [...prev, { role: "user", content: text }])
>
>     const chunks: string[] = []
>     for await (const event of session.stream(text)) {
>       if (event.type === "chunk") {
>         chunks.push(event.content)
>         // Replace or append the streaming assistant message
>         setMessages(prev => {
>           const last = prev[prev.length - 1]
>           const updated: Message = {
>             role: "assistant",
>             content: chunks.join(""),
>             streaming: true,
>           }
>           return last?.streaming
>             ? [...prev.slice(0, -1), updated]
>             : [...prev, updated]
>         })
>       }
>       if (event.type === "turn_end") {
>         setMessages(prev =>
>           prev.map((m, i) =>
>             i === prev.length - 1 ? { ...m, streaming: false } : m
>           )
>         )
>       }
>     }
>   }
>
>   return (
>     <div>
>       <p>Status: {status}</p>
>       {status === "idle" && (
>         <button onClick={openSession}>Start conversation</button>
>       )}
>       <div>
>         {messages.map((m, i) => (
>           <div key={i} style={{ textAlign: m.role === "user" ? "right" : "left" }}>
>             <span>{m.content}{m.streaming && "▌"}</span>
>           </div>
>         ))}
>       </div>
>       {status === "open" && (
>         <form onSubmit={e => { e.preventDefault(); sendMessage(input); setInput("") }}>
>           <input value={input} onChange={e => setInput(e.target.value)} />
>           <button type="submit">Send</button>
>         </form>
>       )}
>     </div>
>   )
> }
> ```
>
> ### `useKioskSession` props
>
> **credential** `string` *required*
>
> Device credential token, `hk_live_…` (production) or `hk_test_…` (sandbox).
>
> **autoOpen** `boolean` (default: false)
>
> Automatically call `openSession()` when the component mounts.
>
> **onError** `(error: KioskError) =\> void`
>
> Called on unhandled errors within the session.
>
> ### `useKioskSession` return values
>
> **session** `Session | null`
>
> The active session object, or `null` if no session is open.
>
> **status** `"idle" | "opening" | "open" | "closing" | "closed" | "error"`
>
> Current session state. Gate your UI on this value.
>
> **openSession** `() =\> Promise\<void\>`
>
> Opens a new text session.
>
> **closeSession** `() =\> Promise\<void\>`
>
> Closes the current session. Safe to call if no session is open.

> [tab] Voice
>
> The `<KioskVoice>` component handles WebRTC negotiation, microphone access, and audio
> playback for you. It renders a minimal voice UI — a status indicator and a mic button.
>
> ```tsx theme={null}
> import { KioskVoice } from "@humain/kiosk-js/react"
>
> export function VoiceKiosk() {
>   return (
>     <KioskVoice
>       credential="hk_live_your_credential_here"
>       onTranscript={(text) => console.log("Heard:", text)}
>       onResponse={(text) => console.log("AI said:", text)}
>     />
>   )
> }
> ```
>
> ### `<KioskVoice>` props
>
> **credential** `string` *required*
>
> Device credential token, `hk_live_…` (production) or `hk_test_…` (sandbox).
>
> **iceServers** `RTCIceServer[]`
>
> TURN/STUN servers for WebRTC NAT traversal. Defaults to Google's public STUN server.
> Provide your own TURN server for production deployments on restricted networks.
>
> **onTranscript** `(text: string) =\> void`
>
> Called with the recognised speech each time the VAD detects end-of-utterance.
>
> **onResponse** `(text: string) =\> void`
>
> Called with the AI's response text after TTS synthesis completes.
>
> **onStatusChange** `(status: VoiceStatus) =\> void`
>
> Called when the voice session status changes. Status values: `"idle"`, `"connecting"`,
> `"listening"`, `"processing"`, `"speaking"`, `"error"`.
>
> **className** `string`
>
> CSS class applied to the wrapper — use to position the component in your layout.

## Advanced topics

> [accordion] Next.js usage
>
> Both `<KioskChat>` and `<KioskVoice>` are client components — they use browser APIs
> (WebSocket, WebRTC, `navigator.mediaDevices`) and cannot run during SSR.
>
> Add `"use client"` to any file that imports them:
>
> ```tsx theme={null}
> "use client"
>
> import { KioskChat } from "@humain/kiosk-js/react"
>
> export function SupportWidget() {
>   return <KioskChat credential={process.env.NEXT_PUBLIC_HUMAIN_CREDENTIAL!} />
> }
> ```
>
> Do **not** instantiate `HumainKioskClient` at module scope in a Server Component — the
> client constructor accesses `window` and will throw during server render. Instantiate it
> inside a `useEffect` or inside a Client Component.
>
> For credentials in Next.js, use `NEXT_PUBLIC_HUMAIN_CREDENTIAL` for client-side usage
> (sandbox only) or proxy calls through an API route to keep live credentials server-side.

> [accordion] Resetting a session on page navigation
>
> If your app navigates between pages without a full page reload (SPA routing), call
> `closeSession()` in a cleanup function to avoid leaking sessions:
>
> ```tsx theme={null}
> useEffect(() => {
>   openSession()
>   return () => { closeSession() }
> }, [])
> ```
>
> `useKioskSession` also auto-closes the session when the component unmounts if `autoOpen`
> is enabled.

> [!WARNING]
>
> **Never expose live credentials in client-side code.** Your `hk_live_…` token is a long-lived
> secret. In browser environments, either proxy API calls through your own server or issue an
> `hk_test_…` (sandbox) credential instead for public-facing demos. Treat it like a password.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.