Chabi

Guide

Build an AI app with Next.js

A Next.js chat app whose AI runs on your users' plans, in about 40 lines.

Updated November 2026

What you'll build

A Next.js page where users sign in with Chabi and chat with a model. The AI calls go from the browser straight to Chabi with the user's token, so there's no API key to hide and no server route needed for the AI.

Set up

  1. Register your app in the developer portal. Add http://localhost:3000/ as a redirect URI (and your real address later), and copy the client ID.
  2. Put it in .env.local as NEXT_PUBLIC_CHABI_CLIENT_ID. It isn't a secret: sign-in uses PKCE, so there's no client secret at all.
  3. Install the SDK and the official OpenAI SDK:
npm install @chabi/js openai

The code

One client component, for example app/page.tsx. The Chabi client is made on first use in the browser, because it keeps the user's tokens in localStorage.

"use client";import { useEffect, useRef, useState } from "react";import { Chabi, signInButton, describeError } from "@chabi/js";import OpenAI from "openai"; let chabi: Chabi | undefined;const getChabi = () =>  (chabi ??= new Chabi({    clientId: process.env.NEXT_PUBLIC_CHABI_CLIENT_ID!,    redirectUri: window.location.origin + "/",  })); export default function Chat() {  const [signedIn, setSignedIn] = useState(false);  const [reply, setReply] = useState("");  const button = useRef<HTMLDivElement>(null);   useEffect(() => {    const c = getChabi();    // Finishes a sign-in when the user comes back from Chabi.    c.handleRedirect().catch((e) => setReply(describeError(e).message)).finally(() => setSignedIn(c.signedIn));    const b = signInButton();    b.onclick = () => c.signIn();    button.current?.replaceChildren(b);    return c.onChange((s) => setSignedIn(s));  }, []);   async function ask(text: string) {    const ai = new OpenAI({ ...getChabi().openai(), dangerouslyAllowBrowser: true });    try {      const r = await ai.chat.completions.create({        model: "gpt-6-luna", // your choice; Chabi never swaps it        messages: [{ role: "user", content: text }],      });      setReply(r.choices[0]?.message.content ?? "");    } catch (e) {      setReply(describeError(e).message); // out of credits, signed out, rate limited…    }  }   return signedIn    ? <form action={(f) => ask(String(f.get("q")))}><input name="q" /><p>{reply}</p></form>    : <div ref={button} />;}
  • dangerouslyAllowBrowser is safe here: the key is the user's own token for your app, not your API key.
  • For other makers' models (vendor/model ids), use chabiAI(chabi) from @chabi/js/ai. For Claude, the Anthropic SDK with chabi.anthropic(). Requests in the docs.
  • Add a sign-out button that calls getChabi().signOut(), and show the user's balance with await getChabi().credits().

Before you ship

  • Add your production address as a redirect URI in the portal.
  • Handle running out of credits kindly: describeError gives kind: "out_of_credits" and an upgradeUrl to show.
  • Set max_tokens sensibly. Chabi holds the most a request could cost before sending it, so a huge cap can refuse a request a user could afford.

Next