Developer Quick-Start: Build production-ready SMS two-factor authentication using Sinch Verification API and Fastify. Step-by-step TypeScript tutorial with Prisma, JWT, and secure OTP implementation for Node.js applications. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
Build secure SMS-based two-factor authentication (2FA) using Sinch Verification API within a Fastify Node.js application. This guide covers project setup, Sinch SMS OTP integration, secure JWT authentication, error handling, and deployment best practices for production environments.
Important Security Note: SMS-based OTP has known vulnerabilities (SIM swapping, SMS interception, phishing). Per OWASP MFA guidelines, do not use SMS 2FA as the sole protection for applications containing Personally Identifiable Information (PII) or financial data. Consider TOTP authenticator apps or hardware tokens (e.g., FIDO2/WebAuthn) for high-security applications. This guide demonstrates SMS 2FA implementation for educational purposes and moderate-security use cases.
Target Audience: Developers familiar with Node.js and potentially Fastify, looking to add robust SMS OTP verification to their applications. Goal: Build a production-ready Fastify API that handles user registration, login, and secures sessions using Sinch SMS OTP verification. Outcome: A secure, documented, and testable API backend with SMS 2FA.
What You Will Build:
- A Fastify API backend using TypeScript.
- User registration and basic password-based login.
- Integration with Sinch Verification API for sending and verifying SMS OTP codes.
- Secure session management (e.g., using JWT) enhanced with 2FA status.
- API endpoints for initiating and verifying OTPs.
Why This Approach?
- Fastify: A high-performance, low-overhead Node.js web framework focused on developer experience and speed.
- Sinch Verification API: A dedicated service for handling the complexities of OTP generation, delivery (SMS, voice, flashcall, data), and verification β simplifying implementation and improving reliability. Using a dedicated service offloads concerns like number formatting, carrier deliverability, and OTP lifecycle management.
- TypeScript: Provides static typing for better code maintainability, scalability, and reduced runtime errors.
- Database (Prisma): Simplifies database interactions with type safety and migrations (using PostgreSQL in this example).
System Architecture:
+-------------+ +-----------------+ +-----------------+ +-----------------+
| End User | <---> | Frontend App | <--> | Fastify Backend | <--> | Sinch Platform |
| (Browser/ | | (React/Vue/etc.)| | (Node.js/TS) | | (Verification |
| Mobile App) | +-----------------+ +-------+---------+ | API via SDK) |
+-------------+ | +-----------------+
|
+-------+---------+
| Database |
| (PostgreSQL/ |
| Prisma) |
+-----------------+
- User Action: User initiates login or a sensitive action requiring 2FA.
- Frontend Request: Frontend sends credentials (and potentially phone number) to the Fastify backend.
- Backend Logic (Login): Backend verifies username/password against the database.
- Backend Logic (OTP Initiate): If credentials are valid (or upon registration confirmation), backend calls the Sinch SDK to initiate SMS verification for the user's phone number.
- Sinch Sends SMS: Sinch generates an OTP and sends it via SMS to the user's phone.
- User Enters OTP: User receives the SMS and enters the OTP into the frontend.
- Frontend Request (Verify): Frontend sends the entered OTP to the Fastify backend.
- Backend Logic (OTP Verify): Backend calls the Sinch SDK to report (verify) the entered OTP against the phone number.
- Sinch Verifies: Sinch checks if the OTP is correct for that number and initiation request.
- Backend Response: Backend receives success/failure from Sinch. If successful, it updates the user's session/state to indicate 2FA completion and grants access. If failed, it returns an error.
- Frontend Update: Frontend reflects the success or failure to the user.
Prerequisites:
- Node.js (v20 or later required β v18 reached EOL April 30, 2025; current LTS versions are v20 "Iron" and v22 "Jod") and npm/yarn installed.
- A Sinch Account: Sign up at Sinch.com and create a Verification App in your Sinch Dashboard.
- A mobile phone number capable of receiving SMS messages for testing.
- Basic understanding of TypeScript, REST APIs, and async/await.
- Docker and Docker Compose (optional, for running a local PostgreSQL database).
- A code editor (e.g., VS Code).
- curl or a tool like Postman/Insomnia for API testing.
1. Fastify Project Setup with TypeScript and Sinch SDK
Initialize your Fastify project using TypeScript and set up the basic structure.
1.1 Initialize Project & Install Dependencies
Run these commands:
mkdir fastify-sinch-otp
cd fastify-sinch-otp
npm init -y
npm install fastify @fastify/sensible
npm install --save-dev typescript @types/node ts-node nodemon @tsconfig/node20
npm install @prisma/client
npm install --save-dev prisma pg
npm install @sinch/verification
npm install dotenv fastify-type-provider-zod zod
npm install @fastify/jwt bcrypt
npm install --save-dev @types/bcrypt @types/jsonwebtoken
1.2 Configure TypeScript (tsconfig.json)
Create a tsconfig.json file in the project root:
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"module": "CommonJS",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
Why these settings?
- extends: Inherits sensible defaults for Node.js 20 (current LTS).
- outDir, rootDir: Define project structure for source and compiled files.
- module: CommonJS is standard for Node.js unless you're specifically setting up ES Modules.
- strict: Catches more potential errors during development.
- esModuleInterop, forceConsistentCasingInFileNames, skipLibCheck: Common settings for compatibility and faster builds.
- sourceMap: Crucial for debugging compiled code.
1.3 Configure Development Scripts (package.json)
Add these scripts to your package.json:
"scripts": {
"build": "tsc",
"start": "node dist/server.js",
"dev": "nodemon --watch src --ext ts --exec ts-node src/server.ts",
"test": "vitest run",
"prisma:dev:deploy": "prisma migrate deploy",
"prisma:dev:migrate": "prisma migrate dev",
"prisma:generate": "prisma generate",
"prisma:studio": "prisma studio"
},
- build: Compile TypeScript to JavaScript
- start: Run the compiled JavaScript application (for production)
- dev: Run the application using ts-node and nodemon for automatic restarts during development
- test: Run automated tests (example uses Vitest; replace with your test runner like Jest)
- prisma:\*: Manage database migrations and generate the Prisma client
1.4 Project Structure
Create this directory structure:
- fastify-sinch-otp/
- prisma/
- schema.prisma # Prisma schema definition
- migrations/ # Database migration files (generated)
- src/
- modules/ # Feature modules (e.g., auth, users)
- auth/
- auth.controller.ts
- auth.routes.ts
- auth.schema.ts # Zod schemas & type definitions
- auth.service.ts
- plugins/ # Fastify plugins (e.g., db client, auth)
- prisma.ts
- sinch.ts
- jwtAuth.ts
- config/ # Configuration files/logic
- index.ts
- types/ # Global or shared type definitions
- fastify-jwt.d.ts # JWT type augmentations
- utils/ # Utility functions
- hash.ts
- app.ts # Main Fastify application setup
- server.ts # Entry point, starts the server
- .env # Environment variables (ignored by git)
- .env.example # Example environment variables
- .gitignore
- package.json
- package-lock.json
- tsconfig.json
Why this structure?
- Modularity: Grouping features (like auth) into modules makes the codebase easier to navigate and maintain.
- Separation of Concerns: Routes handle HTTP requests/responses, controllers orchestrate, services contain business logic, schemas define data shapes, and plugins encapsulate reusable setup (like DB connection).
- Configuration: Centralizing configuration makes it easier to manage settings across different environments.
- Types: Dedicated types directory for global type definitions and augmentations (like for Fastify plugins).
1.5 Environment Variables (.env and .env.example)
Create .env.example with these placeholders:
ini
NODE_ENV=development
PORT=3000
HOST=0.0.0.0
DATABASE_URL="postgresql://user:password@localhost:5432/mydatabase?schema=public"
SINCH_APPLICATION_KEY=YOUR_SINCH_APP_KEY
SINCH_APPLICATION_SECRET=YOUR_SINCH_APP_SECRET
JWT_SECRET=a_very_strong_and_long_secret_key_please_change_me
JWT_EXPIRES_IN=30m
JWT_OTP_EXPIRES_IN=5m
Create a .env file (copy from .env.example) and fill in your actual development
values. Add .env to your .gitignore file to prevent committing
secrets.
# .gitignore
node_modules
dist
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Why .env? Keeps sensitive information (API keys, database URLs, secrets) out of
your codebase and makes configuration environment-specific.
Security Best Practice: For production, use a secrets management service (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault) instead of .env files. Never commit secrets to version control.
1.6 Docker Setup for PostgreSQL (Optional)
If you don't have PostgreSQL running locally, create this docker-compose.yml file:
version: '3.8'
services:
postgres:
image: postgres:15
container_name: fastify_sinch_db
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Run docker-compose up -d to start the database container in the background.
2. Database Schema and User Model (Prisma)
Use Prisma to define your database schema and interact with the database.
2.1 Define Prisma Schema
Edit prisma/schema.prisma:
Prisma
// prisma/schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // Loads from .env file
}
model User {
id String @id @default(cuid()) // Unique identifier
email String @unique // User's email, must be unique
password String // Hashed password
name String? // Optional user name
phone String? @unique // User's phone number, unique if present
// Make required if needed for your flow
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
// You could add more models here (e.g., Session, AuditLog) if needed
Why this schema?
- id: Standard unique primary key.
- email, password: Essential for basic authentication.
- phone: Required for Sinch SMS OTP. Making it @unique prevents multiple accounts using the same phone number for verification. Decide if it should be nullable or required based on your signup flow.
- createdAt, updatedAt: Standard practice for tracking record changes.
2.2 Generate Prisma Client and Run Initial Migration
- Generate Client: Create the type-safe Prisma client based on your schema.
npx prisma generate
- Create Migration: Create the initial SQL migration file based on the schema changes. Use a descriptive name.
npx prisma migrate dev --name init
This command:
- Creates the prisma/migrations directory if it doesn't exist.
- Generates a new SQL migration file inside a timestamped folder (e.g., prisma/migrations/20250420000000\_init/migration.sql).
- Applies the migration to your database (creating the User table).
- Ensures the Prisma client is up-to-date.
2.3 Create Prisma Plugin for Fastify
Create src/plugins/prisma.ts to make the Prisma client available throughout your Fastify
application.
import fp from 'fastify-plugin';
import { PrismaClient } from '@prisma/client';
declare module 'fastify' {
interface FastifyInstance {
prisma: PrismaClient;
}
}
export default fp(async (fastify) => {
const prisma = new PrismaClient();
await prisma.$connect();
fastify.decorate('prisma', prisma);
fastify.addHook('onClose', async (instance) => {
await instance.prisma.$disconnect();
});
});
Why this plugin?
- Encapsulates Prisma client initialization.
- Uses fastify-plugin to avoid encapsulation issues and make fastify.prisma available globally within the Fastify instance.
- Handles connecting and disconnecting gracefully.
3. Implementing Core Functionality (Auth Service & Controller)
Build the core logic for user registration, login, and OTP preparation.
3.1 Configuration Setup
Create src/config/index.ts to load environment variables safely.
import dotenv from 'dotenv';
import { z } from 'zod';
dotenv.config();
const envSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
PORT: z.coerce.number().default(3000),
HOST: z.string().default('0.0.0.0'),
DATABASE_URL: z.string().url(),
SINCH_APPLICATION_KEY: z.string().min(1, "Sinch Application Key is required"),
SINCH_APPLICATION_SECRET: z.string().min(1, "Sinch Application Secret is required"),
JWT_SECRET: z.string().min(64, "JWT Secret must be at least 64 characters long per OWASP recommendations"),
JWT_EXPIRES_IN: z.string().default('30m'),
JWT_OTP_EXPIRES_IN: z.string().default('5m'),
});
try {
const config = envSchema.parse(process.env);
console.log("β Environment variables loaded successfully.");
const appConfig = {
env: config.NODE_ENV,
port: config.PORT,
host: config.HOST,
databaseUrl: config.DATABASE_URL,
sinch: {
appKey: config.SINCH_APPLICATION_KEY,
appSecret: config.SINCH_APPLICATION_SECRET,
},
jwt: {
secret: config.JWT_SECRET,
expiresIn: config.JWT_EXPIRES_IN,
otpExpiresIn: config.JWT_OTP_EXPIRES_IN,
}
};
console.log("β App Config:", {
env: appConfig.env,
port: appConfig.port,
host: appConfig.host,
jwt: { expiresIn: appConfig.jwt.expiresIn, otpExpiresIn: appConfig.jwt.otpExpiresIn }
});
if (appConfig.env !== 'development') {
console.log("β Sinch App Key: [Loaded]");
} else {
console.log(`β Sinch App Key: ${appConfig.sinch.appKey.substring(0, 5)}...`);
}
export default appConfig;
} catch (error) {
if (error instanceof z.ZodError) {
console.error("β Invalid environment variables:", error.format());
} else {
console.error("β Error loading environment variables:", error);
}
process.exit(1);
}
Why Zod for config? Validates required variables and enforces correct types at application startup, preventing runtime errors.
3.2 Zod Schemas and Type Definitions
Create src/modules/auth/auth.schema.ts to define input shapes using Zod and centralize JWT
payload types.
import { z } from 'zod';
export const registerUserSchema = z.object({
email: z.string().email({ message: "Invalid email address" }),
password: z.string().min(8, { message: "Password must be at least 8 characters long" }),
name: z.string().optional(),
phone: z.string().min(10, { message: "Phone number seems too short" })
.max(15, { message: "Phone number seems too long"})
.regex(/^\+?[1-9]\d{1,14}$/, { message: "Invalid phone number format (E.164 expected: +[country code][number], max 15 digits total, e.g., +14155552671)"}),
});
export type RegisterUserInput = z.infer<typeof registerUserSchema>;
export const loginUserSchema = z.object({
email: z.string().email(),
password: z.string(),
});
export type LoginUserInput = z.infer<typeof loginUserSchema>;
export const initiateOtpSchema = z.object({
});
export type InitiateOtpInput = z.infer<typeof initiateOtpSchema>;
export const verifyOtpSchema = z.object({
otp: z.string().length(4, { message: "OTP must be 4 digits" })
.regex(/^\d+$/, { message: "OTP must contain only digits" }),
});
export type VerifyOtpInput = z.infer<typeof verifyOtpSchema>;
export const authResponseSchema = z.object({
accessToken: z.string()
});
export const userResponseSchema = z.object({
id: z.string(),
email: z.string(),
name: z.string().nullable(),
phone: z.string().nullable(),
createdAt: z.date(),
});
export const loginResponseSchema = z.object({
otpToken: z.string().optional(),
accessToken: z.string().optional(),
message: z.string().optional()
});
export const messageResponseSchema = z.object({
message: z.string()
});
export interface AuthJWTPayload {
id: string;
email: string;
otpVerified: boolean;
}
export interface OtpJWTPayload {
id: string;
phone: string;
}
Why Zod schemas? Provides runtime validation of request bodies and parameters, ensuring data integrity before it reaches your service logic. Enables type inference for request handlers and improves consistency by centralizing JWT types.
3.3 Password Hashing Utility
Create src/utils/hash.ts:
import bcrypt from 'bcrypt';
const SALT_ROUNDS = 12;
export async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, SALT_ROUNDS);
}
export async function comparePassword(plain: string, hash: string): Promise<boolean> {
return bcrypt.compare(plain, hash);
}
Why bcrypt? The standard and secure way to hash passwords per OWASP Password Storage Cheat Sheet. Never store plain text passwords. bcrypt handles salting automatically and resists brute-force attacks.
3.4 Auth Service Logic
Create src/modules/auth/auth.service.ts. This handles the core business logic, interacting with
the database and hashing.
import { PrismaClient, User } from '@prisma/client';
import { hashPassword, comparePassword } from '../../utils/hash';
import { RegisterUserInput, LoginUserInput } from './auth.schema';
import { BadRequest, NotFound, Unauthorized } from 'http-errors';
export class AuthService {
constructor(private prisma: PrismaClient) {}
async registerUser(input: RegisterUserInput): Promise<Omit<User, 'password'>> {
const { email, password, name, phone } = input;
const existingUser = await this.prisma.user.findFirst({
where: { OR: [{ email }, { phone }] },
});
if (existingUser) {
if (existingUser.email === email) {
throw new BadRequest('User with this email already exists.');
}
if (existingUser.phone === phone) {
throw new BadRequest('User with this phone number already exists.');
}
}
const hashedPassword = await hashPassword(password);
const user = await this.prisma.user.create({
data: {
email,
password: hashedPassword,
name,
phone,
},
});
const { password: _, ...userWithoutPassword } = user;
return userWithoutPassword;
}
async loginUser(input: LoginUserInput): Promise<User> {
const { email, password } = input;
const user = await this.prisma.user.findUnique({
where: { email },
});
if (!user) {
throw new NotFound('User not found.');
}
const isPasswordValid = await comparePassword(password, user.password);
if (!isPasswordValid) {
throw new Unauthorized('Invalid password.');
}
return user;
}
async findUserById(userId: string): Promise<Omit<User, 'password'> | null> {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: {
id: true,
email: true,
name: true,
phone: true,
createdAt: true,
updatedAt: true,
}
});
if (!user) {
throw new NotFound('User not found.');
}
return user;
}
}
Why this structure?
- Dependency Injection: Injecting PrismaClient makes the service testable (you can pass a mock client).
- Clear Methods: Each method has a single responsibility (register, login, find).
- Error Handling: Uses http-errors for standard, descriptive HTTP errors. Throws errors instead of returning complex objects.
- Security: Explicitly removes the password hash before returning user data. Handles existing user checks.
3.5 Auth Controller (Request/Response Handling)
Create src/modules/auth/auth.controller.ts. This connects HTTP requests to the service logic.
import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify';
import { AuthService } from './auth.service';
import {
RegisterUserInput,
LoginUserInput,
VerifyOtpInput,
AuthJWTPayload,
OtpJWTPayload
} from './auth.schema';
import config from '../../config';
import { BadRequest, Unauthorized } from 'http-errors';
export class AuthController {
constructor(
private authService: AuthService,
private fastify: FastifyInstance
) {}
async registerHandler(request: FastifyRequest<{ Body: RegisterUserInput }>, reply: FastifyReply) {
try {
const user = await this.authService.registerUser(request.body);
return reply.code(201).send(user);
} catch (error: any) {
request.log.error(error, "Registration failed");
throw error;
}
}
async loginHandler(request: FastifyRequest<{ Body: LoginUserInput }>, reply: FastifyReply) {
try {
const user = await this.authService.loginUser(request.body);
if (!user.phone) {
request.log.warn(`User ${user.email} logged in but has no phone number for OTP.`);
const tokenPayload: AuthJWTPayload = { id: user.id, email: user.email, otpVerified: false };
const accessToken = this.fastify.jwt.sign(tokenPayload, { expiresIn: config.jwt.expiresIn });
return reply.send({ accessToken });
}
const otpTokenPayload: OtpJWTPayload = { id: user.id, phone: user.phone };
const otpToken = this.fastify.jwt.sign(otpTokenPayload, { expiresIn: config.jwt.otpExpiresIn });
return reply.send({ otpToken, message: "Credentials verified. Proceed to OTP verification." });
} catch (error: any) {
request.log.error(error, "Login failed");
throw error;
}
}
async getMeHandler(request: FastifyRequest, reply: FastifyReply) {
const userId = (request.user as AuthJWTPayload).id;
try {
const user = await this.authService.findUserById(userId);
return reply.send(user);
} catch (error) {
request.log.error(error, `Failed to get user details for ID: ${userId}`);
throw error;
}
}
async initiateOtpHandler(request: FastifyRequest, reply: FastifyReply) {
const otpPayload = request.user as OtpJWTPayload;
try {
const sinchResponse = await (this.fastify as any).initiateSmsOtp(otpPayload.phone);
request.log.info(`OTP initiated successfully via Sinch for user ${otpPayload.id}, phone ${otpPayload.phone}`);
return reply.code(200).send({ message: `OTP sent successfully to ${otpPayload.phone.substring(0, otpPayload.phone.length - 4)}****.` });
} catch (error: any) {
request.log.error(error, `Failed to initiate OTP for user ${otpPayload.id}`);
if (error.response?.data?.errorCode) {
const sinchErrorCode = error.response.data.errorCode;
if (sinchErrorCode === 40003) {
throw new BadRequest("Invalid phone number format provided to Sinch.");
}
}
throw new BadRequest("Failed to send OTP. Please try again later.");
}
}
async verifyOtpHandler(request: FastifyRequest<{ Body: VerifyOtpInput }>, reply: FastifyReply) {
const otpPayload = request.user as OtpJWTPayload;
const { otp } = request.body;
try {
const sinchResponse = await (this.fastify as any).verifySmsOtp(otpPayload.phone, otp);
if (sinchResponse.status === 'SUCCESSFUL') {
request.log.info(`OTP verification successful for user ${otpPayload.id}`);
const user = await this.authService.findUserById(otpPayload.id);
if (!user) throw new Unauthorized("User associated with token not found during OTP verification.");
const finalTokenPayload: AuthJWTPayload = {
id: user.id,
email: user.email,
otpVerified: true,
};
const accessToken = this.fastify.jwt.sign(finalTokenPayload, { expiresIn: config.jwt.expiresIn });
return reply.send({ accessToken });
} else {
request.log.warn(`OTP verification failed for user ${otpPayload.id}. Sinch status: ${sinchResponse.status}, Reason: ${sinchResponse.reason || 'N/A'}`);
throw new Unauthorized("Invalid or expired OTP.");
}
} catch (error: any) {
request.log.error(error, `Failed to verify OTP for user ${otpPayload.id}`);
if (error instanceof Unauthorized || error instanceof BadRequest) {
throw error;
}
throw new BadRequest("Failed to verify OTP. Please try again later.");
}
}
}
Frequently Asked Questions About Sinch SMS OTP with Fastify
How does Sinch Verification API work with Fastify?
Sinch Verification API integrates with Fastify through the official @sinch/verification SDK. Create a Fastify plugin that initializes
the Sinch client with your application credentials (Application Key and Secret from your Sinch
Dashboard), then expose methods for initiating SMS OTP (sending verification codes) and verifying
user-entered codes. The SDK handles communication with Sinch's platform via Application Signed Request authentication, including SMS delivery, OTP
generation, and verification status tracking.
What are the Sinch SMS pricing and rate limits?
Sinch SMS OTP pricing varies by country and volume, typically ranging from $0.005 to $0.10 per verification attempt depending on destination. Free trial accounts include limited verification credits for testing. Rate limits depend on your account tier but generally allow 1β5 verification attempts per phone number per hour to prevent abuse. Check your Sinch Dashboard for specific pricing and implement exponential backoff for rate-limited requests. Contact Sinch support for enterprise pricing and higher rate limits.
How secure is SMS-based OTP compared to other 2FA methods?
SMS OTP provides moderate security but has known vulnerabilities including SIM swapping attacks, SS7 protocol interception, and SMS phishing. Per OWASP MFA guidelines, do not use SMS 2FA as the sole protection for high-value applications containing PII or financial data. For stronger security, consider TOTP authenticator apps (Google Authenticator, Authy), hardware tokens (YubiKey), or FIDO2/WebAuthn passkeys. SMS OTP works well for moderate-security use cases like account recovery or secondary verification. NIST SP 800-63B also restricts SMS for authenticators due to these vulnerabilities.
What Node.js version is required for Fastify v5?
Fastify v5 requires Node.js v20 or later. Node.js v18 reached end-of-life on April 30, 2025 and no longer receives security updates. Current LTS versions are v20 "Iron" (Maintenance LTS until April 30, 2026) and v22 "Jod" (Active LTS until October 21, 2025, then Maintenance until April 30, 2027). Always use actively supported Node.js versions in production to ensure security patches and optimal performance. See the official Node.js release schedule for details.
How do I test SMS OTP locally without sending real messages?
For local testing, use Sinch's verification callback simulation or implement a development mode that bypasses
actual SMS sending. Create a test endpoint that returns mock OTP codes, check if
NODE_ENV=development and use hardcoded test codes (like "1234"), or use Sinch's test phone
numbers if available in your region. Always validate production flows with real SMS to ensure carrier
compatibility. Consider implementing a flag in your configuration to toggle between real Sinch API calls and
mock responses during development.
For OTP-specific tokens (issued after password verification but before OTP entry), use short expiration times of 5β10 minutes per security best practices. This limits the window for token theft while giving users enough time to receive and enter their SMS code. For standard access tokens (issued after successful OTP verification), OWASP recommends 15β30 minute idle timeouts for low-risk applications. High-value applications should use shorter timeouts (2β5 minutes). Implement refresh token rotation for longer sessions and absolute timeouts (4β8 hours) regardless of activity.
How do I handle Sinch API errors in production?
Implement comprehensive error handling for Sinch API responses including network failures, invalid phone formats (error code 40003 per Sinch API docs), rate limiting (HTTP 429), and verification failures. Log all Sinch interactions with correlation IDs for debugging, use circuit breakers to prevent cascade failures, and provide user-friendly error messages without exposing internal details. Monitor your Sinch Dashboard for service status and set up alerts for elevated error rates. Common Sinch error codes include 40001 (invalid credentials), 40003 (parameter validation failure), and 40301 (insufficient funds).
Can I use Sinch SMS OTP with other databases besides PostgreSQL?
Yes, Sinch SMS OTP integration is database-agnostic. While this guide uses PostgreSQL with Prisma, you can
use any database supported by Prisma including MySQL (5.6+), MariaDB (10.0+),
SQLite, MongoDB (4.2+), Microsoft SQL Server (2017+), and CockroachDB (21.2.4+). You can also use other ORMs
like TypeORM, Sequelize, Mongoose, or Knex.js. The core requirement is storing user phone numbers and
authentication state. Adjust your Prisma schema datasource provider and connection string
accordingly.
How do I implement retry logic for failed SMS deliveries?
Implement exponential backoff with maximum retry limits (typically 3 attempts) using libraries like
p-retry or custom retry logic. Track verification attempts in your database to prevent abuse, add
delays between retries (1s, 2s, 4s), and provide users with alternative delivery methods (voice call via Sinch
FlashCall or Phone Call verification) after multiple failures. Log all retry attempts and failure reasons for
debugging carrier-specific issues. Sinch automatically handles some retry logic, but you should implement
application-level tracking to prevent users from requesting excessive OTPs and to detect potential abuse
patterns.
What are E.164 phone format requirements for Sinch?
E.164 is the international phone number format required by Sinch as defined in ITU-T Recommendation
E.164: +[country code][subscriber number] with maximum 15 digits total and no spaces or
special characters. Examples: +14155552671 (US), +442071838750 (UK),
+61291234567 (Australia). Validate phone numbers using the regex
/^\+?[1-9]\d{1,14}$/ and use libraries like libphonenumber-js for parsing, formatting, and validating user
input before sending to Sinch. The leading + is required when sending to Sinch API, and country
code must be 1β3 digits.
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β