Chabi

Developer Reference

AI prompt

One prompt that has an AI coding tool add Chabi to your app.

Updated November 2026

AI prompt

Building with an AI coding tool, like Cursor, Claude Code, Lovable or Bolt? Copy this prompt into it, fill in your client ID and redirect URI from the developer portal, and it adds sign-in, requests and error handling for you.

Add "Sign in with Chabi" to this app, so its AI features run on each user's own Chabi plan instead of my API key. Chabi is compatible with the OpenAI and Anthropic APIs. Full docs: https://chabi.dev/docs

My Chabi client ID: <paste it from https://chabi.dev/developers/portal>
My redirect URI: <the URL users return to after signing in, e.g. https://myapp.com/callback; it must be registered in the portal, and add http://localhost:<port>/callback there for development>

1. Sign-in.
- If the app is JavaScript or TypeScript (a website, Node or Electron), use Chabi's SDK: npm install @chabi/js. It runs the sign-in, keeps and refreshes the tokens (one refresh at a time across tabs), and signs the user out when they remove the app's access. Don't write the flow by hand as well.
- In the browser: import { Chabi, signInButton } from "@chabi/js"; const chabi = new Chabi({ clientId, redirectUri }). Call await chabi.handleRedirect() on every page load: it finishes a sign-in when the user comes back, and throws a ChabiError if one fails (err.info.kind is "access_denied" when they declined, so say so kindly). Put signInButton() where signed-out users see it and call chabi.signIn() on click. chabi.signedIn says whether someone is signed in, chabi.onChange((signedIn, reason) => ...) fires when that changes (reason is signed_in, signed_out, access_removed or expired, including from another tab), and chabi.signOut() signs out.
- In Node or Electron: import { Chabi } from "@chabi/js/node"; const chabi = new Chabi({ clientId, storage, openBrowser: (url) => shell.openExternal(url) }). Register http://127.0.0.1/callback as a redirect URI. Await chabi.load() at start, then chabi.signIn() opens the system browser and resolves once the user is back and signed in. storage is { load, save, clear } for the tokens: in Electron encrypt them with safeStorage, never a plain file.
- Any other language: OAuth 2.0 authorization code flow with PKCE, by hand. There is no client secret.
- Make a random code_verifier (43-128 characters, base64url) and a random state. Save them in localStorage (not sessionStorage), keyed by state, with the time, so the return works even in another tab (email sign-in links often open one).
- Send the user to https://chabi.dev/oauth/authorize with response_type=code, client_id, redirect_uri, code_challenge = base64url(SHA-256(code_verifier)), code_challenge_method=S256 and state.
- When they come back to the redirect URI: if the URL has error=access_denied, they declined, so say so kindly. Otherwise look up the verifier by state and delete that entry so it can't be used twice (no match, or older than 15 minutes, means the sign-in didn't start here: start again; never trust a missing state), remove ?code from the address bar, and POST form-encoded to https://chabi.dev/oauth/token with grant_type=authorization_code, code, redirect_uri, client_id and code_verifier.
- The response has access_token, refresh_token and expires_in (seconds). Save all three, with the expiry time.
- Stay signed in: shortly before the token expires, or when a request fails with 401 and code invalid_token, POST grant_type=refresh_token, refresh_token and client_id to the same endpoint. Each refresh token works once, so always save the new one. Using one twice ends the sign-in (it looks like a stolen token): its access token stops working too, so only one refresh may run at a time across every open tab, not just within one page. In the browser, wrap it in navigator.locks.request("chabi-refresh", ...) and, inside the lock, reread the saved tokens first: if the saved refresh token is no longer the one this tab was about to use, another tab already refreshed, so use its tokens instead (the docs have a copyable example). If refreshing fails (invalid_grant), sign the user in again.
- Add a "Sign in with Chabi" button for signed-out users and a sign-out that clears the tokens. The button is required to show the Chabi key logo before the words "Sign in with Chabi". With @chabi/js, signInButton() makes it (signInButton({ style: "dark" }) for the dark style). Otherwise use this markup and CSS as they are (the gold style, or add class "dark" for the dark style); you may change the size and spacing but don't redraw, recolor, stretch or crop the logo, and keep it at least 20px:
<button class="chabi-signin" type="button">
  <svg width="22" height="22" viewBox="0 0 100 100" aria-hidden="true"><mask id="chabi-key"><rect width="100" height="100" fill="#fff"/><circle cx="33" cy="50" r="15" fill="#000"/><circle cx="33" cy="50" r="7" fill="#fff"/><path d="M45 46L82 46L82 62L77 62L77 54L73 54L73 62L68 62L68 54L45 54Z" fill="#000"/></mask><circle cx="50" cy="50" r="50" fill="currentColor" mask="url(#chabi-key)"/></svg>
  Sign in with Chabi
