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
- 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. - Put it in
.env.localasNEXT_PUBLIC_CHABI_CLIENT_ID. It isn't a secret: sign-in uses PKCE, so there's no client secret at all. - Install the SDK and the official OpenAI SDK:
npm install @chabi/js openaiThe 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 itmessages: [{ 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} />;}
dangerouslyAllowBrowseris safe here: the key is the user's own token for your app, not your API key.- For other makers' models (
vendor/modelids), usechabiAI(chabi)from@chabi/js/ai. For Claude, the Anthropic SDK withchabi.anthropic(). Requests in the docs. - Add a sign-out button that calls
getChabi().signOut(), and show the user's balance withawait getChabi().credits().
Before you ship
- Add your production address as a redirect URI in the portal.
- Handle running out of credits kindly:
describeErrorgiveskind: "out_of_credits"and anupgradeUrlto show. - Set
max_tokenssensibly. Chabi holds the most a request could cost before sending it, so a huge cap can refuse a request a user could afford.
Next
- Read the docs: sign-in, requests and errors, with copyable code.
- The AI prompt: paste it into Claude Code, Cursor or another coding agent, and it adds Sign in with Chabi to your app.
- Open the developer portal to register your app and get a client ID.
- Work out your AI bill and earnings with the calculator.