How to Build an AI Chatbot in 10 Minutes
Platform: DevTo | Type: article
How to Build an AI Chatbot in 10 Minutes
Building an AI chatbot used to require months of training and complex infrastructure. But with modern APIs and serverless functions, you can ship a fully functional chatbot in under 10 minutes. Let’s do it.
What We’ll Build
A lightweight chatbot that:
- Accepts natural language queries
- Returns contextual responses using OpenAI’s GPT model
- Stores conversation history for continuity
- Runs on a serverless Node.js backend
Tech stack: Node.js, Express, OpenAI API, Vercel (or any host)
Step 1: Project Setup
mkdir ai-chatbot
cd ai-chatbot
npm init -y
npm install express openai dotenv cors
touch index.js .env
Add your OpenAI API key to .env:
OPENAI_API_KEY=your-key-here
Step 2: The Core Bot Logic
In index.js, create the chatbot endpoint:
import express from 'express';
import cors from 'cors';
import OpenAI from 'openai';
import 'dotenv/config';
const app = express();
app.use(cors());
app.use(express.json());
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// In-memory conversation history (for demo)
const conversations = new Map();
app.post('/api/chat', async (req, res) => {
const { message, sessionId } = req.body;
// Initialize or get conversation history
if (!conversations.has(sessionId)) {
conversations.set(sessionId, [
{ role: 'system', content: 'You are a helpful assistant.' }
]);
}
const history = conversations.get(sessionId);
history.push({ role: 'user', content: message });
try {
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: history,
max_tokens: 300,
});
const reply = completion.choices[0].message.content;
history.push({ role: 'assistant', content: reply });
res.json({ response: reply });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Something went wrong' });
}
});
app.listen(3000, () => console.log('Bot running on port 3000'));
Step 3: The Frontend (5 Lines of HTML)
<!DOCTYPE html>
<html>
<body>
<div id="chat"></div>
<input id="input" placeholder="Type a message..." />
<button onclick="sendMessage()">Send</button>
<script>
const sessionId = crypto.randomUUID();
async function sendMessage() {
const msg = document.getElementById('input').value;
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: msg, sessionId })
});
const data = await response.json();
document.getElementById('chat').innerHTML +=
`<p><strong>You:</strong> ${msg}</p>
<p><strong>Bot:</strong> ${data.response}</p>`;
}
</script>
</body>
</html>
Step 4: Deploy in 60 Seconds
For Vercel:
npm install -g vercel
vercel --prod
That’s it. Your API is live. No database? No problem. The conversation history lives in memory for demo purposes—swap it for Redis or PostgreSQL in production.
When to Go Beyond 10 Minutes
This 10-minute bot is perfect for:
- Internal tools
- Rapid prototyping
- Hackathon demos
For production, add:
- Rate limiting
- Persistence (Firebase, Supabase)
- Streaming responses
- Context window management
The 10-Minute Takeaway
You don’t need a PhD in NLP to build an AI chatbot. With one API call and a handful of lines of code, you can give your users conversational superpowers.
Try it now. Your bot will be answering questions before your coffee gets cold.
🔗 Useful tools (affiliate links)
- ProxyEmpire — residential proxies for scraping & automation: https://toptoday.pw/go/proxyempire
- X-Mode — location intelligence & audience data: https://toptoday.pw/go/xmode.ai
- TopToDay — our AI tools platform: https://toptoday.pw/?ref=goose-seo
- 📲 Telegram: https://t.me/toptodayai
- 🌐 https://toptoday.pw/seo/
Top comments (0)