Add an AI chatbot to your website, safely
A chatbot can answer visitor questions around the clock. Built carelessly, it leaks your API key or becomes a free chatbot for strangers on your bill. The safe pattern below keeps the key on a server and the bot on topic.
Write what the bot should know
One page of facts: services, hours, prices if you have them, contact details, and what the bot must not do (give legal advice, promise refunds, and so on).
Get an API key and keep it secret
Create a key with an AI provider such as Google Gemini, Anthropic or OpenAI. Store it only as a secret environment variable on your host, never in the website's code.
Add a small server function
On Vercel, Netlify or Cloudflare, add one serverless function that receives the visitor's message, adds your fixed instructions and facts, calls the AI with the secret key, and returns the reply.
Lock it down
Ignore any instructions sent from the browser, cap message length, limit how many messages each visitor can send per minute, and only accept requests from your own site.
Add the chat box
A small script on your pages shows a chat bubble and sends messages to your function. Label it clearly as an AI assistant.
Test like a stranger
Try to make it go off topic, reveal its instructions, or spam it. Fix whatever gets through before you announce it.
Checklist
- Facts and rules page written
- API key stored only as a server secret
- Server function working
- Fixed instructions, length cap and rate limit on
- Requests only accepted from your site
- Chat box labeled as AI
- Tested against misuse
Rather have it done for you?
Max does this work professionally. Send a note with what you need and you will get a reply by email.
