๐Ÿ’ฌ

AI Chat with Web UI

๐Ÿง‘โ€๐Ÿณ Cookโฑ๏ธ 30 minutes

๐Ÿ“‹ Suggested prerequisites

  • โ€ขAt least one terminal chatbot completed
  • โ€ขNode.js installed

What you'll build

A complete and functional web chat application with artificial intelligence. You'll have a modern interface with conversation bubbles, a message input field, a "typing..." indicator, and responses rendered in Markdown. Your terminal chatbot will transform into something anyone can use from their browser.


Why a web interface?

TerminalWeb UI
Only you can use itAnyone with the link
Plain textNice design, markdown
One conversationMultiple chats
Copy/paste is hardClick to copy

Step 1: Choose your API

Use the same API from your previous chatbot:

APIEnvironment variable
GeminiGEMINI_API_KEY
ClaudeANTHROPIC_API_KEY
OpenAIOPENAI_API_KEY
Ollama(none, runs local)

Step 2: Ask an AI to generate the app

Open Google AI Studio, ChatGPT, or Claude.

Copy and paste this prompt (change the API to your choice):

I need an AI chat application using Next.js and React that:

- Uses the Gemini API (GEMINI_API_KEY variable)
- Has a modern interface with Tailwind CSS
- Shows message history in bubbles
- Has a fixed input at the bottom
- Shows a "typing..." indicator while waiting
- Renders markdown in responses
- Is responsive (works on mobile)

Give me:
1. Commands to create the project
2. Code for each file
3. How to run it

๐Ÿ’ก Tip: If you prefer another API, change "Gemini" to "Claude", "OpenAI", or "Ollama".


Step 3: Follow the instructions

The AI will give you something like:

npx create-next-app@latest my-chat --typescript --tailwind
cd my-chat
npm install @google/generative-ai react-markdown

Then it will give you code for several files. Create them in your editor.


Step 4: Configure your API key

# macOS/Linux
export GEMINI_API_KEY="your-key-here"

# Windows PowerShell
$env:GEMINI_API_KEY="your-key-here"

Step 5: Run the app

npm run dev

Open http://localhost:3000


Did it work?

You should see something like:

  • Input to write messages
  • Chat bubbles (you vs AI)
  • Responses with markdown formatting

If something failed

ErrorCauseSolution
Module not foundMissing dependencyRun npm install again
API key not validKey misconfiguredCheck the export in same terminal
Blank screenReact errorCheck browser console (F12)
CORS errorCalling from frontendAPI must be in a Next.js API route
Markdown not renderingMissing react-markdownnpm install react-markdown
Styles not workingTailwind misconfiguredCheck tailwind.config.js

Optional improvements

Ask the AI again:

  • "Add a button to copy responses"
  • "Add dark mode"
  • "Save history to localStorage"
  • "Add a model selector"

Each improvement is a new prompt. The AI updates the code.


Basic architecture

my-chat/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ page.tsx        # Chat UI
โ”‚   โ””โ”€โ”€ api/
โ”‚       โ””โ”€โ”€ chat/
โ”‚           โ””โ”€โ”€ route.ts # Endpoint that talks to the AI
โ”œโ”€โ”€ components/
โ”‚   โ””โ”€โ”€ ChatMessage.tsx  # Bubble component
โ””โ”€โ”€ package.json

๐Ÿ’ก Important: API calls should be made from the server (route.ts), not from the client. This protects your API key.


What did you learn?

Before (Terminal)Now (Web)
Node's readlineReact components
console.logUI rendering
Local processWeb server

The flow is still:

  1. User types
  2. App sends to API
  3. API responds
  4. App shows response

Next step

โ†’ Public AI Chat with Auth โ€” Put it on the internet with login


Want to understand more?

โ†’ What is an LLM? โ†’ Terminal basics