cd /news/ai-agents/add-phone-verification-to-an-express… · home topics ai-agents article
[ARTICLE · art-128282] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Add phone verification to an Express app with Claude Code, with a real OTP behind it

A developer at MyOTP.App demonstrated wiring the company's OTP API into an Express app via a Claude Code MCP server, letting the model send a real SMS code and inspect the response before generating the verification routes. The resulting code keys verification on the API's message_id rather than the phone number and keeps the API key server-side, with the channel swappable to WhatsApp or Telegram. The author notes the pattern targets plain Express or Fastify apps, since Supabase and Better Auth already offer SMS hooks.

by read3 min views1 publishedSep 13, 2026

Most apps that need phone verification rebuild the same five things: a form, a send, an expiry, a retry rule and a verify step. It's not interesting code but it's load bearing. Here's what happens when Claude Code has a real OTP API attached while it writes that code. I work at MyOTP.App, the API in question.

One line, once:

claude mcp add myotp -e MYOTP_API_KEY=your-key -- npx -y @myotp/mcp

The key comes from myotp.app/sign-up, 15 trial credits, no card. Allowlist your IP on the key, or * while developing.

Add phone verification to my Express app. The user enters a phone number, gets a 6 digit code by SMS, types it in, and on success is marked verified. Use the MyOTP REST API from the server, never from the browser.

// routes/verify.js
const express = require("express");
const router = express.Router();
const pending = new Map(); // phone -> message_id

const API = "https://api.myotp.app";
const headers = {
  "Content-Type": "application/json",
  "X-API-Key": process.env.MYOTP_API_KEY,
};

// MyOTP wants digits only, country code first, no plus, no leading zero.
const clean = (p) => (p || "").replace(/\D/g, "").replace(/^0+/, "");

router.post("/send-code", async (req, res) => {
  const phone = clean(req.body.phone);
  if (!/^[1-9][0-9]{6,14}$/.test(phone)) {
    return res.status(400).json({ error: "invalid phone" });
  }
  const r = await fetch(`${API}/generate_otp`, {
    method: "POST", headers,
    body: JSON.stringify({ phone_number: phone, channel: "sms", otp_length: 6 }),
  });
  const data = await r.json();
  if (!r.ok) return res.status(r.status).json({ error: data.error?.message });
  pending.set(phone, data.message_id);
  res.json({ status: "sent", expires_at: data.expires_at });
});

router.post("/verify-code", async (req, res) => {
  const phone = clean(req.body.phone);
  const message_id = pending.get(phone);
  if (!message_id) return res.status(400).json({ error: "no code pending" });
  const r = await fetch(`${API}/verify_otp`, {
    method: "POST", headers,
    body: JSON.stringify({ phone_number: phone, message_id, otp: req.body.code }),
  });
  const data = await r.json();
  if (data.status !== "success") {
    return res.status(400).json({ error: data.reason }); // "invalid" | "expired" | "not found"
  }
  pending.delete(phone);
  res.json({ status: "verified" });
});

module.exports = router;

Plus the form and two lines in app.js. About a minute from prompt to a code arriving on my phone.

Three things in that code are the things people get wrong by hand.

The phone regex matches what the API enforces. phone_number must be digits only, 7 to 15 of them, no leading zero. The tool description carries that rule, so the model didn't guess.

Verification is keyed on message_id, not on the phone. Two codes in flight to the same number can't be confused.

The key never reaches the browser. The client posts a phone and later a code, nothing else.

The interesting part is that it tested the flow before writing it. With the MCP server attached, "send a code to my number" is a tool call. It sent one, read the response shape, then wrote code against what it had seen instead of what it remembered.

Change channel to whatsapp or telegram and nothing else moves. Worth doing where SMS is expensive or slow, which is most of South Asia, Africa and Latin America.

If your framework already owns phone auth, use its hook instead. Supabase has the Send SMS Hook, Better Auth has a sendOTP callback, and both take a MyOTP adapter without you writing the routes above. This pattern is for the plain Express or Fastify app that has nothing yet.

Disclosure again: I work at MyOTP.App. Prices and limits are on the site. If the code above breaks for you, tell me in the comments and I'll fix the post.

── more in #ai-agents 4 stories · sorted by recency
── more on @myotp.app 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/add-phone-verificati…] indexed:0 read:3min 2026-09-13 ·