> ## 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.

# Text session (WebSocket)

> Upgrades to a WebSocket for text conversation turns. **Served on `agent_url`**, the
base URL returned by `POST /v1/sessions` — not necessarily the host that served that
request. Standard HTTP `101 Switching Protocols` response.

**Auth** — non-browser clients send `Authorization: Bearer <token>` on the upgrade
request. Browsers cannot set that header on a WS handshake, so first call
`POST /v1/sessions/{sid}/ws-ticket` and pass the result as `?ticket=<raw>`.

**Sending a message** — one JSON text frame per turn:
```json
{"type": "message", "content": "What are your opening hours?"}
```

**Receiving a response**:
```json
{"type": "response", "content": "We are open Monday to Friday, 9 AM–6 PM.", "message_id": "3fa85f64-..."}
```

**Errors** arrive as a JSON frame on the same connection — the WebSocket is not
closed with a 4xxx code:
```json
{"type": "error", "code": "TURN_ERROR", "message": "INPUT_REJECTED: Contains a blocked phrase."}
```
The `code` field is `TURN_ERROR` for any pipeline failure (guardrail rejection,
FraudGuard block, rate limit, provider error); the real machine-readable reason is
the `SCREAMING_SNAKE_CASE` prefix of `message`, before the first `: `. `SESSION_ENDED`
and `SERVICE_UNAVAILABLE` are sent as distinct `code` values.

**Session expiry warning** — sent once, `SESSION_TIME_WARNING_SECONDS` (default 60s)
before the session's time limit:
```json
{"type": "session_expiring", "message": "Your session will expire in 60 seconds."}
```




## OpenAPI

````yaml /openapi.yaml get /v1/sessions/{sid}/ws
openapi: 3.1.0
info:
  title: Humain Kiosk — Device API
  version: '1.0'
  description: >
    The Device API is consumed by physical kiosk terminals, VoIP adapters, and
    website

    widgets. All requests require a device credential issued from the Humain
    admin panel.


    **Base URL:** `https://api.humain.ai` — there is no separate sandbox host.
    Sandbox is a

    property of the credential, not the URL: issue an `hk_test_…` credential
    from the admin

    panel and call the exact same endpoints. In sandbox mode, text sessions echo
    the input back

    verbatim and voice sessions run a record → beep → echo loop — no LLM call is
    made, per-turn

    guardrail/FraudGuard checks do not run, and no usage is billed. See

    [Sandbox mode](/concepts/sandbox-mode).


    Opening a session returns an `agent_url` — use it as the base URL for every
    subsequent

    real-time call (WebSocket, WebRTC, session end) instead of assuming the same
    host that

    served `POST /v1/sessions`. In most deployments `agent_url` is the same
    public API host

    behind a reverse proxy; treat it as opaque and always use the value
    returned.


    Browser-side integrations that cannot hold a device credential — a website
    widget, a

    single-page app — should use the [Public Gateway](/concepts/public-gateway)
    instead of

    calling these endpoints directly. It runs on a separate host and isn't
    documented in this

    reference; see the linked page for its own request/response shapes.
servers:
  - url: https://api.humain.ai
    description: >-
      Production and sandbox — sandbox behavior is selected by using an hk_test_
      credential, not a different URL
security:
  - DeviceToken: []
tags:
  - name: Sessions
    description: Open, interact with, and close conversation sessions.
  - name: Voice (WebRTC)
    description: Establish real-time voice sessions over WebRTC.
  - name: Webhooks
    description: >
      Manage webhook endpoints and inspect delivery history. Part of the Admin
      API —

      authenticated with the admin session cookie, not a device credential.
  - name: Analytics
    description: |
      Query session and usage analytics for a workspace. Part of the Admin API —
      authenticated with the admin session cookie, not a device credential.
