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.
StartMessaging Team
Engineering
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-vercelEdge 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();
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, idempotencyKey: crypto.randomUUID() }),
});
return new Response(await r.text(), { status: r.status });
}// 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 body = await req.json();
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(body),
});
return new Response(await r.text(), { status: r.status });
}Environment Variables
vercel env add SM_API_KEY productionRate 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.
Related Articles
AWS Lambda OTP tutorial using StartMessaging. Function URLs, Secrets Manager for API keys, DynamoDB for rate limits, and SAM/CDK deployment patterns.
Cloudflare Workers OTP tutorial using StartMessaging. Uses Workers fetch, KV for rate-limit, signed-cookie session, and Durable Objects for production-grade pumping defence.
Send and verify SMS OTPs from a Next.js 14/15 App Router app using server actions and the StartMessaging API. Includes a full login form, server actions, and middleware.
Ready to Send OTPs?
Integrate StartMessaging in 5 minutes. No DLT registration required.