</button>
.chabi-signin { display: inline-flex; align-items: center; gap: 10px; padding: 10px 18px; border: 0;
  border-radius: 8px; background: #E0A526; color: #121212; font: 700 16px/1.2 system-ui, sans-serif; cursor: pointer; }
.chabi-signin:hover { background: #C99520; }
/* Dark style: */
.chabi-signin.dark { background: #121212; color: #FFFFFF; }
.chabi-signin.dark svg { color: #E0A526; }
- If this is a desktop app without the SDK: use http://127.0.0.1:<any port>/callback or the app's own URL scheme as the redirect URI, and keep the tokens in the OS keychain.

2. AI requests: wherever the app calls OpenAI or Anthropic, use the signed-in user's access_token as the API key. With @chabi/js, create the clients with new OpenAI({ ...chabi.openai(), dangerouslyAllowBrowser: true }) and new Anthropic({ ...chabi.anthropic(), dangerouslyAllowBrowser: true }) (leave out dangerouslyAllowBrowser in Node): they get the base URL and the user's current token on every request, so the app never handles tokens.
- GPT models with the official OpenAI SDK: baseURL "https://chabi.dev/v1", apiKey = access_token. Models: gpt-6-astra, gpt-6.1-sol, gpt-6.1-terra, gpt-6-luna, gpt-5.6-sol, chat-latest. Use max_completion_tokens.
- Claude models with the official Anthropic SDK: baseURL "https://chabi.dev", apiKey = access_token. Models: claude-fable-5-1, claude-opus-5-5, claude-sonnet-5-5, claude-haiku-5-5. max_tokens is required.
- Other vendors' chat models (44 vendors, such as aion-labs, amazon, baidu, bytedance-seed, bytedance, cohere) speak OpenAI's API too. Their ids are "vendor/model" (https://chabi.dev/v1/models lists them). With @chabi/js, use its client for them: import { chabiAI } from "@chabi/js/ai"; const ai = chabiAI(chabi); then ai.chat.completions.create(...) and ai.images.generate(...) (it's the official OpenAI client with Chabi's address and the user's token, needs the openai package, and needs no dangerouslyAllowBrowser). Keep GPT models on new OpenAI(...) and Claude on the Anthropic SDK. Without the SDK, use the OpenAI SDK with the same baseURL and key. Use max_tokens. Images only on models that accept them, function tools only, on models that support them; audio and file parts, other tool types, and OpenRouter's own fields (models, route, provider, plugins), are refused.
- If the app already calls OpenRouter (baseURL https://openrouter.ai/api/v1): change the baseURL to https://chabi.dev/v1 and the key to the user's token as above, and remove the OpenRouter API key from the app. Keep "vendor/model" ids for other makers' models. For OpenAI models use Chabi's plain id from the list above (gpt-…, not openai/gpt-…); Claude models move to the Anthropic SDK with Chabi's plain ids (claude-…, not anthropic/claude-…); Google's models aren't served, so ask me which to use instead. Remove OpenRouter's own fields (models, route, provider, plugins), variants like :free and :online, and moving aliases like ~vendor/…: Chabi refuses them and never picks a model for you.
- Image generation (only if the app makes images): the OpenAI SDK's images.generate with the same baseURL and key, an image model from https://chabi.dev/docs/models#images (OpenAI's as plain ids such as gpt-image-2.6-flare, the others as "vendor/model"), prompt, and optionally n, size, quality, aspect_ratio, resolution, output_format, background, seed, and input_references [{ type: "image_url", image_url: { url } }] for edits. Images come back as base64 (data[i].b64_json); response_format url and streaming are refused.
- Web search (only if the app needs it): Claude, add tools: [{ type: "web_search_20260209", name: "web_search" }] to messages.create; max_uses defaults to 5, at most 10. GPT, use the OpenAI SDK's responses.create (same baseURL and key; POST https://chabi.dev/v1/responses) with tools: [{ type: "web_search" }]; max_tool_calls defaults to 5, at most 10. On /v1/responses, background, previous_response_id, conversation, prompt, service_tier are refused and store is always off, so send the whole conversation in input each time. Not on chat-latest or fine-tuned models. Each search costs 1.5 credits plus the tokens its results add.
- These models reason before answering, and that counts toward the output cap: leave room (e.g. 4096), or replies come back cut off or empty.
- For JSON output, use each provider's own structured-output option, which Chabi passes through: OpenAI response_format: { type: "json_schema", json_schema: { name, schema, strict: true } }; Anthropic output_config: { format: { type: "json_schema", schema } } (alongside effort). Still check the parsed result in code.
- Credits are charged by actual usage, so the cost of a feature varies. Don't promise a fixed price per click in the UI.
- In the browser, set dangerouslyAllowBrowser: true. It's safe here: the token only spends that user's credits in this app.
- Keep the app's current model if it's in the lists above (https://chabi.dev/v1/models has the current list). Otherwise ask me which to use. Chabi never swaps models.
- Speech to text (only if the app transcribes voice or audio): the OpenAI SDK's audio.transcriptions.create with the same baseURL and key, model gpt-transcribe. Send uncompressed WAV only (16 kHz mono 16-bit PCM is plenty), up to 25 MB (about 13 minutes at that format) per file; split longer audio into chunks. Browser recordings (MediaRecorder gives webm or mp4) are refused until converted to WAV: the docs have a copyable function. response_format json (default), text, srt or vtt; no streaming. It costs credits per minute of audio, so skip sending silence.
- Text to speech (only if the app reads text aloud): the OpenAI SDK's audio.speech.create with the same baseURL and key, model gpt-realtime-2.1-mini (or gpt-realtime-2.1 for higher quality), voice marin (others: alloy, ash, ballad, coral, echo, sage, shimmer, verse, marin, cedar), input up to 4096 characters. It returns audio (mp3 by default; response_format mp3, opus, aac, flac, wav, pcm) and streams as it's generated. Optional instructions changes only how it sounds, speed 0.25 to 1.5. Send plain text: markdown and code symbols aren't read well, and long digit strings can be misread. OpenAI's tts-1 and gpt-4o-mini-tts aren't served. It costs credits by what each request used, about 3.77 a minute of speech.
- Remove my own OpenAI or Anthropic API key from any code that runs in the browser or app. If a server route only existed to hide that key, tell me and ask before deleting it.

3. Errors Chabi adds. With @chabi/js, describeError(err) turns any error (Chabi's, the OpenAI SDK's or the Anthropic SDK's) into { kind, message, upgradeUrl, manageUrl, retryAfter }, with kind out_of_credits, app_limit, signed_out, rate_limited and so on; the SDK already refreshes on invalid_token and signs out when access is removed.
- 402 insufficient_credits: the user doesn't have enough credits for this request. Chabi holds the request's most possible cost (input plus the full output cap) up front, so this can happen while the balance is still above zero; a smaller max_tokens may fit. Show a friendly message linking to upgrade_url from the error body. (The Anthropic SDK nests error details one level deeper, in err.error.error.)
- 402 app_limit_reached: the user set a monthly limit on how many credits this app can spend, and this request could go over it. Say so, and link to manage_url from the error body, where they can raise it. A smaller max_tokens may still fit.
- 401: invalid_token means refresh as above; grant_revoked or token_revoked means the user removed access, so sign them in again.
- 429: too many requests. Wait the seconds in the Retry-After header, then retry.

4. Nice to have: show the user's remaining credits from GET https://chabi.dev/v1/credits (balance_credits), and which account is signed in from GET https://chabi.dev/v1/me (name, which can be null). Send the access token as a Bearer token. With @chabi/js: await chabi.credits() and await chabi.me().

Nothing secret goes in the app: Chabi has no client secret. Put the client ID and redirect URI in the app's config (e.g. its environment variables), not hardcoded in several places. When you're done, tell me what you changed and anything I still need to do: in the Chabi developer portal, in the app's environment (and rebuild if those values are baked in at build time), and on the live host. To test, sign in with the app's own button: the portal's Preview consent screen button only shows the consent page.

Check what it changed before you ship, and try a sign-in end to end. The rest of this page is the reference it follows.