Authentication
The Agent SDK authenticates with the Kapa API using short-lived session tokens. Your server creates these tokens using your API key, and the SDK uses them to stream responses.
How it works
- The browser calls your server endpoint (e.g.
POST /api/session) - Your server calls the Kapa session API with the
X-API-Keyheader - Kapa returns a session token (valid for 1 hour)
- Your server forwards the token to the browser
- The SDK uses the token for all subsequent API calls
The API key never leaves your server.
Session endpoint
POST https://api.kapa.ai/agent/v1/projects/{projectId}/agent/sessions/
Headers:
X-API-Key: your-api-key
Response:
{
"session_token": "NRd60UqDpLSeeIFzCfmj5dxiRDOJL8G7aSXVzpQ0pPusbe9kHIjEymznutrJu6uf",
"expires_at": "2026-03-17T09:54:51.165812Z"
}
Server-side proxy example
Your getSessionToken function should call your own server, which proxies the request:
getSessionToken={async () => {
const res = await fetch('/api/session', { method: 'POST' });
if (!res.ok) throw new Error('Session creation failed');
return res.json();
}}
Your server just passes through the Kapa API response:
const response = await fetch(
`https://api.kapa.ai/agent/v1/projects/${projectId}/agent/sessions/`,
{ method: 'POST', headers: { 'X-API-Key': apiKey } },
);
res.json(await response.json());
The SDK accepts the raw Kapa API response ({ session_token, expires_at }) directly — no transformation needed. It also accepts the normalized format ({ token, expiresAt }).
Token lifecycle
The SDK manages tokens automatically:
- Lazy fetching —
getSessionTokenis not called until the user sends their first message - Caching — tokens are cached and reused for subsequent requests
- Auto-refresh — tokens are refreshed 30 seconds before expiry
- Retry on 401 — if a request fails with 401, the token is cleared and a fresh one is fetched
- Deduplication — concurrent refresh calls are deduplicated (only one
getSessionTokencall at a time)
Getting your credentials
API key
- Log in to your Kapa dashboard
- Navigate to Settings → API Keys
- Create or copy an existing API key
- Store it as a server-side environment variable (e.g.
KAPA_API_KEY)
Project ID and Integration ID
- In your Kapa dashboard, go to Integrations → Agent
- Create an Agent integration if you don't have one
- Copy the Project ID and Integration ID from the integration setup page
- The Project ID is used both in the session endpoint URL and as the
projectIdprop onAgentProvider - The Integration ID is passed as the
integrationIdprop onAgentProvider