# JavaScript API

> Mount the widget manually and connect a custom transport or subscriber.

[Web documentation](https://threads.teamkrope.com/docs/javascript/) · Inner Threads v1.3.0

## Mount the widget

Include the published stylesheet and provide an HTML mount point.

```html
<link rel="stylesheet" href="/vendor/inner-threads/inner-threads.css">
<div id="staff-chat"></div>
<script type="module" src="/js/staff-chat.js"></script>
```

```js
import { mountInnerThreads } from '/vendor/inner-threads/widget.js'

const target = document.querySelector('#staff-chat')
const chat = mountInnerThreads(target, {
    api: '/inner-threads/api/v1',
    csrf: () => document.querySelector('meta[name="csrf-token"]').content,
    mode: 'inline',
    label: 'Staff chat',
    theme: 'auto'
})

await chat.open()
```

Call `destroy()` before an SPA removes its mount point.

## Options

| Option | Purpose |
| --- | --- |
| `api` | API base URL for the built-in transport |
| `mode`, `view`, `side`, `theme`, `accent`, `label` | Widget appearance |
| `showWatermark` | Set to `false` to hide the linked logo in the inbox header |
| `enterToSend` | Set to `false` to disable sending with Enter |
| `locale`, `strings` | Locale and label overrides |
| `dates` | Date divider overrides using `relative`, `weekday_days`, `format` and `time_zone`, as in the [widget guide](https://threads.teamkrope.com/docs/widget/#chat-dates) |
| `csrf` | Token string or function |
| `transport` | Options forwarded to the default HTTP client |
| `client` | Host-supplied adapter implementing `get` and `write` |
| `subscribe` | Host realtime subscriber |

## Handle

| Method | Behavior |
| --- | --- |
| `open(conversationId?, { focus })` | Open the inbox or a known conversation. Set `focus: false` when opening it during page load. |
| `close()` | Close a floating widget |
| `refresh()` | Refresh the inbox and active thread |
| `destroy()` | Clean up the mount and listeners |

## HTTP client

```js
import { createInnerThreadsClient } from '/vendor/inner-threads/widget.js'

const client = createInnerThreadsClient('/inner-threads/api/v1')
const inbox = await client.get('conversations')
const chat = await client.write('POST', 'conversations/direct', {
    user_id: '42'
})
```

The default client sends JSON, same-origin credentials and the CSRF token from a meta tag. Client options include `base`, `credentials`, `csrf`, `fetcher` and `headers`. Use `FormData` when uploading files. Error responses become `InnerThreadsError` objects with `status`, `message` and `errors`.

## Install browser assets

Blade mounts load the versioned bundled assets by default. For manual JavaScript imports, publish the files with `php artisan vendor:publish --tag=inner-threads-assets --force`. The widget uses the [HTTP API](https://threads.teamkrope.com/docs/api/) for authentication and data, so embedding it from a CDN alone cannot serve conversations.
