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

# Go

> Idiomatic Go SDK for Humain Kiosk — context-aware, goroutine-safe, with streaming support.

## Install

```bash theme={null}
go get github.com/humain/kiosk-go
```

Requires Go 1.21+. No third-party dependencies beyond the Go standard library.

> [!TIP]
>
> During development, initialise the client with an `hk_test_…` sandbox credential instead of
> `hk_live_…`. There is no separate sandbox mode or URL — sandbox behavior comes entirely from
> the credential prefix. No AI credits are consumed and no usage is billed. See
> [Sandbox mode](https://humain.mintlify.site/concepts/sandbox-mode.md).

***

## Quick start

> [tab] Text session
>
> Open a session, send a message, receive the complete response.
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "log"
>
>     kiosk "github.com/humain/kiosk-go"
> )
>
> func main() {
>     client := kiosk.New(kiosk.Config{
>         Credential: "hk_live_your_credential_here",
>     })
>
>     ctx := context.Background()
>
>     session, err := client.OpenSession(ctx, kiosk.SessionOptions{})
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer session.Close(ctx)
>
>     resp, err := session.Message(ctx, "What are your opening hours?")
>     if err != nil {
>         log.Fatal(err)
>     }
>
>     fmt.Println(resp.Text)
>     fmt.Println("Actions called:", resp.ActionsCalled)
> }
> ```

> [tab] Streaming session
>
> Receive tokens as they arrive. Iterate with `stream.Next()` until `io.EOF`.
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "io"
>     "log"
>
>     kiosk "github.com/humain/kiosk-go"
> )
>
> func main() {
>     client := kiosk.New(kiosk.Config{
>         Credential: "hk_live_your_credential_here",
>     })
>
>     ctx := context.Background()
>
>     session, err := client.OpenSession(ctx, kiosk.SessionOptions{})
>     if err != nil {
>         log.Fatal(err)
>     }
>     defer session.Close(ctx)
>
>     stream, err := session.Stream(ctx, "Tell me about your services")
>     if err != nil {
>         log.Fatal(err)
>     }
>
>     fmt.Print("Assistant: ")
>     for {
>         event, err := stream.Next()
>         if err == io.EOF {
>             break
>         }
>         if err != nil {
>             log.Fatal(err)
>         }
>         if event.Type == kiosk.EventChunk {
>             fmt.Print(event.Content)
>         }
>     }
>     fmt.Println()
> }
> ```
>
> Stream event types:
>
> | `event.Type` constant | Description |
> | - | - |
> | `kiosk.EventTurnStart` | The AI has started generating |
> | `kiosk.EventChunk` | A token fragment — read `event.Content` |
> | `kiosk.EventToolCall` | An action endpoint was invoked — read `event.Name`, `event.CallID` |
> | `kiosk.EventToolResult` | Action endpoint returned — read `event.CallID`, `event.Status` |
> | `kiosk.EventTurnEnd` | Generation complete — read `event.MessageCount` |

***

## Client configuration

```go theme={null}
client := kiosk.New(kiosk.Config{
    Credential: "hk_live_your_credential_here",
    Mode:       kiosk.ModeLive,       // or kiosk.ModeSandbox
    Timeout:    30 * time.Second,
    MaxRetries: 2,
})
```

**Credential** `string` *required*

Your device credential token in `hk_live_…` format. Issue credentials from the Humain
admin panel under **Kiosk → Credentials → Issue new credential**.

**Mode** `kiosk.Mode` (default: kiosk.ModeLive)

`kiosk.ModeSandbox` returns instant stub responses without consuming AI credits. Use
`kiosk.ModeLive` for production.

**Timeout** `time.Duration` (default: 30s)

Context deadline applied to each HTTP request. The caller's `context.Context` deadline
takes precedence if shorter.

**MaxRetries** `int` (default: 2)

Number of automatic retries on `503 PROVIDER_ERROR`. Uses exponential back-off starting
at 1 second.

**BaseURL** `string`

Override the API base URL. Defaults to `https://api.humain.ai`.

***

## Session options

```go theme={null}
session, err := client.OpenSession(ctx, kiosk.SessionOptions{
    Mode:     kiosk.SessionModeText,  // default
    Metadata: map[string]string{
        "user_id": "usr_123",
    },
})
```

**Mode** `kiosk.SessionMode` (default: kiosk.SessionModeText)

Session mode. Only `kiosk.SessionModeText` is supported by the SDK — voice sessions
require raw WebRTC or a VoIP integration.

**Metadata** `map[string]string`

Arbitrary key-value pairs attached to the session. Available in your kiosk's action
endpoints as `session.metadata`.

***

## Error handling

The SDK returns typed errors you can check with `errors.As`:

