The Chatbot · Playbook

The step-by-step.

Twelve steps you can follow start to finish. Copy each prompt into your AI assistant, verify the outcome, then move on to the next.

Before you start

  • You have a project idea — what should the bot know about?
  • Sign up for Anthropic at console.anthropic.com and grab an API key.
  • Sign up for Supabase at supabase.com and grab the URL, anon key, and service role key.
  • Have an IDE with an AI coding assistant — Claude Code, Cursor, or Windsurf.
01

Create the project

Prompt your AI assistant
Create a new Next.js Pages Router project called my-chatbot. Install @anthropic-ai/sdk and @supabase/supabase-js. Add a .env.local with placeholders for ANTHROPIC_API_KEY, SUPABASE_URL, SUPABASE_ANON_KEY, and SUPABASE_SERVICE_ROLE_KEY. Do not commit .env.local.
Verifynpm run dev starts on http://localhost:3000.
02

Add your API keys

  • Open .env.local.
  • Paste your Anthropic API key.
  • Paste your Supabase URL and both keys.
  • Restart npm run dev.
03

Build the chat widget UI

Prompt your AI assistant
Add a chat widget to my Next.js site. A fixed bottom-right floating button that opens a panel with a header, a scrollable message list, and a text input. Use CSS modules. Match a dark theme with an amber accent. Do not wire up any backend yet — just the UI, with a dummy "hello" message hard-coded in the list.
VerifyButton appears bottom-right; clicking opens the panel; dummy message renders.
04

Build the backend endpoint

Prompt your AI assistant
Create a Next.js API route at pages/api/chat.js. Accept POST with {messages: [{role, content}]} in the body. For now, just return {answer: "Echo: " + last user message}. Add basic input validation.
Verifycurl -X POST localhost:3000/api/chat -H "Content-Type: application/json" -d '{"messages":[{"role":"user","content":"hi"}]}' returns the echo.
05

Wire the widget to the endpoint

Prompt your AI assistant
Update the chat widget: when the user sends a message, POST the message history to /api/chat, show a typing indicator while waiting, then render the response as a bot bubble. Maintain the history in React state.
VerifyTyping a message shows the echo bubble.
06

Add Claude

Prompt your AI assistant
In pages/api/chat.js, replace the echo with a real call to Claude. Use @anthropic-ai/sdk. Model: claude-sonnet-4-5. Max tokens: 1024. Add a system prompt: "You are a friendly assistant for a company called {your company}." Read the API key from process.env.ANTHROPIC_API_KEY.
VerifySend a real message and get a real response from Claude.
07

Set up the database

Prompt your AI assistant
Create a SQL migration for Supabase with three tables: conversations (id, visitor_id, started_at), messages (id, conversation_id, role, content, ts), and chat_leads (id, name, email, phone, message, captured_at). Enable row-level security. Anon can insert; only the service role can read. Give me the SQL as a copy-paste block.
  • Run the SQL in Supabase → SQL Editor.
  • Verify tables appear in the Table Editor.
08

Persist conversations

Prompt your AI assistant
Update pages/api/chat.js: before calling Claude, insert the incoming user message into the messages table. After Claude replies, insert the assistant message too. Track the conversation_id in the request; if none, create a new conversation row first. Use the service role key server-side.
VerifySend a few messages, then check Supabase — messages appear in the table.
09

Add lead capture

Prompt your AI assistant
Extend the system prompt so Claude appends <<LEAD>>{json}<<END_LEAD>> when the visitor gives their name, email, or phone. In pages/api/chat.js, parse that block out of the response, insert it into chat_leads, and strip the block from the returned text so the user doesn't see it.
VerifyTell the bot "I'm Alex, alex@test.com". A row appears in chat_leads and the JSON block is stripped from the visible response.
10

Add safety

Prompt your AI assistant
Add basic safety to pages/api/chat.js: rate-limit by IP (max 20 requests per minute), reject messages longer than 4000 characters, and cap the message history sent to Claude at the last 20 turns.
VerifySend 25 messages fast — the 21st onward gets rate-limited.
11

Deploy

Prompt your AI assistant
Prepare the app for Vercel deployment. Confirm .env.local is in .gitignore. Give me a checklist of every environment variable I need to add in the Vercel dashboard.
  • Push to GitHub.
  • Import the repo into Vercel.
  • Add environment variables in Vercel (all four from Step 02).
  • Deploy.
  • Test the widget on the deployed URL.
12

Monitor and iterate

  • Watch Vercel function logs for errors (first 24 hours).
  • Watch the Anthropic usage dashboard for cost.
  • Read your chat_leads table weekly.
  • Improve the system prompt based on real conversations.
Rule of thumb

Small verifiable steps, real error messages, visual verification after every step. If a step fails, tell the AI exactly what happened and paste the error. Never let the AI write more than one chunk between tests.

Want the story instead?
The Article
Read the prose version.

Same build, written as a story. Better for handing to someone who wants the context and reasoning before diving into the how-to.

Read the article

Rather have us build it?

Skip the twelve steps. We'll deliver a custom Claude-powered assistant for your business — trained on your voice, wired to your data, deployed on your infrastructure.

Admin
Mirror Advisors
Ask anything — AI assistant