Build the Moltbot Voice Bot (Local) β€” Step-by-Step

WANT TO BOOST YOUR SEO TRAFFIC, RANK #1 & Get More CUSTOMERS?

Get free, instant access to our SEO video course, 120 SEO Tips, ChatGPT SEO Course, 999+ make money online ideas and get a 30 minute SEO consultation!

Just Enter Your Email Address Below To Get FREE, Instant Access!

🎯 Build the Moltbot Voice Bot (Local)

Make a local web app that lets you talk to Moltbot with voice, fast.


πŸ€” What You’ll Get

  • βœ… A local website you open in your browser
  • βœ… A cute Moltbot character on screen
  • βœ… A **Connect** button + **Start/Stop Talking**
  • βœ… Optional: say **β€œrun tool …”** to run Codex CLI and read results out loud

πŸ“‹ What You Need

  • βœ… A Mac (or Linux)
  • βœ… Node.js installed
  • βœ… An OpenAI API key (starts with `sk-`)
  • βœ… Google Chrome (recommended)

⚠️ **Important:** Keep your API key secret. Never paste it into public code.


πŸš€ Step 1: Create the project folder

1. Open **Terminal**

2. Run this:


mkdir -p ~/clawd/projects/moltbot-voice-local/public
cd ~/clawd/projects/moltbot-voice-local

πŸ”¨ Step 2: Create `package.json`

1. In the folder, create `package.json`

2. Paste this:


{
  "name": "moltbot-voice-local",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "dotenv": "^16.4.5",
    "express": "^4.19.2"
  }
}

πŸ”‘ Step 3: Add your API key in `.env`

1. Create a file called `.env`

2. Paste this (and put your key):


OPENAI_API_KEY=sk-your-key-here
OPENAI_REALTIME_MODEL=gpt-4o-realtime-preview
OPENAI_REALTIME_VOICE=alloy
PORT=4187

⚠️ **Important:** Add `.env` to `.gitignore` so it never gets committed.


πŸ”¨ Step 4: Create `server.js`

1. Create a file called `server.js`

2. Paste this:


import express from "express";
import dotenv from "dotenv";
import { execFile } from "node:child_process";
import { promisify } from "node:util";

dotenv.config();
const execFileAsync = promisify(execFile);

const app = express();
app.use(express.json({ limit: "1mb" }));

const PORT = Number(process.env.PORT || 4187);
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const MODEL = process.env.OPENAI_REALTIME_MODEL || "gpt-4o-realtime-preview";
const VOICE = process.env.OPENAI_REALTIME_VOICE || "alloy";

if (!OPENAI_API_KEY) {
  console.error("Missing OPENAI_API_KEY in .env");
  process.exit(1);
}

// Serve the UI
app.use(express.static("public"));

// Create an ephemeral key (browser never sees your real API key)
app.get("/session", async (req, res) => {
  try {
    const r = await fetch("https://api.openai.com/v1/realtime/sessions", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${OPENAI_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ model: MODEL, voice: VOICE }),
    });

    const data = await r.json();
    if (!r.ok) return res.status(r.status).json({ ok: false, data });

    res.json({
      ok: true,
      model: MODEL,
      voice: VOICE,
      client_secret: data?.client_secret?.value,
    });
  } catch (e) {
    res.status(500).json({ ok: false, error: String(e) });
  }
});

// Optional: run Codex CLI when you say "run tool ..."
app.post("/tool", async (req, res) => {
  try {
    const prompt = String(req.body?.prompt || "").trim();
    if (!prompt) return res.status(400).json({ ok: false, error: "Missing prompt" });

    const { stdout: codexPathOut } = await execFileAsync("/bin/sh", ["-lc", "command -v codex || true"]);
    const CODEX_BIN = (codexPathOut || "").trim();
    if (!CODEX_BIN) return res.status(500).json({ ok: false, error: "codex CLI not found" });

    const { stdout: tmpOut } = await execFileAsync("/bin/sh", ["-lc", "mktemp -d"]);
    const tmpDir = (tmpOut || "").trim();

    const shellCmd = `set -e; cd ${JSON.stringify(tmpDir)}; git init -q; ${JSON.stringify(CODEX_BIN)} exec ${JSON.stringify(prompt)}`;
    const { stdout, stderr } = await execFileAsync("/bin/sh", ["-lc", shellCmd], {
      timeout: 180_000,
      maxBuffer: 1024 * 1024,
    });

    res.json({ ok: true, text: (stdout || stderr || "").trim() });
  } catch (e) {
    res.status(500).json({ ok: false, error: String(e?.message || e) });
  }
});

app.listen(PORT, () => {
  console.log(`Moltbot Voice MVP running: http://localhost:${PORT}`);
});

🎨 Step 5: Create the UI files

5A) Create `public/index.html`

1. Create `public/index.html`

2. Paste your HTML (the page layout)

5B) Create `public/style.css`

1. Create `public/style.css`

2. Paste your CSS (the design)

5C) Create `public/app.js`

1. Create `public/app.js`

2. Paste your JavaScript

3. Make it:

– Connect to OpenAI Realtime

– Use browser speech recognition (English)

– Send text to the model

– Play voice back


βœ… Step 6: Install and run

1. Install:


cd ~/clawd/projects/moltbot-voice-local
npm install

2. Start:


npm start

3. Open this in your browser:

http://localhost:4187


πŸ’‘ Pro Tips

**Tip 1: Use Chrome**

Chrome works best for speech recognition.

**Tip 2: Keep the model English**

Always force:

  • `lang = "en-US"`
  • instructions: β€œRespond in English”

**Tip 3: Keep keys safe**

Never commit `.env`.


❓ Common Questions

**Q: Why do we create a /session endpoint?**

So your browser gets a short-lived key. Your real API key stays on your machine.

**Q: Why does Codex need a git repo?**

Codex CLI won’t run unless it is inside a trusted git folder.

**Q: Why does the server get killed sometimes?**

That happens when we restart while editing. Just run `npm start` again.


πŸŽ‰ You Did It!

You now have a local Moltbot voice bot.

Try this:

  • πŸ—£οΈ β€œWhat should I post today?”
  • 🧰 β€œrun tool write 10 YouTube titles about AI automation”

*If you want, I can turn this into a real desktop app next.*

Picture of Julian Goldie

Julian Goldie

Hey, I'm Julian Goldie! I'm an SEO link builder and founder of Goldie Agency. My mission is to help website owners like you grow your business with SEO!

Leave a Comment

WANT TO BOOST YOUR SEO TRAFFIC, RANK #1 & GET MORE CUSTOMERS?

Get free, instant access to our SEO video course, 120 SEO Tips, ChatGPT SEO Course, 999+ make money online ideas and get a 30 minute SEO consultation!

Just Enter Your Email Address Below To Get FREE, Instant Access!