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

# API reference

> Every type and method exposed by the Kotlin SDK.

## `KontextAds.createSession(...)`

```kotlin theme={null}
public fun createSession(context: Context, options: SessionOptions): Session
```

Returns a new `Session` configured from the given `SessionOptions`. Fires `/init` in the background. Pass `context.applicationContext` so the session is not tied to any single Activity.

## `SessionOptions`

**publisherToken** `String` *required*

Your unique publisher token.

**userId** `String` *required*

Stable identifier for the end user. Used for personalization, frequency capping, and rewarded ads.

**conversationId** `String` *required*

Unique ID of the current conversation / chat thread.

**enabledPlacementCodes** `List\<String\>?`

Placement codes to request ads for. Defaults to `["inlineAd"]` when null or empty.

**adServerUrl** `String?`

Override for the ad-server base URL. Leave null to use Kontext's production endpoint.

**character** `Character?`

AI character metadata for contextual targeting.

**Properties**

**id** `String` *required*

**name** `String` *required*

**avatarUrl** `URI` *required*

A `java.net.URI` — construct with `URI.create("…")`, not a raw `String`.

**greeting** `String?`

**persona** `String?`

**tags** `List\<String\>?`

**isNsfw** `Boolean?`

**variantId** `String?`

Publisher-defined cohort identifier (e.g. for A/B testing).

**regulatory** `Regulatory?`

Privacy / consent signals. TCF (`gdpr` / `gdprConsent`) is collected automatically if a TCF-compliant CMP is integrated — set manually only for COPPA, GPP, or US Privacy.

**Properties**

**gdpr** `Int?`

0, 1, or null.

**gdprConsent** `String?`

IAB TCF v2 consent string.

**coppa** `Int?`

0, 1, or null.

**gpp** `String?`

Global Privacy Platform string.

**gppSid** `List\<Int\>?`

GPP section IDs that apply.

**usPrivacy** `String?`

CCPA / LSPA string.

**userEmail** `String?`

End-user email for frequency-cap deduplication.

**advertisingId** `String?`

GAID you collected yourself. Takes priority over the SDK's automatic collection.

**onEvent** `(AdEvent) -\> Unit`

Callback invoked on every ad lifecycle event. Called on the main thread.

**onDebugEvent** `(String, Any?) -\> Unit`

Optional diagnostic stream — fires for every internal SDK event (preload start/end, iframe lifecycle, geometry updates, etc.). Useful during integration. Leave null in production.

## `Session`

**addMessage(message, options)** `method`

Append a `Message` to the conversation. Synchronous (not `suspend`). User messages trigger a debounced preload; assistant messages let the SDK link the matched ad to the placement.

```kotlin theme={null}
session.addMessage(Message(id = "m1", role = Role.USER, content = "Hi"))
session.addMessage(
    Message(id = "m2", role = Role.USER, content = "Hi again"),
    AddMessageOptions(trackOnly = true),
)
```

When `trackOnly = true`, the preload is sent for analytics but no ad is generated.

**createAd(messageId, options)** `method`

Returns an `Ad` for the given `messageId`. Idempotent — repeated calls with the same `messageId` + `code` + `theme` return the same `Ad`. Cache the result.

```kotlin theme={null}
val ad = session.createAd("m2")
val sidebar = session.createAd("m2", AdOptions(code = "sidebar", theme = "dark"))
```

**updateOptions(partial)** `method`

Live-update preload-scoped fields. See [Guides → Live-updating session options](https://docs.kontext.so/sdk/kotlin/guides.md#live-updating-session-options).

**destroy()** `method`

Tear down the session: cancel preloads, destroy ads, release WebView resources. Idempotent. `close()` is an alias (implements `AutoCloseable`).

**events** `SharedFlow\<AdEvent\>`

Hot Flow that delivers the same events as `onEvent`. Useful for ViewModels and coroutine pipelines.

**messages** `List\<Message\>`

Read-only snapshot of messages tracked by the session.

**sessionId** `UUID?`

Server-assigned session ID. `null` until the first successful preload.

**disabled** `Boolean`

`true` if the server has permanently disabled the session via the `/init` response (e.g. geo-restriction). Subsequent preloads are skipped.

**destroyed** `Boolean`

`true` after `destroy()` is called.

## `MutablePublisherOptions`

Subset of `SessionOptions` accepted by `session.updateOptions(...)`. Every field is optional — non-null overwrites, null leaves unchanged.

**variantId** `String?`

**regulatory** `Regulatory?`

**userEmail** `String?`

**advertisingId** `String?`

## `Message`

**id** `String` *required*

Unique message ID.

**role** `Role` *required*

`Role.USER` or `Role.ASSISTANT`.

**content** `String` *required*

Message text.

**createdAt** `Date`

Defaults to `Date()`.

## `AdOptions`

**code** `String`

Placement code. Defaults to `"inlineAd"`.

**theme** `String?`

UI theme hint forwarded to the ad iframe (e.g. `"dark"`).

## `AddMessageOptions`

**trackOnly** `Boolean`

When `true`, the preload is still sent (for analytics) but no ad is generated for this message. Defaults to `false`.

## UI

### `InlineAd` (Compose, recommended)

```kotlin theme={null}
@Composable
public fun InlineAd(
    messageId: String,
    session: Session,
    code: String? = null,
    theme: String? = null,
    modifier: Modifier = Modifier,
)
```

Mounts the ad for the given `messageId`. Internally calls `session.createAd(...)`, attaches a pooled `WebView`, and reports container geometry back to the iframe. Safe to compose in a `LazyColumn` — the underlying `Ad` and `WebView` survive recomposition and scroll-off-screen recycling.

A second overload accepts a pre-resolved `Ad` directly:

```kotlin theme={null}
@Composable
public fun InlineAd(ad: Ad, modifier: Modifier = Modifier)
```

### `InlineAdView` (View interop)

```kotlin theme={null}
public class InlineAdView(context: Context) : FrameLayout {
    public var onHeightChange: ((Float) -> Unit)?
    public fun bind(messageId: String, session: Session, code: String? = null, theme: String? = null)
}
```

For apps not on Compose. Call `bind(messageId, session)` once after inflating; observe `onHeightChange` to resize the surrounding container (e.g. in a `RecyclerView` row).

## `AdEvent`

`AdEvent` is a sealed class with one typed payload per case. Every case has a stable string identifier accessible via `event.name`.

### `AdEvent.Filled` — wire name `ad.filled`

An ad was returned and linked to the placement.

**Filled**

**bidId** `UUID`

**code** `String`

Placement code this ad was matched to. Required for publishers with multiple `enabledPlacementCodes`.

**revenue** `Double?`

### `AdEvent.NoFill` — wire name `ad.no-fill`

No ad was returned for the placement (server skipped).

**NoFill**

**skipCode** `String`

Reason the ad was skipped.

### `AdEvent.AdHeight` — wire name `ad.height`

The ad iframe reported a new height. Use to size the surrounding container in `RecyclerView` rows. The `InlineAd` Compose composable handles this for you automatically.

**AdHeight**

**bidId** `UUID`

**messageId** `String`

**height** `Float`

### `AdEvent.Viewed` — wire name `ad.viewed`

The ad was viewed by the user (IAB MRC viewability standard).

**Viewed**

**bidId** `UUID`

**content** `String`

**messageId** `String`

**format** `String`

**revenue** `Double?`

### `AdEvent.Clicked` — wire name `ad.clicked`

The user clicked the ad.

**Clicked**

**bidId** `UUID`

**content** `String`

**messageId** `String`

**url** `String`

**format** `String`

**area** `String`

Region of the ad that was clicked.

### `AdEvent.RenderStarted` — wire name `ad.render-started`

The first token of the ad content was received.

**RenderStarted**

**bidId** `UUID`

### `AdEvent.RenderCompleted` — wire name `ad.render-completed`

Ad content streaming finished.

**RenderCompleted**

**bidId** `UUID`

### `AdEvent.Error` — wire name `ad.error`

The SDK encountered an error while serving an ad.

**Error**

**message** `String`

**errCode** `String`

### `AdEvent.VideoStarted` — wire name `video.started`

**VideoStarted**

**bidId** `UUID`

### `AdEvent.VideoCompleted` — wire name `video.completed`

**VideoCompleted**

**bidId** `UUID`

### `AdEvent.RewardGranted` — wire name `reward.granted`

Fired for rewarded-ad flows after the user qualifies for a reward.

**RewardGranted**

**bidId** `UUID`


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