paths:
  /v1/sessions/{sid}/ws:
    get:
      tags:
        - Sessions
      summary: Text session (WebSocket)
      description: >
        Upgrades to a WebSocket for text conversation turns. **Served on
        `agent_url`**, the

        base URL returned by `POST /v1/sessions` — not necessarily the host that
        served that

        request. Standard HTTP `101 Switching Protocols` response.


        **Auth** — non-browser clients send `Authorization: Bearer <token>` on
        the upgrade

        request. Browsers cannot set that header on a WS handshake, so first
        call

        `POST /v1/sessions/{sid}/ws-ticket` and pass the result as
        `?ticket=<raw>`.


        **Sending a message** — one JSON text frame per turn:

        ```json

        {"type": "message", "content": "What are your opening hours?"}

        ```


        **Receiving a response**:

        ```json

        {"type": "response", "content": "We are open Monday to Friday, 9 AM–6
        PM.", "message_id": "3fa85f64-..."}

        ```


        **Errors** arrive as a JSON frame on the same connection — the WebSocket
        is not

        closed with a 4xxx code:

        ```json

        {"type": "error", "code": "TURN_ERROR", "message": "INPUT_REJECTED:
        Contains a blocked phrase."}

        ```

        The `code` field is `TURN_ERROR` for any pipeline failure (guardrail
        rejection,

        FraudGuard block, rate limit, provider error); the real machine-readable
        reason is

        the `SCREAMING_SNAKE_CASE` prefix of `message`, before the first `: `.
        `SESSION_ENDED`

        and `SERVICE_UNAVAILABLE` are sent as distinct `code` values.


        **Session expiry warning** — sent once, `SESSION_TIME_WARNING_SECONDS`
        (default 60s)

        before the session's time limit:

        ```json

        {"type": "session_expiring", "message": "Your session will expire in 60
        seconds."}

        ```
      operationId: streamSessionWS
      parameters:
        - $ref: '#/components/parameters/SessionId'
      responses:
        '101':
          description: Switching Protocols — WebSocket connection established
        '401':
          $ref: '#/components/responses/Unauthenticated'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          description: Session not found or expired (manifest not in Redis).
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  parameters:
    SessionId:
      name: sid
      in: path
      required: true
      description: Session UUID returned by `POST /v1/sessions`
      schema:
        type: string
        format: uuid
        example: 3fa85f64-5717-4562-b3fc-2c963f66afa6
  responses:
    Unauthenticated:
      description: Missing or invalid device token.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error: UNAUTHENTICATED
            message: Missing or invalid device token.
            details: {}
    Forbidden:
      description: >
        Valid token but the request is not permitted. One of:

        - `KIOSK_INACTIVE` — the kiosk's status is not *active*

        - `CREDENTIAL_BLOCKED` — FraudGuard blocked this credential. When
        returned, the response
          includes a `Retry-After` header with the number of seconds until the block expires.
      headers:
        Retry-After:
          description: >
            Seconds until the credential block expires. Only present when
            `error` is

            `CREDENTIAL_BLOCKED`. Do not retry before this time has elapsed.
          schema:
            type: integer
            example: 3600
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          examples:
            kioskInactive:
              summary: Kiosk not active
              value:
                error: KIOSK_INACTIVE
                message: This kiosk is not currently active.
                details: {}
            credentialBlocked:
              summary: Credential blocked by FraudGuard
              value:
                error: CREDENTIAL_BLOCKED
                message: This credential has been blocked due to suspicious activity.
                details:
                  blocked_until: '2026-05-24T15:30:00Z'
                  reason: Excessive rate-limit violations
  schemas:
    Error:
      type: object
      properties:
        error:
          type: string
          description: Machine-readable error code in SCREAMING_SNAKE_CASE.
          example: UNAUTHENTICATED
        message:
          type: string
          description: Human-readable description of the error.
          example: Missing or invalid device token.
        details:
          type: object
          description: >-
            Additional error-specific fields (see individual error
            descriptions).
  securitySchemes:
    DeviceToken:
      type: http
      scheme: bearer
      bearerFormat: hk_live_{43-char-base64url} | hk_test_{43-char-base64url}
      description: >
        Device credential issued from the Humain admin panel under

        **Kiosk → Credentials → Issue new credential**.


        Format: `hk_live_` (production) or `hk_test_` (sandbox) followed by 43
        base64url

        characters (32 random bytes). The raw token is shown only once at
        creation time and

        is never stored in plain text.

````

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