This is the 5-minute integration walkthrough. Before you start, make sure you’ve completed Installation.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.
1. Create a session
ImportcreateSession() to configure a session for a specific user and conversation. The returned Session is the entry point for everything else — feeding messages, creating ads, listening to events.
userIdshould stay the same for the same user across sessions/devices — it powers personalization, frequency capping, and rewarded ads.conversationIdshould be unique per chat thread (a single chat room, support ticket, or conversation in your app).
2. Feed messages to the SDK
The SDK preloads ads in response to new chat messages. CalladdMessage() whenever a user or assistant message is created — both roles are needed for Kontext to understand conversation context.
idmust be unique per message and stable — the same id is referenced later when you create an ad.
3. Mount an ad
To display an ad, create anAd instance bound to the assistant message it should appear under, then mount it into a DOM element. The SDK manages the iframe and its lifecycle.
- Render your message list.
- For each assistant message, render a placeholder
<div>for the ad. - Call
session.createAd(messageId)once the placeholder DOM exists, thenad.mount(element). - When the message scrolls off-screen or the conversation ends, call
ad.destroy()to release the iframe.
session.render({ messageId, element, theme }) is a convenience wrapper around createAd + mount.
4. Tear down
Callsession.destroy() when the conversation ends or the page unmounts. Idempotent and required to cancel in-flight preloads and release iframe resources.
Observing events
Subscribe to the lifecycle of every ad via theonEvent callback. Every event has a stable string name and a typed payload:
ad.filled, ad.viewed, ad.clicked, ad.render-*, video.*, reward.granted) also carries a top-level code field naming the matched placement, so publishers with multiple enabledPlacementCodes can disambiguate. Session-wide events (ad.no-fill, ad.error) omit it.
For SDK-internal diagnostics during integration, also pass an onDebugEvent callback — it receives (name, data?) for every internal step the SDK takes.