Chatbots Tutorial menu

Deploying a Website Chatbot

To put your chatbot online you need a chat widget on the page, your Node.js server running on a host, and your secret key set on that host.

The Project Structure

Example

text
my-chatbot/
  server.js          Express server with /api/chat
  ai.js              the askAI function
  package.json
  .env               secrets for local testing (never commit)
  .gitignore         contains: node_modules and .env
  public/
    index.html       the page with the chat widget
    chat.js          the widget's browser code

The Chat Widget

The page needs a message area, a text box and a button. In public/index.html:

Example

html
<div id="chat">
  <div id="log"></div>
  <form id="form">
    <input id="text" placeholder="Type your question..." autocomplete="off">
    <button>Send</button>
  </form>
</div>
<script src="chat.js"></script>

And public/chat.js, which talks only to your server, never to the AI company:

Example

javascript
const history = [];
const log = document.getElementById("log");

function show(role, text) {
  const p = document.createElement("p");
  p.className = role;
  p.textContent = text;            // plain text, safe from HTML injection
  log.appendChild(p);
  log.scrollTop = log.scrollHeight;
  return p;
}

document.getElementById("form").addEventListener("submit", async (e) => {
  e.preventDefault();
  const input = document.getElementById("text");
  const text = input.value.trim();
  if (!text) return;
  input.value = "";
  show("user", text);
  history.push({ role: "user", content: text });

  const bubble = show("assistant", "Typing...");
  try {
    const res = await fetch("/api/chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ messages: history }),
    });
    const data = await res.json();
    bubble.textContent = data.reply;
    history.push({ role: "assistant", content: data.reply });
  } catch {
    bubble.textContent = "Connection problem. Please try again.";
  }
});

There is no API key anywhere in these files. That is correct.

Choosing a Host

Your server needs a host that runs Node.js:

OptionGood for
Platform services (Render, Railway, Fly.io and similar)Easy deploy from GitHub, free or low-cost tiers
Serverless functions (Vercel, Netlify)If your site is already there; write /api/chat as a function
A VPS (a small Linux server)Full control; you install Node.js and a process manager yourself

Setting the Environment Variables

On every host, find the Environment Variables (or "Secrets") section in the dashboard and add:

Example

text
AI_API_KEY   = your real key
AI_BASE_URL  = the provider's base URL
AI_MODEL     = the model name

Most hosts also set PORT for you. Use it in your server:

Example

javascript
const PORT = process.env.PORT || 3000;
console.log(`Server will listen on port ${PORT}`);
Output
Server will listen on port 3000

Before You Go Live: Checklist

  • The site uses HTTPS (most hosts give it free).
  • .env is in .gitignore, and you searched your repository for the key to be sure it is not there.
  • A spending limit is set in your AI provider's dashboard.
  • Rate limiting and input length limits are on.
  • The fallback and error messages are friendly.
  • You tested on a phone, since most visitors in Pakistan browse on mobile.
  • The page says it is an AI assistant and may make mistakes.

After Launch

  • Read real conversations (with personal data hidden) to find questions the bot answers badly.
  • Update the system prompt or knowledge chunks.
  • Watch token usage and costs every week.

If your key ever leaks (for example, pushed to GitHub by mistake), revoke it in the provider dashboard immediately and create a new one. Deleting the commit is not enough.

Exercise

Exercise

On the live website, where should the browser send chat messages?

Learn Chatbots with a teacher

Join the AI & Prompt Engineering course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp