JavaScript Demo
See how easy it is to integrate high-performance ads into your web app using our lightweight SDK.
Getting started
The JavaScript SDK is designed for any web environment where you can run JavaScript in the browser: static HTML pages, custom chat UIs, or full SPA frameworks. You only need:- A publisher account (to get your
publisherTokenand placementcode) - A way to access your chat messages (with
id,role,content,createdAt)
1. Include the SDK
You will need a publisher account to obtain your
publisherToken and placement code.{publisherToken} with your actual publisher token.
2. Initialize the SDK
The SDK is available as a global variablewindow.KontextSdk.
Import KontextAds() to create an SDK instance and then createSession() to configure a session
for a specific user and conversation.
userIdshould stay the same for the same user across sessions/devices if you want better personalization and frequency capping.conversationIdshould be unique per chat thread or dialog (e.g. a specific chat room, support ticket, or conversation in your app).
3. Add user and assistant messages to the session
For Kontext to decide when and where to show an ad, it needs the conversation history. Add every message (both user and assistant) to the session as it appears.idmust be unique per message and stable (do not change it later).- The same
messageIdwill be used later when you render an ad for that message. - You should call
session.addMessage()every time a new message appears in your chat UI.
4. Display ads
To display an ad, callsession.render() with:
messageId: the ID of the assistant message you want to attach the ad toelement: the DOM node where the ad iframe will be injected
- Kontext will select the best ad for the latest assistant message that has a matching bid.
- The ad is rendered as an
<iframe>insideelementand is fully managed by the SDK. - You can create different containers (e.g. one below each assistant message) and call
session.render()with a differentmessageIdandelementto show multiple ads within the same conversation.
- Render your message list.
- For each assistant message, create a corresponding
<div>for the ad. - Call
session.render({ messageId, element })once the message DOM has been mounted.
API documentation
KontextAds().createSession(options) → session
Creates an ad session tied to a specific user and conversation.
Your publisher token used to fetch ads.
Unique identifier for the user. Must remain stable across visits for personalization, frequency capping, and rewarded ads.
Email of the user.
Unique ID of the conversation or chat thread.
Placement codes available in this conversation. Default:
inlineAd.Character metadata used for contextual ad selection and personalization.
Regulatory object used in this conversation.
Publisher-provided identifier for the user cohort (for A/B testing).
Callback triggered when an event occurs. See Supported events for more details.
session.addMessage(message)
Add a chat message. Every message must have the following properties:
Unique ID of the message.
Message role — must be
user or assistant.Text content of the message.
Timestamp of message creation.
session.render(options) → displays an ad
Renders an inline ad as an <iframe> into a specific DOM container.
ID of the assistant message the ad should be anchored to.
The container where the ad will be injected.
session.destroy() (optional)
Cleans up internal state.Call this when your conversation screen unmounts or session ends.