```go theme={null}
import (
    "errors"
    "fmt"
    "time"

    kiosk "github.com/humain/kiosk-go"
)

resp, err := session.Message(ctx, "Hello")
if err != nil {
    var rateLimitErr *kiosk.RateLimitError
    var providerErr  *kiosk.ProviderError

    switch {
    case errors.As(err, &rateLimitErr):
        fmt.Printf("Rate limited — retry after %v\n", rateLimitErr.RetryAfter)
        time.Sleep(rateLimitErr.RetryAfter)

    case errors.As(err, &providerErr):
        if providerErr.Retryable {
            fmt.Println("Provider unavailable — retryable")
        }

    default:
        log.Fatal(err)
    }
}
```

**`*kiosk.RateLimitError` fields:**

**RetryAfter** `time.Duration`

Minimum wait before the next request. Derived from the `Retry-After` response header.

**`*kiosk.ProviderError` fields:**

**Retryable** `bool`

`true` if the error is transient and safe to retry with exponential back-off.

**StatusCode** `int`

The HTTP status code returned by the API (typically `503`).

***

## Concurrent sessions

`*kiosk.Client` is safe to use from multiple goroutines. Each `OpenSession` call creates an
independent server-side session.

> [accordion] Running multiple sessions concurrently
>
> ```go theme={null}
> package main
>
> import (
>     "context"
>     "fmt"
>     "sync"
>
>     kiosk "github.com/humain/kiosk-go"
> )
>
> func main() {
>     client := kiosk.New(kiosk.Config{
>         Credential: os.Getenv("HUMAIN_CREDENTIAL"),
>     })
>
>     questions := []string{
>         "What are your opening hours?",
>         "Do you offer refunds?",
>         "Where are you located?",
>     }
>
>     results := make([]string, len(questions))
>     var wg sync.WaitGroup
>
>     for i, q := range questions {
>         wg.Add(1)
>         go func(i int, q string) {
>             defer wg.Done()
>
>             ctx := context.Background()
>             session, err := client.OpenSession(ctx, kiosk.SessionOptions{})
>             if err != nil {
>                 results[i] = fmt.Sprintf("error: %v", err)
>                 return
>             }
>             defer session.Close(ctx)
>
>             resp, err := session.Message(ctx, q)
>             if err != nil {
>                 results[i] = fmt.Sprintf("error: %v", err)
>                 return
>             }
>             results[i] = resp.Text
>         }(i, q)
>     }
>
>     wg.Wait()
>
>     for i, q := range questions {
>         fmt.Printf("Q: %s\nA: %s\n\n", q, results[i])
>     }
> }
> ```

> [accordion] Using with net/http servers
>
> Create the client once at server startup and close sessions in a deferred call or after
> each HTTP handler completes.
>
> ```go theme={null}
> package main
>
> import (
>     "encoding/json"
>     "net/http"
>     "os"
>
>     kiosk "github.com/humain/kiosk-go"
> )
>
> var kioskClient *kiosk.Client
>
> func init() {
>     kioskClient = kiosk.New(kiosk.Config{
>         Credential: os.Getenv("HUMAIN_CREDENTIAL"),
>     })
> }
>
> func chatHandler(w http.ResponseWriter, r *http.Request) {
>     var body struct{ Message string `json:"message"` }
>     if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
>         http.Error(w, "bad request", http.StatusBadRequest)
>         return
>     }
>
>     session, err := kioskClient.OpenSession(r.Context(), kiosk.SessionOptions{})
>     if err != nil {
>         http.Error(w, "could not open session", http.StatusServiceUnavailable)
>         return
>     }
>     defer session.Close(r.Context())
>
>     resp, err := session.Message(r.Context(), body.Message)
>     if err != nil {
>         http.Error(w, err.Error(), http.StatusInternalServerError)
>         return
>     }
>
>     json.NewEncoder(w).Encode(map[string]string{"response": resp.Text})
> }
>
> func main() {
>     http.HandleFunc("/chat", chatHandler)
>     http.ListenAndServe(":8080", nil)
> }
> ```

***

> [!WARNING]
>
> Never hard-code your credential. Read it from an environment variable:

```python Python theme={null}
import os
from humain_kiosk import KioskClient

client = KioskClient(credential=os.environ["HUMAIN_CREDENTIAL"])
```

```go Go theme={null}
import (
    "os"
    kiosk "github.com/humain/kiosk-go"
)

client := kiosk.New(kiosk.Config{
    Credential: os.Getenv("HUMAIN_CREDENTIAL"),
})
```

```csharp .NET theme={null}
// appsettings.json: { "Humain": { "Credential": "hk_live_..." } }
var client = new KioskClient(new KioskClientOptions
{
    Credential = configuration["Humain:Credential"],
});
```


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