How to Send OTP with Vercel Functions (2026)
Vercel Functions OTP tutorial using StartMessaging. Edge vs Node runtime trade-offs, environment variables, signed cookies, and KV-style rate limiting.
Vercel Functions are the natural deployment target for Next.js teams. This tutorial wires StartMessaging as a stand-alone function (works the same inside a Next.js app).
Setup
pnpm create vercel-app otp-vercel
cd otp-vercel
Edge vs Node Runtime
- Edge — global, fast cold start, fetch + crypto only.
- Node — full Node API, slower cold start.
The Function
// api/auth/send-otp.ts
export const config = { runtime: 'edge' };
export default async function handler(req: Request) {
if (req.method !== 'POST') return new Response('method not allowed', { status: 405 });
const { phoneNumber } = await req.json();
// Generate the 6-digit code here (Web Crypto works on the Edge runtime)
const otp = String(100000 + (crypto.getRandomValues(new Uint32Array(1))[0] % 900000));
const r = await fetch('https://api.startmessaging.com/otp/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.SM_API_KEY! },
body: JSON.stringify({ phoneNumber, variables: { otp }, idempotencyKey: crypto.randomUUID() }),
});
const json = await r.json();
if (!r.ok) return Response.json(json, { status: r.status });
// Only hand the client the ID it needs for /otp/verify, never the code
return Response.json({ requestId: json.data.otpRequestId });
}
// api/auth/verify-otp.ts
export const config = { runtime: 'edge' };
export default async function handler(req: Request) {
if (req.method !== 'POST') return new Response('method not allowed', { status: 405 });
const { requestId, otpCode } = await req.json(); // requestId = otpRequestId from send
const r = await fetch('https://api.startmessaging.com/otp/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.SM_API_KEY! },
body: JSON.stringify({ requestId, otpCode }),
});
return new Response(await r.text(), { status: r.status });
}
Environment Variables
vercel env add SM_API_KEY production
Rate Limiting
Use Upstash Redis (Vercel marketplace add-on) or Vercel KV for per-phone limits.
FAQ
For App Router-native flow see our Next.js guide.
StartMessaging Team
StartMessaging Team