API Guides

Implementing OTP/2FA with Fastify and AWS SNS

A step-by-step guide to building an SMS-based OTP/2FA system using Fastify, Node.js, and AWS SNS.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 6, 2026 β€’ 13 min read

Developer Quick-Start: A step-by-step guide to building an SMS-based OTP/2FA system using Fastify, Node.js, and AWS SNS. 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.

This guide provides a step-by-step walkthrough for building a production-ready One-Time Password (OTP) / Two-Factor Authentication (2FA) system using Fastify, Node.js, and AWS Simple Notification Service (SNS) for SMS delivery.

We'll cover everything from initial project setup and AWS configuration to implementing core OTP logic, securing endpoints, handling errors, and preparing for deployment.

Project Overview and Goals

Goal: To add a secure SMS-based OTP verification layer to a Fastify application. This is commonly used for phone number verification during signup, implementing 2FA for logins, or authorizing sensitive actions.

Problem Solved: Enhances application security by verifying user possession of a specific phone number, mitigating unauthorized access and fraudulent activities.

Technologies:

  • Node.js: The runtime environment.
  • Fastify: A high-performance, low-overhead web framework for Node.js. Chosen for its speed, extensibility, and developer-friendly features like built-in schema validation.
  • AWS SNS (Simple Notification Service): A managed messaging service used here to reliably send OTP codes via SMS to users' phones globally. Chosen for its scalability, cost-effectiveness, and integration ease.
  • fastify-aws-sns: A community Fastify plugin simplifying interaction with the AWS SNS API.
  • @fastify/env: For loading and validating environment variables.
  • @fastify/sensible: Provides sensible defaults, including standard HTTP errors.
  • @fastify/rate-limit: To protect against brute-force attacks on OTP endpoints.
  • ioredis & @fastify/redis (Recommended): For temporarily storing OTPs securely and efficiently with Time-To-Live (TTL). An in-memory store will be used initially for simplicity, but Redis is strongly advised for production.

System Architecture:

