Build an app with free inference
Add a chat endpoint, show the generated content, and render the ad that funds inference.
Your server calls Koah and returns content plus an adId when Koah requests an
ad. Your app always shows the content and passes the ID to the Koah SDK only
when one is returned. Not every response includes an ad.
This guide uses an existing Next.js app with signed-in users and the OpenAI TypeScript SDK. For other languages or platforms, start with the quickstart.
- 1
Send a question
Your app sends the user’s question to your server.
- 2
Request inference
Your server calls Koah. Koah runs the model and decides whether to request an ad.
- 3
Return the answer and any ad ID
Koah returns the answer through your server. An adId is included only when Koah requests an ad. No ad payload is included.
- 4
Show the answer and any requested ad
Your app shows the answer. Only if adId is present, call window.koah.displayAd(adId, { target }) in a separate visible slot.
- 5
Support free inference
Koah validates eligible ad views. Ads help fund inference usage.
You will add three files:
| File | What it does |
|---|---|
app/api/chat/route.ts | Calls Koah and returns { content, adId }. |
public/chat.html | Adds a question form, content area, and ad slot. |
public/chat.js | Connects the form to your server and renders the result. |
Set your API key
Add your inference key to .env.local. Keep this key on the server.
.env.local
KOAH_INFERENCE_API_KEY="YOUR_KOAH_INFERENCE_KEY"
Create the server endpoint
Create app/api/chat/route.ts. It accepts a question, calls the model, and sends
only the content and ad ID to the browser.
Replace the auth import with your app's session helper. Use the signed-in
user's ID from the server session, not an ID supplied by the browser.
app/api/chat/route.ts
import OpenAI from 'openai'
import { auth } from '@/auth'
const client = new OpenAI({
baseURL: 'https://inference.koah.ai/v1',
apiKey: process.env.KOAH_INFERENCE_API_KEY,
})
export async function POST(request: Request) {
const session = await auth()
if (!session?.user?.id) {
return Response.json({ error: 'Sign in to continue.' }, { status: 401 })
}
const body = await request.json()
if (typeof body?.question !== 'string' || !body.question.trim()) {
return Response.json({ error: 'A question is required.' }, { status: 400 })
}
const { data, response } = await client.chat.completions.create(
{
model: 'sail/DeepSeek-V4.1-Flash-1017-free',
messages: [{ role: 'user', content: body.question }],
},
{ headers: { 'X-Koah-User-Id': session.user.id } },
).withResponse()
return Response.json({
content: data.choices[0]?.message.content ?? '',
adId: response.headers.get('X-Koah-Ad-Id'),
})
}
The response has this shape:
{
"content": "Inference uses a trained model to generate content from new input.",
"adId": "ad_example_123"
}
When Koah does not request an ad, the X-Koah-Ad-Id header is absent and this
endpoint returns adId: null. Show the content without calling the ad renderer.
You can change the model to any supported model.
Add the form and ad slot
Create public/chat.html. Replace YOUR_PUBLISHER_ID with your public publisher
ID. The scripts load in order: the Koah SDK, then your chat code.
public/chat.html
<form id="chat-form">
<label for="question">Ask a question</label>
<input id="question" name="question" required />
<button type="submit">Send</button>
</form>
<p id="content" aria-live="polite"></p>
<div id="koah-ad-slot"></div>
<script defer src="https://app.koah.ai/js?token=YOUR_PUBLISHER_ID"></script>
<script defer src="/chat.js"></script>
Connect the form
Create public/chat.js. On submit, it sends the question to /api/chat, shows
the content, and calls displayAd only when an ad ID is returned.
Render an ad only when Koah returns an ad ID. When an ID is returned, show that ad in a visible slot below its generated content. Deliberately skipping a requested ad or hiding it violates the Terms of Service for free inference.
The displayAd call below is a proposed API. It is not yet available in the
released JavaScript SDK.
public/chat.js
const form = document.getElementById('chat-form')
const question = document.getElementById('question')
const contentTarget = document.getElementById('content')
const adTarget = document.getElementById('koah-ad-slot')
form.addEventListener('submit', async (event) => {
event.preventDefault()
contentTarget.textContent = 'Loading…'
adTarget.replaceChildren()
const response = await fetch('/api/chat', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question: question.value }),
})
if (!response.ok) {
contentTarget.textContent = 'Could not load content. Please try again.'
return
}
const { content, adId } = await response.json()
contentTarget.textContent = content
if (adId) {
await window.koah.displayAd(adId, { target: adTarget })
}
})
If ad delivery fails, keep the content visible. For a conversation, give each requested ad its own slot beside the associated response.
Try your app
Start your app, sign in, and open /chat.html. Submit a question.
Success looks like this when Koah requests an ad: the generated content appears with a separate ad below it. Your inference key stays on the server.
What is inference in machine learning?
Inference is when a trained model uses new input to make a prediction or generate content. For example, a model can identify an object in a photo or respond to your question.
If the content does not appear, check the /api/chat response. Content without
an ad is expected when adId is null. If an ID is present but no ad appears,
confirm the Koah SDK loaded.
Next steps
- Streaming: show content as it arrives, then render an ad if requested.
- Models: choose a model for your app.
- Rate Limits: check capacity and request higher limits.
- API Reference: explore other request formats.