Guides
OAuth integrations
Let a third-party app ask a Kalsenti user to approve an API key, with PKCE, one-time codes and no client secret.
On this page
How the flow works
An integration lets someone approve a Kalsenti API key without copying it out of the dashboard. The integration sends them to Kalsenti, they sign in, see the integration’s name, choose the key’s name, spending limit, models and expiry, then approve. Kalsenti returns a one-time code, and the integration exchanges it for the API key.
Integrations are public clients: they get a client_id but no client secret. That is deliberate. A secret shipped in an extension can be read by anyone who has the extension, so the security comes from the checks Kalsenti makes, not from hiding something in the client.
| Step | Who does it | What happens |
|---|---|---|
| Register | Kalsenti staff | Creates a client ID and an exact redirect-URI allow-list. |
| Authorize | The integration | Opens the authorization URL with state and a PKCE S256 challenge. |
| Consent | The user | Signs in, chooses key settings and approves. |
| Exchange | The integration | Sends the code and PKCE verifier to the token endpoint. |
| Use | The integration | Calls the API with the returned key, like any other Kalsenti key. |
Register the integration
Ask Kalsenti to register the integration in the admin console. You need a name, an optional description, and every redirect URI the integration will use. Each URI is matched exactly: no wildcards, no extra paths, no different scheme. A native app can use https, an http loopback address such as http://127.0.0.1:8787/callback, or its own custom scheme with a host and path.
Kalsenti gives you a client ID that starts with oa_. There is no client secret to store.
A loopback redirect is for a desktop or local development app. The port may differ if it is part of a native app’s callback, so register the exact URI the app will use. A web integration must use https.
1. Send the user to authorize
Send the user’s browser to this address. The integration must create a fresh state for every attempt and keep it, and must create a PKCE verifier and send only its S256 challenge.
https://kalsenti.ai/api/auth/oauth2/authorize
?response_type=code
&client_id=oa_your_client_id
&redirect_uri=http%3A%2F%2F127.0.0.1%3A8787%2Fcallback
&scope=kalsenti.api
&state=an_unguessable_value
&code_challenge=base64url_sha256_of_verifier
&code_challenge_method=S256When the user approves, Kalsenti redirects to the registered URI with code and the same state. Compare the returned state with the one you kept before doing anything with the code. A missing or different state means the response is not yours to use.
import { createHash, randomBytes } from "node:crypto"
const verifier = randomBytes(48).toString("base64url")
const challenge = createHash("sha256").update(verifier).digest("base64url")
const state = randomBytes(24).toString("base64url")
const url = new URL("https://kalsenti.ai/api/auth/oauth2/authorize")
url.searchParams.set("response_type", "code")
url.searchParams.set("client_id", CLIENT_ID)
url.searchParams.set("redirect_uri", REDIRECT_URI)
url.searchParams.set("scope", "kalsenti.api")
url.searchParams.set("state", state)
url.searchParams.set("code_challenge", challenge)
url.searchParams.set("code_challenge_method", "S256")
// Keep verifier and state for this attempt; open url in the user's browser.2. The user chooses key settings
The consent page shows the integration’s registered name and the exact redirect URI. The user chooses the key’s name, an optional spending limit and period, an optional model allow-list, and an optional expiry. Then they confirm it is them and approve.
The key is created only when the code is exchanged. If the user closes the page, changes their mind, or the code expires, no key is made. Kalsenti blocks the request instead of guessing when the client is unknown or disabled, the redirect does not match exactly, the state or PKCE challenge is missing, or the account may not create keys.
3. Exchange the code
Exchange the code at the site’s token endpoint. Send JSON; Kalsenti’s endpoint accepts it and returns JSON. Do not send cookies. The code, the redirect URI and the PKCE verifier are the proof.
POST https://kalsenti.ai/oauth/token
Content-Type: application/json
{
"grant_type": "authorization_code",
"client_id": "oa_your_client_id",
"code": "the_code_from_the_redirect",
"redirect_uri": "http://127.0.0.1:8787/callback",
"code_verifier": "the_verifier_you_kept",
"scope": "kalsenti.api"
}The response carries the API key in api_key, along with its ID, name and expiry.
{
"access_token": "…",
"token_type": "Bearer",
"expires_in": 3600,
"api_key": "sk-kalsenti-…",
"key_id": "…",
"key_name": "Oh My Pi",
"key_expires_at": "2027-01-01T00:00:00.000Z"
}Read api_key, not access_token. The OAuth access token is part of the protocol but is not the Kalsenti API credential. The key is shown once in this response and appears in the user’s Developer page.
4. Use the API key
Use api_key as the bearer token for the normal API. It has the limits the user chose, counts towards their balance, and can be revoked or rotated from their dashboard like any other key.
curl https://gateway.kalsenti.ai/v1/models \
-H "Authorization: Bearer $KALSENTI_API_KEY"What Kalsenti checks
- The client must be registered and enabled.
- The redirect URI must match one registered URI exactly, including scheme, host, port and path.
stateis required, and the integration must verify it on return.- PKCE with
S256is required. Plain challenges and missing verifiers are refused. - Codes are short-lived and one-time. A second exchange fails.
- The user must be signed in, unrestricted, and pass the same identity check as creating a key.
- The account’s 20-active-key limit still applies.
- Token and authorization requests are rate limited.
These checks are why a public client is acceptable. They do not make an extension’s code secret, and you should not treat anything shipped in it as secret.
Revoking access
The user can revoke the key from the Developer page. It stops working within about a minute, exactly like a key they made themselves. Deleting the OAuth client in the console stops new authorizations, but it does not revoke keys that were already issued.
Rotating the key from the dashboard gives it a new secret. Anything using the old secret fails, so an integration that stores the key must let the user paste the new one or start the authorization again.