I added an AI chatbot to a static website. Please don't put your API key in the frontend like I almost did
A few weeks ago a small business owner asked me for something that sounded easy:
"Can you add an AI chatbot to my website? It should answer customer questions."
The website was just HTML, CSS and a bit of JavaScript. No backend. No database. I thought, okay, I will call the AI API from the browser, done in one hour.
It worked on the first try. And then I opened DevTools and saw my API key sitting in the Network tab for everyone to copy.
Yeah. That was a bad moment.
Why the "just call the API from the browser" idea is a trap
If you work only on the frontend, this is the fast way:
// Please do NOT do this in production fetch("https://some-ai-provider.com/v1/chat", { method: "POST", headers: { Authorization: "Bearer sk-my-real-secret-key" }, body: JSON.stringify({ message: "Hello" }), }); It works, but here is what can go wrong:
- Anyone can steal your key. Open DevTools, copy it, done. Then they use your credits.
- Nobody is stopping spam. A bot can send 10,000 messages in one night. You wake up to a big bill.
- The bot knows nothing about your business. It gives generic answers like "I am not sure about your return policy." Not great for a customer.
- You don't get leads. Someone asks about pricing, leaves, and you never know who they were.
The "right" fix is to build a small backend that hides the key. But for a simple business website, that means a server, rate limits, a database, spam protection, and someone to maintain it. That is a lot of work for one chat bubble.
Option 1: Build your own small proxy
You keep the key on a server and your frontend talks to your server.
// server.js (Node + Express, very simple version) import express from "express"; const app = express(); app.use(express.json()); app.post("/chat", async (req, res) => { const r = await fetch("https://your-ai-provider.com/v1/chat", { method: "POST", headers: { Authorization: `Bearer ${process.env.AI_KEY}`, // key stays on the server "Content-Type": "application/json", }, body: JSON.stringify({ message: req.body.message }), }); res.json(await r.json()); }); app.listen(3000); This is better. The key is safe now. But the work is not finished. You still need:
- rate limiting per visitor
- CORS and allowed domains
- a daily cost limit
- a way to teach the bot your FAQ or docs
- saving leads and support requests
- a chat window that looks good on mobile
Each one is a small task. Together they are a few weeks.
Option 2: Use a ready chatbot widget (what I ended up doing)
I tried RSFlowHub for this. Full honesty: I built it, so I am a bit biased. But the reason I made it is exactly this problem.
The idea is simple. You paste one script tag, and the settings live in a dashboard, not in your code.
<script src="https://rsflowhub.com/widget/v1/rsflowhub-widget.js" data-widget-token="wt_sec_your_token_here" async ></script> That's all. No backend and no AI key in your page. The token only works for the domains you allow, so if someone copies it to another site, it won't work.
From the dashboard you can:
- Teach the bot your content. Upload your FAQ, pricing page or docs, and it answers from that instead of guessing.
- Allow only your domain. Requests from other sites are rejected.
- Set a daily message limit. If a bot spams your site, it stops at your limit and your credits are safe.
- Collect leads and tickets. When someone shares their email or needs help, you see it in the dashboard (and get an email).
- Change colors, text and position without touching code.
A small thing I care about as a developer: the widget loads a tiny file first (about 4 KB), and the full chat only loads when the visitor clicks the bubble. It uses Shadow DOM, so it does not mess with your CSS and your CSS does not mess with it.
Option 3: You want your own chat UI? Use the API
Sometimes the widget is not enough. Maybe you use React and want your own design, or you only need one AI feature, like checking if a contact form message is spam.
For that you can use the RSFlowHub APIs from your own backend (or a serverless function). The key stays in an environment variable:
// Example: sentiment analysis from a serverless function const res = await fetch("https://apis.rsflowhub.com/api/v1/ai/sentiment-analysis", { method: "POST", headers: { Authorization: `Bearer ${process.env.RSFLOWHUB_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ text: "I waited 2 hours and nobody replied." }), }); console.log(await res.json()); There are also endpoints for grammar fix, spam detection, email reply, review reply, PII redaction, text summary, image generation and more. They are small and focused, so you don't need to write long prompts yourself. There are SDKs too (PHP, Node, Python) if you don't want to write raw fetch.
You can also create API keys with limited scopes. For example, a key that can only call AI endpoints and nothing else. That way even a leaked key can't do much.
Quick comparison
| Key in browser | Own proxy | RSFlowHub widget | RSFlowHub API | |
|---|---|---|---|---|
| Time to set up | 10 min | days or weeks | about 5 min | about 30 min |
| Key is safe | No | Yes | Yes | Yes (server side) |
| Knows your business | No | You build it | Yes | Yes (knowledge bases) |
| Spam / cost limits | No | You build it | Yes | Yes |
| Custom UI | Yes | Yes | Some | Fully yours |
My checklist before putting any AI chat on a website
- [ ] No secret key in the browser. Ever.
- [ ] Only your own domain can use the chat.
- [ ] A daily limit, so a bug or a bot can't drain your budget.
- [ ] The bot answers from your real content, not from its imagination.
- [ ] A simple privacy note if you collect emails.
- [ ] Test it on a phone. Most visitors are on a phone.
Wrap up
If you are a frontend developer, you don't need to become a backend engineer just to add one chatbot. Hide the key, add limits, give the bot real content. You can do that with your own proxy, or save the time and use a widget.
If you want to try it, you can start free at rsflowhub.com. The API docs are there too. I'd love to hear how you handle this on your own projects. Do you build the proxy yourself, or do you use a ready tool? Tell me in the comments.