The Open Source, Self-Hosted AI Chatbot Builder.
Build, manage, and embed AI chatbots powered by Google Gemini. No subscription fees, no data lock-in.
β If you find this project useful, please consider giving it a star!
- π Self-Hosted Foundation: Next.js 16+, MongoDB, and NextAuth. You own the code and the data.
- π§ Gemini AI Powered: Integrated with Google's state-of-the-art Gemini models (2.0 Flash, 1.5 Pro, 1.5 Flash).
- π¨ Visual Bot Builder: Customize system prompts, temperature, and themes (colors, welcome messages, icons).
- π Knowledge Base (RAG): Upload PDF & Word documents. Automatic vector indexing with MongoDB Atlas Vector Search.
- π Rich Text Support: Full Markdown support for bold, italics, lists, and code blocks in chat.
- π Real-Time Analytics: Track bot usage, message count, and performance metrics.
- π¬ Multi-Turn Conversations: Full conversation history with LangChain integration.
- π Embed Anywhere: One-line
<script>tag to add your bot to any website with domain validation. - π Public Share Pages: Standalone, fully-themed chat pages for every bot.
- π‘οΈ Advanced Security: Domain whitelisting, IP-based rate limiting, and admin authentication.
- π Serverless Ready: Optimized for Vercel with Mongoose connection pooling.
First, fork this repository to your GitHub account:
A fork creates your own copy of the repository. You need to do this to deploy to Vercel.
After forking, deploy directly to Vercel with a single click:
Replace
YOUR_GITHUB_USERNAMEwith your actual GitHub username.
During deployment, Vercel will prompt you to add these environment variables:
| Variable | Required | Description | Example |
|---|---|---|---|
ADMIN_EMAIL |
β | Admin login email | admin@example.com |
ADMIN_PASSWORD |
β | Admin login password | MySecurePass123! |
AUTH_SECRET |
β | JWT encryption key (generate: openssl rand -base64 32) |
abc123... |
MONGODB_URI |
β | MongoDB connection string | mongodb+srv://user:pass@cluster.mongodb.net/botx |
GEMINI_API_KEY |
β | Google Gemini API key | AIzaSy... |
Getting Your API Keys:
- MongoDB URI: Create a free cluster at MongoDB Atlas
- Gemini API Key: Get it free from Google AI Studio
| Component | Technology | Version |
|---|---|---|
| Framework | Next.js (App Router) | 16.1.4 |
| Database | MongoDB (Mongoose) | 8.21.0 |
| Auth | NextAuth.js | 5.0.0-beta.30 |
| AI/LLM | Google Gemini API | Latest |
| Vector Search | MongoDB Atlas Vector Search | Native |
| UI Library | React | 19.2.3 |
| Styling | Tailwind CSS | 4 |
| AI Framework | LangChain | 1.2.10 |
Key Features:
- Mongoose connection pooling for serverless (Vercel)
- Automated vector index creation on first bot deployment
- Enterprise-grade RAG with MongoDB Atlas Vector Search
- No subscription required for AI models (BYOK - Bring Your Own Key)
# List all bots
GET /api/bots
Authorization: Admin session required
# Create a new bot
POST /api/bots
Body: { name, systemPrompt, temperature, aiModel, theme }
# Get bot details
GET /api/bots/[id]
# Update bot
PUT /api/bots/[id]
# Delete bot
DELETE /api/bots/[id]# Send message to bot
POST /api/chat
Body: {
botId: "publicId or _id",
message: "User message",
history: [{ role: "user", content: "..." }]
}
Response: { reply: "Bot response" }# Upload document
POST /api/bots/[id]/knowledge
Body: FormData with file
# Delete document
DELETE /api/bots/[id]/knowledge/[docId]Add this single line to your website's HTML:
<script src="https://yourdomain.com/embed.js?botId=YOUR_BOT_PUBLIC_ID"></script>The bot widget will appear at the bottom-right corner.
const response = await fetch("/api/bots", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Customer Support Bot",
systemPrompt: "You are a helpful customer support agent.",
temperature: 0.7,
aiModel: "gemini-2.0-flash",
theme: {
primaryColor: "#8b5cf6",
chatTitle: "Support Bot",
welcomeMessage: "Hello! How can I help?",
},
}),
});
const { bot } = await response.json();
console.log("Bot created:", bot.publicId);const response = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
botId: "YOUR_BOT_PUBLIC_ID",
message: "What are your business hours?",
history: [],
}),
});
const { reply } = await response.json();
console.log("Bot says:", reply);src/
βββ app/
β βββ api/ # API Routes
β β βββ bots/ # Bot management endpoints
β β βββ chat/ # Public chat endpoint
β β βββ auth/ # Authentication routes
β β βββ admin/ # Admin utilities
β βββ dashboard/ # Admin Dashboard Pages
β βββ share/ # Public Bot Chat Pages
β βββ login/ # Admin Login
β βββ globals.css # Global styles
βββ models/ # Mongoose Schemas
β βββ bot.model.ts # Bot configuration
β βββ knowledge-base.model.ts
β βββ knowledge-chunk.model.ts
βββ lib/
β βββ db.ts # MongoDB connection with pooling
β βββ gemini.ts # Gemini API integration
β βββ atlas.ts # Vector search utilities
β βββ rate-limit.ts # Rate limiting logic
βββ components/ # React components
β βββ message-renderer.tsx
β βββ dashboard/ # Dashboard UI components
βββ auth.ts # NextAuth configuration
βββ middleware.ts # Route protection
public/
βββ embed.js # Widget embedding script
βββ favicon.ico
β Admin Authentication: Environment-based credentials with JWT sessions β API Keys: Gemini API key stored server-side, never exposed to client β Domain Whitelisting: Control which domains can embed your bot β Rate Limiting: IP-based rate limiting on chat endpoint (prevents abuse) β Bot Status Control: Enable/disable bots without deletion β Serverless Safe: Connection pooling prevents database exhaustion on Vercel
For Production: Use strong passwords, enable MongoDB IP whitelisting, and enable HTTPS.
- Ensure
.env.localexists in root directory - Check the variable name is exactly
MONGODB_URI - Restart dev server after adding env variables
- Get a free API key from Google AI Studio
- Add it to
.env.localand restart the server - Check you're using Gemini API, not Google Cloud API
- Verify bot is marked as
isActive: true - Check bot
publicIdor_idmatches the request - If using domain whitelisting, verify your domain is in the list
- Index is created automatically on first bot creation
- For manual index setup:
POST /api/admin/setup-index - Verify MongoDB Atlas has vector search enabled (M10+ cluster)
- The API limits requests by IP address
- Wait a few minutes before retrying
- Use a reverse proxy or cache in production
MIT Β© kishorem
Have questions? Open an issue on GitHub!