+---------+       (1) Request OTP        +-----------------+      (3) Send SMS via SNS       +--------------+
|  User   | -------------------------> | Fastify Server  | -----------------------------> |   AWS SNS    | --(SMS)--> User's Phone
| Browser/|       (Phone Number)       | (Node.js App)   |      (OTP Code, Phone #)       +--------------+
|  App    |                            +-----------------+
+---------+                                |        ^
    |                                      | (2)    | (5)
    |                                      | Generate & Store OTP
    | (4) Submit OTP                       |        | Verify OTP
    | (Phone Number, OTP Code)             v        |
    |                                  +---------------+
    +------------------------------->  |  OTP Store    |
                                     | (Redis / Mem) |
                                     +---------------+

Note: Step 3 implicitly includes SNS delivering the SMS to the user's phone.

Outcome: A Fastify application with two API endpoints: one to request an OTP sent to a provided phone number via SMS, and another to verify the submitted OTP against the stored value.

Prerequisites:

  • Node.js (v18 or later recommended) and npm installed.
  • An AWS account with permissions to manage IAM users and SNS.
  • Basic understanding of JavaScript, Node.js, REST APIs, and Fastify fundamentals.
  • Access to a terminal or command prompt.
  • (Optional but Recommended) Redis server accessible for OTP storage in production.

1. Setting up the Project

Let's initialize our Node.js project and install the necessary dependencies.

1.1 Initialize Project:

Open your terminal and create a new project directory:

mkdir fastify-sns-otp
cd fastify-sns-otp
npm init -y

This creates a package.json file.

1.2 Install Dependencies:

Install Fastify and essential plugins:

npm install fastify fastify-aws-sns @fastify/env @fastify/sensible @fastify/rate-limit ioredis @fastify/redis
  • fastify: The core framework.
  • fastify-aws-sns: Plugin for AWS SNS integration.
  • @fastify/env: For managing environment variables.
  • @fastify/sensible: Provides useful utilities like HTTP errors.
  • @fastify/rate-limit: To prevent abuse of OTP endpoints.
  • ioredis: A robust Redis client (recommended for production OTP storage).
  • @fastify/redis: Fastify plugin to integrate ioredis.

Install development dependencies (like pino-pretty for logging):

npm install --save-dev pino-pretty tap sinon

1.3 Project Structure:

Create the basic directory structure:

mkdir src
mkdir src/routes
mkdir src/services
mkdir src/config
mkdir test
mkdir test/routes
touch src/server.js
touch src/app.js
touch src/routes/otp.js
touch src/services/otpService.js
touch src/config/environment.js
touch test/setup.js
touch test/routes/otp.test.js
touch .env
touch .env.example
touch .gitignore
  • src/: Main application code.
  • src/routes/: API route definitions.
  • src/services/: Business logic (OTP generation, verification, SNS interaction).
  • src/config/: Configuration files (environment variables schema).
  • test/: Automated tests.
  • src/server.js: Entry point to start the Fastify server.
  • src/app.js: Fastify application setup (plugins, routes).
  • .env: Stores sensitive credentials and environment-specific settings (DO NOT commit this file).
  • .env.example: A template showing required environment variables.
  • .gitignore: Specifies files/directories Git should ignore.

1.4 Configure .gitignore:

Add the following to your .gitignore file to avoid committing sensitive data and unnecessary files:

# Dependencies
node_modules

# Environment Variables
.env
*.env.local
*.env.*.local

# Logs
*.log
logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Build output
dist
build

# OS generated files
.DS_Store
Thumbs.db

1.5 Configure Environment Variables Schema:

Define the expected environment variables in src/config/environment.js:

const environmentSchema = {
  type: 'object',
  required: [
    'PORT',
    'HOST',
    'AWS_ACCESS_KEY_ID',
    'AWS_SECRET_ACCESS_KEY',
    'AWS_DEFAULT_REGION',


  ],
  properties: {
    PORT: { type: 'string', default: '3000' },
    HOST: { type: 'string', default: '0.0.0.0' },
    AWS_ACCESS_KEY_ID: { type: 'string' },
    AWS_SECRET_ACCESS_KEY: { type: 'string' },
    AWS_DEFAULT_REGION: { type: 'string' },
    OTP_LENGTH: { type: 'integer', default: 6 },
    OTP_TTL_SECONDS: { type: 'integer', default: 300 },
    REDIS_URL: { type: 'string' },
    LOG_LEVEL: { type: 'string', default: 'info'},
    RATE_LIMIT_MAX_REQUESTS: { type: 'integer', default: 5 },
    RATE_LIMIT_WINDOW_MS: { type: 'integer', default: 60000 },
    SNS_SMS_SENDER_ID: { type: 'string', default: 'MyApp'},
    SNS_SMS_TYPE: { type: 'string', default: 'Transactional' }
  }
};

export default environmentSchema;

1.6 Set up .env.example:

Populate .env.example as a template for other developers (and yourself):

.env

# .env.example

# Server Configuration
PORT=3000
HOST=0.0.0.0
LOG_LEVEL=info

# AWS SNS Configuration
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION=us-east-1 # Change to your desired AWS region
SNS_SMS_SENDER_ID=MyApp # Optional: Custom Sender ID (check AWS docs for regional support & rules)
SNS_SMS_TYPE=Transactional # 'Transactional' for higher reliability, 'Promotional' for lower cost

# OTP Configuration
OTP_LENGTH=6
OTP_TTL_SECONDS=300 # 5 minutes

# Rate Limiting
RATE_LIMIT_MAX_REQUESTS=5
RATE_LIMIT_WINDOW_MS=60000 # 1 minute

# Redis Configuration (Recommended for Production)
# REDIS_URL=redis://localhost:6379

1.7 Create .env file:

Copy .env.example to .env and fill in your actual AWS credentials and any other desired settings. Remember to keep this file secure and out of version control.

cp .env.example .env

2. AWS SNS Configuration

We need an IAM user with specific permissions to send SMS messages via SNS.

2.1 Create an IAM User:

  1. Navigate to the IAM console in your AWS account: https://console.aws.amazon.com/iam/
  2. Go to Users and click Create user.
  3. Enter a User name (e.g., fastify-sns-otp-service).
  4. Select Provide user access to the AWS Management Console - optional if you don't need console access for this user. If you do, set up a password or let AWS generate one.
  5. Click Next.
  6. Choose Attach policies directly.
  7. Search for and select the AmazonSNSFullAccess policy for simplicity. Security Best Practice: For production, create a custom inline policy granting only the sns:Publish permission to minimize attack surface.
  • Example Custom Policy (Least Privilege):
     {
         ""Version"": ""2012-10-17"",
         ""Statement"": [
             {
                 ""Effect"": ""Allow"",
                 ""Action"": ""sns:Publish"",
                 ""Resource"": ""*""
             }
         ]
     }

Note: You could restrict the Resource further to specific Topic ARNs if using topics, but for direct SMS publishing, * is often necessary unless specific controls are in place.

  1. Click Next.
  2. Review the user details and permissions, then click Create user.

2.2 Obtain Access Keys:

  1. After the user is created, click on the username in the user list.
  2. Go to the Security credentials tab.
  3. Scroll down to Access keys and click Create access key.
  4. Select Application running outside AWS (or Command Line Interface (CLI) if applicable).
  5. Understand the recommendation (alternatives exist, but access keys are needed here). Click Next.
  6. Optionally add a description tag (e.g., fastify-sns-otp-app).
  7. Click Create access key.
  8. Crucial: Immediately copy the Access key ID and Secret access key. You cannot retrieve the secret key again after closing this window. Store them securely.
  9. Update your .env file with these keys:

.env

   # .env
   AWS_ACCESS_KEY_ID=COPIED_ACCESS_KEY_ID
   AWS_SECRET_ACCESS_KEY=COPIED_SECRET_ACCESS_KEY
   AWS_DEFAULT_REGION=your-chosen-region # e.g., us-east-1

2.3 Configure SNS Defaults (Optional but Recommended):

While fastify-aws-sns allows setting SMS type per message, you can set account-wide defaults in the SNS console for consistency.

  1. Navigate to the SNS console: https://console.aws.amazon.com/sns/
  2. In the left navigation pane, choose Text messaging (SMS).
  3. Under Account spending limit, click Edit and set a reasonable monthly limit to prevent unexpected costs (e.g., $1.00 initially). Save changes.
  4. Under Default settings for text messages, click Edit.
  5. Set the Default message type to Transactional for OTP messages (optimized for reliability).
  6. You can optionally configure a Default sender ID here, but be aware of regional support and regulations. Using the SNS\_SMS\_SENDER\_ID environment variable allows more flexibility per application.
  7. Save changes.

3. Implementing Core Functionality (OTP Service)

Now, let's write the logic for generating, storing, and verifying OTPs. We'll use an in-memory store initially and show Redis integration later (Section 6).

3.1 OTP Service (src/services/otpService.js):

import crypto from 'node:crypto';

const otpStore = new Map();

async function storeOtpMemory(fastify, phoneNumber, otp, ttlSeconds) {
  const expiry = Date.now() + ttlSeconds * 1000;
  otpStore.set(phoneNumber, { otp, expiry });


  setTimeout(() => {
    const stored = otpStore.get(phoneNumber);
    if (stored && stored.otp === otp) {
        otpStore.delete(phoneNumber);
        fastify.log.info(`Expired OTP for ${phoneNumber} removed from memory.`);
    }
  }, ttlSeconds * 1000);

  fastify.log.info(`OTP ${otp} stored in memory for ${phoneNumber}, expires in ${ttlSeconds}s.`);
}

async function verifyOtpMemory(fastify, phoneNumber, submittedOtp) {
  const storedData = otpStore.get(phoneNumber);

  if (!storedData) {
    fastify.log.warn(`Verification failed: No OTP found in memory for ${phoneNumber}.`);
    return false;
  }

  const { otp: storedOtp, expiry } = storedData;

  if (Date.now() > expiry) {
    fastify.log.warn(`Verification failed: OTP expired for ${phoneNumber}.`);
    otpStore.delete(phoneNumber);
    return false;
  }

  if (storedOtp === submittedOtp) {
    fastify.log.info(`Verification successful for ${phoneNumber}. Removing from memory.`);
    otpStore.delete(phoneNumber);
    return true;
  }

  fastify.log.warn(`Verification failed: Incorrect OTP for ${phoneNumber}.`);
  return false;
}

function generateOtp(length) {

  const otpLength = Math.max(1, Math.floor(length));

  const min = Math.pow(10, otpLength - 1);
  const max = Math.pow(10, otpLength) - 1;

  return (crypto.randomInt(min, max + 1)).toString().padStart(otpLength, '0');
}

async function storeOtpRedis(fastify, phoneNumber, otp, ttlSeconds) {
    throw new Error('storeOtpRedis not implemented yet');
}
async function verifyOtpRedis(fastify, phoneNumber, submittedOtp) {
    throw new Error('verifyOtpRedis not implemented yet');
}

export {
    generateOtp,
    storeOtpMemory,
    verifyOtpMemory,
    storeOtpRedis,
    verifyOtpRedis
};
  • generateOtp: Creates a random numeric OTP using Node.js's crypto module.
  • storeOtpMemory/verifyOtpMemory: Initial implementation using an in-memory Map with setTimeout for TTL. Marked clearly as non-production.
  • Redis function placeholders (storeOtpRedis/verifyOtpRedis) are included for later implementation in Section 6.

4. Building the API Layer (Fastify Routes)

Define the API endpoints for requesting and verifying OTPs.

4.1 OTP Routes (src/routes/otp.js):

import {
    generateOtp,
    storeOtpMemory,
    verifyOtpMemory,
    storeOtpRedis,
    verifyOtpRedis
} from '../services/otpService.js';

const otpPattern = `^\\d{${process.env.OTP_LENGTH || 6}}`;

const requestOtpSchema = {
  body: {
    type: 'object',
    required: ['phoneNumber'],
    properties: {

      phoneNumber: { type: 'string', pattern: '^\\+[1-9]\\d{1,14}' }
    },
    additionalProperties: false
  },
  response: {
    200: {
      type: 'object',
      properties: {
        message: { type: 'string' },
        messageId: { type: 'string' }
      }
    }

  }
};

const verifyOtpSchema = {
  body: {
    type: 'object',
    required: ['phoneNumber', 'otp'],
    properties: {
      phoneNumber: { type: 'string', pattern: '^\\+[1-9]\\d{1,14}' },
      otp: { type: 'string', pattern: otpPattern }
    },
    additionalProperties: false
  },
  response: {
    200: {
      type: 'object',
      properties: {
        success: { type: 'boolean' },
        message: { type: 'string' }
      }
    },
    400: {
        type: 'object',
        properties: {
            statusCode: { type: 'integer' },
            error: { type: 'string' },
            message: { type: 'string' }
        }
    }

  }
};

async function otpRoutes(fastify, options) {

  const rateLimitOptions = {
    max: fastify.config.RATE_LIMIT_MAX_REQUESTS,
    timeWindow: fastify.config.RATE_LIMIT_WINDOW_MS,

  };


  const useRedis = !!fastify.redis;
  const storeOtp = useRedis ? storeOtpRedis : storeOtpMemory;
  const verifyOtp = useRedis ? verifyOtpRedis : verifyOtpMemory;

  if (useRedis) {
      fastify.log.info('OTP routes will use Redis for storage.');
  } else {
      fastify.log.warn('OTP routes using in-memory storage (NOT FOR PRODUCTION).');
  }


  fastify.post('/request-otp', { schema: requestOtpSchema, config: { rateLimit: rateLimitOptions } }, async (request, reply) => {
    const { phoneNumber } = request.body;
    const otp = generateOtp(fastify.config.OTP_LENGTH);
    const message = `Your verification code is: ${otp}`;


    if (!useRedis && fastify.config.REDIS_URL) {
        fastify.log.error(`Cannot request OTP for ${phoneNumber}: Redis configured but unavailable.`);
        throw fastify.httpErrors.serviceUnavailable('OTP service dependency temporarily unavailable. Please try again later.');
    }

    try {
      fastify.log.info(`Requesting OTP for ${phoneNumber}`);


      const snsResult = await fastify.snsSMS.publish({
        message: message,
        phoneNumber: phoneNumber,
        messageAttributes: {
            'AWS.SNS.SMS.SMSType': {
                DataType: 'String',
                StringValue: fastify.config.SNS_SMS_TYPE
            },
            'AWS.SNS.SMS.SenderID': {
                DataType: 'String',
                StringValue: fastify.config.SNS_SMS_SENDER_ID
            }
        }
      });

      fastify.log.info(`SNS publish successful for ${phoneNumber}, MessageID: ${snsResult.MessageId}`);


      await storeOtp(fastify, phoneNumber, otp, fastify.config.OTP_TTL_SECONDS);

      return reply.code(200).send({
          message: 'OTP requested successfully. Check your phone.',
          messageId: snsResult.MessageId
        });

    } catch (error) {
      fastify.log.error({ err: error, phoneNumber }, `Failed to send or store OTP for ${phoneNumber}`);


      if (error.message?.includes('unavailable')) {
           throw fastify.httpErrors.serviceUnavailable('OTP service temporarily unavailable.');
      }
      if (error.code === 'InvalidParameterException') {

          throw fastify.httpErrors.badRequest('Invalid phone number format or region unsupported.');
      }
      if (error.code === 'AuthorizationError' || error.code === 'AccessDenied') {
          throw fastify.httpErrors.internalServerError('SNS authorization configuration error.');
      }

      throw fastify.httpErrors.internalServerError('Failed to process OTP request.');
    }
  });


  fastify.post('/verify-otp', { schema: verifyOtpSchema, config: { rateLimit: rateLimitOptions } }, async (request, reply) => {
    const { phoneNumber, otp } = request.body;


    if (!useRedis && fastify.config.REDIS_URL) {
        fastify.log.error(`Cannot verify OTP for ${phoneNumber}: Redis configured but unavailable.`);
        throw fastify.httpErrors.serviceUnavailable('OTP service dependency temporarily unavailable. Please try again later.');
    }

    fastify.log.info(`Verifying OTP for ${phoneNumber}`);

    try {

      const isValid = await verifyOtp(fastify, phoneNumber, otp);

      if (isValid) {
        return reply.code(200).send({ success: true, message: 'OTP verified successfully.' });
      } else {

        throw fastify.httpErrors.badRequest('Invalid or expired OTP.');
      }
    } catch (error) {

       fastify.log.error({ err: error, phoneNumber }, `Error during OTP verification for ${phoneNumber}`);

       if (error.message?.includes('unavailable')) {
           throw fastify.httpErrors.serviceUnavailable('OTP service temporarily unavailable.');
       }
       if (error.statusCode) {
           throw error;
       }
       throw fastify.httpErrors.internalServerError('Failed to verify OTP.');
    }
  });
}

export default otpRoutes;
  • Schemas: Corrected regex patterns for phoneNumber and otp. Added a note about using process.env for OTP\_LENGTH in the schema due to limitations in accessing fastify.config during schema definition.
  • Route Logic:
  • Imports both memory and Redis service functions.
  • Determines which storage functions (storeOtp, verifyOtp) to use based on whether fastify.redis is available after app setup.
  • Adds checks to ensure Redis is available if it was configured via REDIS\_URL, returning 503 if not.
  • Calls the selected storage functions (storeOtp/verifyOtp).
  • Includes robust try...catch blocks, using fastify.httpErrors for standard responses.
  • Applies route-specific rate limiting using config: { rateLimit: rateLimitOptions }.

5. Integrating Plugins and App Setup

Now, let's assemble the Fastify application, register plugins in the correct order, and wire up the routes.

5.1 Application Setup (src/app.js):

import Fastify from 'fastify';
import sensible from '@fastify/sensible';
import env from '@fastify/env';
import rateLimit from '@fastify/rate-limit';
import redisPlugin from '@fastify/redis';
import awsSns from 'fastify-aws-sns';
import environmentSchema from './config/environment.js';
import otpRoutes from './routes/otp.js';

async function buildApp(options = {}) {
  const fastify = Fastify({
    logger: {
        level: process.env.LOG_LEVEL || 'info',
        transport: process.env.NODE_ENV !== 'production'
            ? { target: 'pino-pretty' }
            : undefined,
    },
    ...options
  });


  await fastify.register(env, {
    dotenv: true,
    schema: environmentSchema,
    confKey: 'config'
  });

  fastify.log.level = fastify.config.LOG_LEVEL;
  fastify.log.info(`Log level set to: ${fastify.config.LOG_LEVEL}`);


  await fastify.register(sensible);



  let redisClient = null;
  if (fastify.config.REDIS_URL) {
    try {
      await fastify.register(redisPlugin, {
        url: fastify.config.REDIS_URL,
        closeClient: true

      });
      redisClient = fastify.redis;
      fastify.log.info(`Redis connection registered: ${fastify.config.REDIS_URL}`);
    } catch (err) {
      fastify.log.error({ err }, 'Failed to connect to Redis. Rate limiting and OTP storage will use in-memory fallback.');

    }
  } else {
    fastify.log.warn('REDIS_URL not configured. Using in-memory OTP storage and rate limiting.');
  }



  const rateLimitConfig = {
      global: false,
      ...(redisClient && { redis: redisClient })
  };
  await fastify.register(rateLimit, rateLimitConfig);
  if (redisClient) {
      fastify.log.info('Rate limiter configured to use Redis store.');
  } else {
      fastify.log.info('Rate limiter configured to use in-memory store.');
  }



  await fastify.register(awsSns);
  fastify.log.info('AWS SNS plugin registered. Using credentials from environment variables.');


  await fastify.register(otpRoutes, { prefix: '/api/v1' });


  fastify.addHook('onClose', (instance, done) => {
    instance.log.info('Server closing. Closing connections...');


    done();
  });

  return fastify;
}

export default buildApp;
  • Logging: Configures Pino logger, using pino-pretty (dev dependency). Log level set from fastify.config.
  • Plugin Registration Order: Critical for dependencies.
  1. @fastify/env: Loads config first.
  2. @fastify/sensible: Adds utilities.
  3. @fastify/redis: Conditionally registered based on REDIS\_URL. If successful, fastify.redis becomes available. Errors are caught.
  4. @fastify/rate-limit: Registered once. The configuration object passed during registration conditionally includes the redis client obtained from the previous step. This ensures rate-limit uses Redis if available, otherwise defaults to memory store.
  5. fastify-aws-sns: Registered to enable fastify.snsSMS.
  6. otpRoutes: Registers API endpoints.
  • Graceful Shutdown: onClose hook logs shutdown. @fastify/redis handles its own connection closing.

5.2 Server Entry Point (src/server.js):

import buildApp from './app.js';

async function start() {
  let app;
  try {

    app = await buildApp();


    const host = app.config.HOST;
    const port = app.config.PORT;


    await app.listen({ port: parseInt(port, 10), host: host });


    app.log.info(`Server listening on http://${host}:${port}`);
    app.log.info(`Using AWS Region: ${app.config.AWS_DEFAULT_REGION}`);
    if (app.redis) {
        app.log.info(`Successfully connected to Redis for OTP Store & Rate Limiting: ${app.config.REDIS_URL}`);
    } else if (app.config.REDIS_URL) {
         app.log.warn(`Redis connection FAILED (${app.config.REDIS_URL}). Using in-memory storage/rate-limiting.`);
    } else {
        app.log.warn(`Redis not configured. Using in-memory storage/rate-limiting (NOT FOR PRODUCTION).`);
    }

  } catch (err) {

    console.error('Error starting server:', err);
    if (app && app.log) {
        app.log.fatal({ err }, 'Failed to start server');
    }
    process.exit(1);
  }


    const K_TERMINATE_SIGNALS = ['SIGINT', 'SIGTERM'];
    K_TERMINATE_SIGNALS.forEach(signal => {
        process.on(signal, async () => {
            try {
                console.log(`Received ${signal}. Shutting down gracefully...`);
                if (app) {
                    await app.close();
                    console.log('Server closed.');
                }
                process.exit(0);
            } catch (err) {
                console.error('Error during graceful shutdown:', err);
                if (app && app.log) {
                    app.log.error({ err }, 'Error during shutdown.');
                }
                process.exit(1);
            }
        });
    });
}

start();
  • Imports buildApp.
  • Retrieves HOST and PORT from app.config.
  • Starts the server using app.listen().
  • Includes startup error handling.
  • Logs Redis status more accurately based on whether app.redis exists.
  • Implements signal handlers (SIGINT, SIGTERM) for graceful shutdown via app.close().

5.3 Update package.json Scripts:

Ensure your package.json includes scripts for starting, development, and testing:

  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch src/server.js | pino-pretty",
    "test": "tap \"test/**/*.test.js\""
  },
  • start: Runs the application normally.
  • dev: Runs with Node's watcher and pretty-printed logs (requires pino-pretty installed as a dev dependency).
  • test: Runs tests using tap (requires tap installed as a dev dependency).

The in-memory store is unsuitable for production. Let's implement the Redis storage logic in otpService.

6.1 Implement Redis Functions in otpService.js:

Fill in the placeholder functions in src/services/otpService.js.

import crypto from 'node:crypto';

Code

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 β†’