Tutorials

NestJS WhatsApp Integration with Sinch API: Step-by-Step Guide

Complete guide to integrating Sinch WhatsApp API with NestJS. Learn to send messages, handle webhooks, and implement secure signature verification with production-ready TypeScript code examples.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 1, 2026 β€’ 16 min read

Developer Quick-Start: Complete guide to integrating Sinch WhatsApp API with NestJS. Learn to send messages, handle webhooks, and implement secure signature verification with production-ready TypeScript code examples. 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.

Learn how to integrate WhatsApp messaging into your NestJS application using the Sinch Conversation API. This comprehensive guide covers sending WhatsApp messages, receiving webhook notifications, and implementing production-ready security patterns for Node.js backends.

Leverage NestJS's robust framework to build scalable applications that communicate with customers on the world's most popular messaging platform.

Technologies Used:

  • NestJS: A progressive Node.js framework for building efficient, reliable, and scalable server-side applications.
  • Sinch Conversation API: A unified API for managing conversations across multiple channels, including WhatsApp.
  • TypeScript: Provides static typing for better code quality and maintainability.
  • Node.js: The runtime environment.
  • (Optional) MongoDB & Mongoose: For persisting message history.
  • (Optional) Docker: For containerization and deployment consistency.

System Architecture:

+-----------------+      +-------------------+      +-----------------+      +-------------------+
|                 |----->|                   |----->|                 |----->|                   |
| Your Application|      | NestJS Backend    |      | Sinch Platform  |      | WhatsApp User     |
| (e.g., Frontend)|      | (This Guide)      |      | (Conversation API)|<-----|                   |
|                 |<-----|                   |<-----|                 |<-----|                   |
+-----------------+      +-------------------+      +-----------------+      +-------------------+
       |  (API Call to Send)         ^                      |  (API Call)              ^
       |                             | (Webhook Post)       |                          |
       +-----------------------------+                      +--------------------------+
                                  (Receive Message)                     (Send/Receive)

(Note: This ASCII diagram illustrates the flow. For final documentation, consider generating a visual diagram using tools like MermaidJS or other diagramming software.)

Prerequisites:

  • Node.js (LTS version recommended, v18+ for best compatibility) and npm/yarn installed.
  • NestJS CLI installed (npm install -g @nestjs/cli).
  • A Sinch account with access to the Conversation API (sign up at https://www.sinch.com/). For more on SMS integration options, see our phone number formatting guide.
  • A registered and approved WhatsApp Business Sender configured within your Sinch account.
  • A publicly accessible URL for receiving webhooks (ngrok can be used for local development, but a stable HTTPS domain/IP is needed for production).
  • Basic understanding of NestJS concepts (Modules, Controllers, Services). See official documentation at https://docs.nestjs.com/.
  • (Optional) MongoDB instance (local or cloud like MongoDB Atlas).
  • (Optional) Docker installed.

1. Project Setup and Configuration

Initialize your NestJS project and set up the basic structure and environment configuration.

1.1. Create NestJS Project:

Open your terminal and run:

nest new sinch-whatsapp-integration
cd sinch-whatsapp-integration

Choose your preferred package manager (npm or yarn).

1.2. Install Dependencies:

Install modules for HTTP requests, configuration management, data validation, and handling raw request bodies for webhooks. Optionally, add Mongoose for database interaction.

npm install @nestjs/config axios class-validator class-transformer body-parser @types/body-parser

npm install @nestjs/mongoose mongoose
  • @nestjs/config: Manages environment variables.
  • axios: A promise-based HTTP client for making requests to the Sinch API. (Alternatively, use NestJS's built-in HttpModule).
  • class-validator & class-transformer: For validating incoming webhook payloads and API request bodies.
  • body-parser & @types/body-parser: Required for accessing the raw request body, necessary for webhook signature verification.
  • @nestjs/mongoose & mongoose: For MongoDB integration (if storing messages).

1.3. Environment Variables:

Store sensitive credentials outside your codebase using a .env file – a security best practice.

Create a .env file in the project root:

.env

# .env

# === IMPORTANT ===
# The values starting with 'YOUR_' below are placeholders.
# You MUST replace them with your actual credentials from the Sinch portal.
# Do NOT commit this file with real secrets to version control.

# Sinch API Credentials
SINCH_SERVICE_PLAN_ID=YOUR_SINCH_SERVICE_PLAN_ID # Replace with your Service Plan ID from Sinch
SINCH_API_TOKEN=YOUR_SINCH_API_TOKEN           # Replace with your API Token from Sinch
SINCH_PROJECT_ID=YOUR_SINCH_PROJECT_ID         # Replace with your Project ID (often same as Service Plan ID, verify in Sinch portal)

# Sinch WhatsApp Sender ID (Phone Number associated with your WABA)
# Replace with your actual approved WhatsApp sender number in E.164 format (e.g., +12345550100)
SINCH_WHATSAPP_SENDER_ID=+12345678900 # EXAMPLE ONLY – REPLACE THIS

# Webhook Secret (Generate a strong random string for security)
# This is used to verify incoming webhooks from Sinch. You MUST provide the same secret in the Sinch portal webhook config.
SINCH_WEBHOOK_SECRET=YOUR_STRONG_RANDOM_SECRET # Replace with a strong, unique secret

# Application Port
PORT=3000

# Optional: Database Connection String (if using Mongoose)
# DATABASE_URL=mongodb://user:password@host:port/database

How to Obtain Sinch Credentials:

  1. Log in to your Sinch Customer Dashboard at https://dashboard.sinch.com/.
  2. Navigate to APIs > API Credentials. You should find your Service Plan ID and API Token. If you don't have one, create an API token. Copy these accurately.
  3. Your Project ID is typically associated with your Service Plan. Verify this in the portal.
  4. Navigate to Apps under the Conversation API section. Find or create an App configured for WhatsApp. The associated phone number is your SINCH\_WHATSAPP\_SENDER\_ID. This must be the exact, approved number in E.164 format (including + and country code, e.g., +12345678900).
  5. Generate a strong, unique secret for SINCH\_WEBHOOK\_SECRET (use a password manager or online generator). Provide this exact same secret to Sinch when configuring the webhook later. Treat this like a password.

Note on E.164 Format: All phone numbers used with Sinch Conversation API must be in E.164 format: +[country code][number] with no spaces, dashes, or parentheses. For example, a US number: +14155551234. This international phone number standard ensures proper routing across global telecommunications networks.

1.4. Configure Environment Module:

Load the .env file into the application using @nestjs/config.

Modify src/app.module.ts:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),

  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

1.5. Project Structure:

Organize your code into modules for better separation of concerns.

src/
β”œβ”€β”€ app.controller.ts
β”œβ”€β”€ app.module.ts
β”œβ”€β”€ app.service.ts
β”œβ”€β”€ main.ts
β”œβ”€β”€ config/              # Configuration related files (optional)
β”‚   └── sinch.config.ts
β”œβ”€β”€ sinch/               # Module for Sinch API interaction
β”‚   β”œβ”€β”€ sinch.module.ts
β”‚   β”œβ”€β”€ sinch.service.ts
β”‚   └── dto/             # Data Transfer Objects for Sinch API
β”‚       β”œβ”€β”€ send-message.dto.ts
β”‚       └── sinch-send-response.dto.ts # DTO for Sinch send response
β”œβ”€β”€ webhook/             # Module for handling incoming Sinch webhooks
β”‚   β”œβ”€β”€ webhook.module.ts
β”‚   β”œβ”€β”€ webhook.controller.ts
β”‚   β”œβ”€β”€ webhook.service.ts
β”‚   β”œβ”€β”€ guards/          # Custom guards (e.g., for webhook verification)
β”‚   β”‚   └── sinch-webhook.guard.ts
β”‚   └── dto/             # DTOs for incoming webhook payloads
β”‚       └── sinch-webhook.dto.ts # Renamed for clarity
└── database/            # Optional: Module for database interaction
    β”œβ”€β”€ database.module.ts
    β”œβ”€β”€ schemas/
    β”‚   └── message.schema.ts
    └── services/
        └── message-persistence.service.ts

Create these folders (sinch, webhook, config, etc.) within the src directory.


2. Implementing Core Functionality: Sending WhatsApp Messages

Create a dedicated NestJS service to interact with the Sinch Conversation API for sending WhatsApp messages programmatically.

2.1. Create Sinch Module and Service:

Generate the module and service using the NestJS CLI:

nest generate module sinch
nest generate service sinch

2.2. Define Sinch Configuration (Optional but Recommended):

Create a typed configuration file for Sinch settings.

import { registerAs } from '@nestjs/config';

export default registerAs('sinch', () => ({
  servicePlanId: process.env.SINCH_SERVICE_PLAN_ID,
  apiToken: process.env.SINCH_API_TOKEN,
  projectId: process.env.SINCH_PROJECT_ID || process.env.SINCH_SERVICE_PLAN_ID,
  whatsappSenderId: process.env.SINCH_WHATSAPP_SENDER_ID,
  webhookSecret: process.env.SINCH_WEBHOOK_SECRET,



  apiUrl: `https://us.conversation.api.sinch.com/v1/projects/${process.env.SINCH_PROJECT_ID || process.env.SINCH_SERVICE_PLAN_ID}`,
}));

Inject this configuration into your SinchService.

2.3. Implement SinchService:

This service contains the logic to call the Sinch API.

import { Injectable, Logger, Inject } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import axios, { AxiosInstance, AxiosError } from 'axios';
import * as crypto from 'crypto';
import sinchConfig from '../config/sinch.config';
import { SinchSendMessageResponseDto } from './dto/sinch-send-response.dto';

@Injectable()
export class SinchService {
  private readonly logger = new Logger(SinchService.name);
  private readonly httpClient: AxiosInstance;

  constructor(
    @Inject(sinchConfig.KEY)
    private config: ConfigType<typeof sinchConfig>,
  ) {

    if (!config.projectId || !config.apiToken || !config.whatsappSenderId || !config.webhookSecret) {
      this.logger.error('Sinch configuration incomplete. Check .env file for SINCH_PROJECT_ID, SINCH_API_TOKEN, SINCH_WHATSAPP_SENDER_ID, and SINCH_WEBHOOK_SECRET.');
      throw new Error('Sinch Service configuration is incomplete. Please check environment variables.');
    }

    this.httpClient = axios.create({
      baseURL: config.apiUrl,
      headers: {
        Authorization: `Bearer ${config.apiToken}`,
        'Content-Type': 'application/json',
      },
    });
  }


  async sendWhatsAppTextMessage(
    recipientPhoneNumber: string,
    textContent: string,
  ): Promise<SinchSendMessageResponseDto> {
    const endpoint = '/messages:send';





    const payload = {
      app_id: this.config.whatsappSenderId,
      recipient: {
        contact_id: recipientPhoneNumber,
      },
      message: {
        text_message: {
          text: textContent,
        },
      },
      channel_priority_order: ['WHATSAPP'],
    };

    this.logger.log(`Sending WhatsApp message to ${recipientPhoneNumber} via app_id ${this.config.whatsappSenderId}`);

    try {
      const response = await this.httpClient.post<SinchSendMessageResponseDto>(endpoint, payload);
      this.logger.log(`Message sent successfully. Message ID: ${response.data?.message_id}`);
      return response.data;
    } catch (error) {
      const axiosError = error as AxiosError;
      const errorDetails = axiosError.response?.data || axiosError.message;
      this.logger.error(
        `Failed to send WhatsApp message to ${recipientPhoneNumber}. Status: ${axiosError.response?.status}. Details: ${JSON.stringify(errorDetails)}`,
        axiosError.stack
      );

      throw error;
    }
  }





  <!-- GAP: Missing implementation examples for template messages and media messages (Type: Substantive, Priority: High) -->


   verifyWebhookSignature(
    rawBody: Buffer,
    signatureHeader: string | undefined,
    timestampHeader: string | undefined,
  ): boolean {
    if (!signatureHeader || !timestampHeader || !this.config.webhookSecret) {
      this.logger.warn('Webhook verification failed: Missing signature, timestamp, or webhook secret.');
      return false;
    }

    try {
      const hmac = crypto.createHmac('sha256', this.config.webhookSecret);
      const [signatureTimestamp, receivedSignature] = signatureHeader.split(',');


      if (!signatureTimestamp || !receivedSignature || signatureTimestamp !== `t=${timestampHeader}`) {
          this.logger.warn(`Webhook verification failed: Malformed or mismatched timestamp in signature header. Header: "${signatureHeader}", Timestamp: "${timestampHeader}"`);
          return false;
      }


      const signedPayload = `${timestampHeader}.${rawBody.toString('utf8')}`;
      const expectedSignature = hmac.update(signedPayload).digest('base64');


      const receivedSigBuffer = Buffer.from(receivedSignature, 'base64');
      const expectedSigBuffer = Buffer.from(expectedSignature, 'base64');


      if (receivedSigBuffer.length === expectedSigBuffer.length &&
          receivedSigBuffer.length > 0 &&
          crypto.timingSafeEqual(receivedSigBuffer, expectedSigBuffer)) {
        this.logger.debug('Webhook signature verified successfully.');
        return true;
      } else {
        this.logger.warn(`Webhook verification failed: Invalid signature. Received: "${receivedSignature}", Expected: "${expectedSignature}" (Base64)`);
        return false;
      }

    } catch (error) {
      this.logger.error('Error during webhook signature verification:', error);
      return false;
    }
  }
}

2.4. Update SinchModule:

Register the service and the configuration.

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SinchService } from './sinch.service';
import sinchConfig from '../config/sinch.config';

@Module({
  imports: [
    ConfigModule.forFeature(sinchConfig),
  ],
  providers: [SinchService],
  exports: [SinchService],
})
export class SinchModule {}

2.5. Import SinchModule into AppModule:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SinchModule } from './sinch/sinch.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    SinchModule,

  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

3. Building the API Layer (Example: Sending a Message)

Expose an endpoint to trigger sending a message.

3.1. Create DTOs for Sending Messages and Responses:

Define the expected request body structure and validation rules. Also, define a basic DTO for the Sinch API response.

import { IsNotEmpty, IsPhoneNumber, IsString, MaxLength } from 'class-validator';

export class SendMessageDto {
  @IsNotEmpty({ message: 'Recipient phone number should not be empty.' })
  @IsPhoneNumber(null, { message: 'Recipient phone number must be a valid E.164 format phone number (e.g., +15551234567).' })
  readonly recipientPhoneNumber: string;

  @IsNotEmpty({ message: 'Message text should not be empty.' })
  @IsString()
  @MaxLength(4096, { message: 'WhatsApp message text cannot exceed 4096 characters per message.' })
  readonly messageText: string;
}
export class SinchSendMessageResponseDto {
  message_id: string;
  accepted_time?: string;

}

3.2. Create a Controller (e.g., add to AppController or create a dedicated MessageController):

import { Controller, Get, Post, Body, UsePipes, ValidationPipe, HttpCode, HttpException, HttpStatus, Logger } from '@nestjs/common';
import { AppService } from './app.service';
import { SinchService } from './sinch/sinch.service';
import { SendMessageDto } from './sinch/dto/send-message.dto';
import { AxiosError } from 'axios';

@Controller()
export class AppController {
  private readonly logger = new Logger(AppController.name);

  constructor(
    private readonly appService: AppService,
    private readonly sinchService: SinchService,
  ) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('send-whatsapp')
  @HttpCode(HttpStatus.ACCEPTED)
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }))
  async sendWhatsAppMessage(@Body() sendMessageDto: SendMessageDto) {
    try {
      const result = await this.sinchService.sendWhatsAppTextMessage(
        sendMessageDto.recipientPhoneNumber,
        sendMessageDto.messageText,
      );

      return { success: true, messageId: result.message_id };
    } catch (error) {

      if (error instanceof AxiosError && error.response) {

          this.logger.error(`Sinch API Error: Status ${error.response.status}, Data: ${JSON.stringify(error.response.data)}`);
          throw new HttpException(
              `Failed to send message via Sinch: ${error.response.data?.message || error.response.statusText || 'API error'}`,
              error.response.status || HttpStatus.BAD_GATEWAY
          );
      } else if (error instanceof Error) {
          this.logger.error(`Internal Error: ${error.message}`, error.stack);
          throw new HttpException(`Internal server error: ${error.message}`, HttpStatus.INTERNAL_SERVER_ERROR);
      } else {
          this.logger.error('Unknown Error sending message', error);
          throw new HttpException('An unexpected error occurred', HttpStatus.INTERNAL_SERVER_ERROR);
      }
    }
  }
}

Explanation:

  1. Injection: SinchService is injected.
  2. Endpoint: POST /send-whatsapp is defined.
  3. Validation: @UsePipes(new ValidationPipe(...)) validates the request body against SendMessageDto. forbidNonWhitelisted: true rejects requests with extra fields.
  4. Logic: Calls sinchService.sendWhatsAppTextMessage.
  5. Response: Returns 202 Accepted with the messageId on success.
  6. Error Handling: Catches errors, specifically checks for AxiosError to provide more context from Sinch API failures, and throws appropriate HttpException instances which NestJS handles.

3.3. Testing the Endpoint:

Run your NestJS application:

npm run start:dev

Use curl or Postman. Using curl (note the use of single quotes around the JSON data for shell safety):

curl -X POST http://localhost:3000/send-whatsapp \
-H 'Content-Type: application/json' \
-d '{
  "recipientPhoneNumber": "+15551234567",
  "messageText": "Hello from NestJS via Sinch!"
}'

Expected Response (Success – Status Code 202):

{
  "success": true,
  "messageId": "01HXXXX..."
}

Expected Response (Validation Error – Status Code 400):

{
  "statusCode": 400,
  "message": [
    "Recipient phone number must be a valid E.164 format phone number (e.g., +15551234567)."
  ],
  "error": "Bad Request"
}

Expected Response (Sinch API Error – Status Code e.g., 400, 401, 503):

The response will reflect the HttpException thrown in the controller, e.g.:

{
  "statusCode": 400,
  "message": "Failed to send message via Sinch: Invalid recipient phone number"
}

4. Receiving WhatsApp Messages: Webhook Integration

Sinch notifies your NestJS application about incoming WhatsApp messages and delivery status updates via webhooks. Create a secure endpoint to receive and process these real-time notifications.

4.1. Configure Webhook in Sinch Dashboard:

  1. Log in to your Sinch Customer Dashboard.
  2. Navigate to Apps under the Conversation API section.
  3. Select the App associated with your WhatsApp sender.
  4. Find the Webhook or Callback URL settings.
  5. Enter the publicly accessible URL pointing to the webhook endpoint you'll create (e.g., https://your-public-domain.com/webhook/sinch or your ngrok URL like https://<your-id>.ngrok.io/webhook/sinch). This URL MUST use HTTPS.
  6. Crucially: Find the field for the Webhook Secret. Paste the exact same secret you defined in your .env file (SINCH\_WEBHOOK\_SECRET). This is used for signature verification. Ensure there are no typos or extra spaces.
  7. Select the events you want to subscribe to (e.g., MESSAGE\_INBOUND, MESSAGE\_DELIVERY). Start with MESSAGE\_INBOUND.
  8. Save the configuration.

Screenshot Example (Conceptual – Actual UI may vary):

+-------------------------------------------------+
| Sinch App Configuration                         |
+-------------------------------------------------+
| App Name: My WhatsApp Service                   |
| App ID: <your_app_id>                           |
|                                                 |
| Webhook Settings:                               |
|   Callback URL: [ https://<your_domain>/webhook/sinch ] | (Must be HTTPS)
|   Webhook Secret: [ *************************** ] | <--- Paste your exact secret here
|                                                 |
| Subscribed Events:                              |
|   [x] MESSAGE_INBOUND                           |
|   [x] MESSAGE_DELIVERY                          |
|   [ ] CONTACT_CREATE                            |
|   ...                                           |
|                                                 |
|   [ Save Changes ]                              |
+-------------------------------------------------+

4.2. Create Webhook Module, Controller, and Service:

nest generate module webhook
nest generate controller webhook
nest generate service webhook

4.3. Enable Raw Body Parsing (Via Middleware):

Webhook signature verification requires the raw, unparsed request body. Apply body-parser's raw body middleware specifically to the webhook route. This is configured in the WebhookModule (see section 4.4).

Modify src/main.ts to ensure global pipes or other global body parsers don't interfere, although selective middleware is preferred:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {

  const app = await NestFactory.create(AppModule);


  const configService = app.get(ConfigService);
  const port = configService.get<number>('PORT', 3000);
  const logger = new Logger('Bootstrap');




  await app.listen(port);
  logger.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

4.4. Create Webhook Verification Guard & Configure Middleware:

This guard checks the x-sinch-signature header.

import { Injectable, CanActivate, ExecutionContext, Logger, RawBodyRequest, BadRequestException, ForbiddenException } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Request } from 'express';
import { SinchService } from '../../sinch/sinch.service';

@Injectable()
export class SinchWebhookGuard implements CanActivate {
  private readonly logger = new Logger(SinchWebhookGuard.name);

  constructor(private readonly sinchService: SinchService) {}

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest<RawBodyRequest<Request>>();


    const rawBody = request.rawBody;
    if (!rawBody) {

        this.logger.error('Webhook Guard Error: Raw body not available. Ensure raw body parsing middleware is applied correctly to the webhook route.');

        throw new BadRequestException('Server configuration error: Raw body not available for webhook verification.');
    }

    const signature = request.headers['x-sinch-signature'] as string | undefined;
    const timestamp = request.headers['x-sinch-timestamp'] as string | undefined;

    this.logger.debug(`Verifying webhook signature. Timestamp: ${timestamp}, Signature Header: ${signature ? signature.substring(0, 10) + '...' : 'MISSING'}`);

    const isValid = this.sinchService.verifyWebhookSignature(
      rawBody,
      signature,
      timestamp,
    );

    if (!isValid) {
      this.logger.warn(`Invalid webhook signature received from IP: ${request.ip}. Blocking request.`);

      throw new ForbiddenException('Invalid webhook signature.');
    }

    this.logger.log('Webhook signature verified successfully.');
    return true;
  }
}

Middleware Configuration (Important!): Apply the raw body parser middleware in WebhookModule.

Modify src/webhook/webhook.module.ts:

import { Module, MiddlewareConsumer, NestModule, RequestMethod } from '@nestjs/common';
import { WebhookController } from './webhook.controller';
import { WebhookService } from './webhook.service';
import { SinchModule } from '../sinch/sinch.module';
import * as bodyParser from 'body-parser';

@Module({
  imports: [SinchModule],
  controllers: [WebhookController],
  providers: [WebhookService],
})
export class WebhookModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer

      .apply(bodyParser.raw({ type: 'application/json' }))
      .forRoutes({ path: 'webhook/sinch', method: RequestMethod.POST });
  }
}

Import WebhookModule into AppModule:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SinchModule } from './sinch/sinch.module';
import { WebhookModule } from './webhook/webhook.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    SinchModule,
    WebhookModule,

  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

4.5. Define DTO for Incoming Webhooks:

Map the structure of Sinch's webhook payloads. This DTO handles multiple event types using the trigger field. Note: The original provided DTO was incomplete; this version provides a more structured example for MESSAGE_INBOUND.

import { Type } from 'class-transformer';
import { IsString, IsNotEmpty, ValidateNested, IsOptional, IsEnum, Allow, IsObject } from 'class-validator';

class ContactIdentifier {
  @IsOptional() @IsString() phone?: string;
  @IsOptional() @IsString() user_id?: string;
  @IsOptional() @IsString() whatsapp_id?: string;
}

class RecipientInfo {

  @Allow() contact_id?: ContactIdentifier | string;
  @IsOptional() @IsString() identified_by?: string;
}

class SenderInfo {
  @Allow() contact_id?: ContactIdentifier | string;
  @IsOptional() @IsString() user_display_name?: string;
}

class TextMessageContent {
  @IsNotEmpty() @IsString() text: string;
}

class MessageContent {
  @IsOptional() @ValidateNested() @Type(() => TextMessageContent)
  text_message?: TextMessageContent;




}

class MessageMetadata {
    @IsOptional() @IsString() locale?: string;

}

class InboundMessagePayload {
  @IsNotEmpty() @IsString() id: string;

  @IsOptional() @IsString() direction?: 'INBOUND';

  @IsNotEmpty() @ValidateNested() @Type(() => RecipientInfo)
  recipient: RecipientInfo;

  @IsNotEmpty() @ValidateNested() @Type(() => SenderInfo)
  sender: SenderInfo;

  @IsNotEmpty() @ValidateNested() @Type(() => MessageContent)
  message: MessageContent;

  @IsNotEmpty() @IsString() @IsEnum(['WHATSAPP', 'SMS', 'MESSENGER'], { message: 'Channel must be a valid Sinch channel' })
  channel: string;

  @IsNotEmpty() @IsString()
  timestamp: string;

  @IsOptional() @ValidateNested() @Type(() => MessageMetadata)
  metadata?: MessageMetadata;

  @IsOptional() @IsString() app_id?: string;

  @IsOptional() @IsString() conversation_id?: string;

  @IsOptional() @IsString() contact_id?: string;

  @IsOptional() @IsString() processing_mode?: string;
}

export class SinchWebhookDto {
  @IsNotEmpty() @IsString() trigger: string;

  @IsNotEmpty() @IsString() app_id: string;

  @IsNotEmpty() @IsString()
  accepted_time: string;




  @IsOptional() @ValidateNested() @Type(() => InboundMessagePayload)
  message_inbound?: InboundMessagePayload;






  [key: string]: any;
}

4.6. Implement Webhook Controller:

4.7. Implement Webhook Service:


5. Database Integration (Optional)


6. Testing Strategy


7. Security Best Practices


8. Deployment Guide


9. Monitoring and Observability


10. Troubleshooting Common Issues


Frequently Asked Questions

How do I authenticate with the Sinch WhatsApp API in NestJS?

Authenticate using Bearer token authentication. Store your SINCH_API_TOKEN in your .env file and include it in the Authorization header as Bearer YOUR_TOKEN for all API requests. The Sinch API Token is available in your Sinch Dashboard under APIs > API Credentials. Use @nestjs/config to inject the token securely into your services and create an axios instance with the Authorization header configured globally.

What phone number format does Sinch require for WhatsApp messaging?

Sinch requires E.164 format for all phone numbers: +[country code][number] with no spaces, dashes, or parentheses. For example, a US number would be +14155551234, not (415) 555-1234 or 415-555-1234. This international standard ensures proper routing across global telecommunications networks. Use the @IsPhoneNumber() validator from class-validator in your DTOs to enforce this format. Learn more about E.164 phone number formatting.

How do I verify webhook signatures from Sinch?

Sinch uses HMAC-SHA256 signature verification with Base64 encoding. Extract the x-sinch-signature header (format: t=<timestamp>,<base64_signature>) and x-sinch-timestamp header. Construct the signed payload as <timestamp>.<raw_request_body_utf8>, compute HMAC-SHA256 using your webhook secret, encode as Base64, and compare using crypto.timingSafeEqual() to prevent timing attacks. Apply body-parser.raw() middleware specifically to webhook routes to preserve the raw body. See official documentation at https://developers.sinch.com/docs/conversation/keyconcepts/#callback-signature-verification.

How do I set up NestJS modules for Sinch integration?

Create separate modules for concerns: a SinchModule for API interaction (services for sending messages), a WebhookModule for receiving callbacks (controller with guards for signature verification), and optionally a DatabaseModule for persisting message history. Use @nestjs/config with ConfigModule.forFeature() to inject typed configuration into each module. Export SinchService from SinchModule so other modules can inject it, particularly the WebhookGuard for signature verification.

What is the WhatsApp message length limit through Sinch?

WhatsApp Business API allows up to 4,096 characters per text message through Sinch Conversation API. Use @MaxLength(4096) validator in your SendMessageDto to enforce this limit. Messages exceeding this length will be rejected by the API. For longer content, consider breaking it into multiple messages or using media attachments. Template messages may have different character limits depending on the template configuration.

How do I handle errors from the Sinch API in NestJS?

Wrap Sinch API calls in try-catch blocks and check for AxiosError instances to extract detailed error information. Access error.response.status for HTTP status codes and error.response.data for Sinch-specific error details. Throw appropriate HttpException instances with meaningful messages (e.g., BadRequestException, UnauthorizedException, BadGatewayException). Log detailed errors internally using NestJS Logger but return generic messages to clients to avoid exposing sensitive information.

How can I test WhatsApp webhooks locally in NestJS?

Yes, use ngrok to create a secure tunnel to your local NestJS development server. Install ngrok (npm install -g ngrok), run ngrok http 3000 (or your port), and use the generated HTTPS URL (e.g., https://abc123.ngrok.io/webhook/sinch) in your Sinch Dashboard webhook configuration. Ensure you configure the same webhook secret in both your .env file and Sinch Dashboard. ngrok provides a web interface at http://localhost:4040 to inspect webhook payloads, which is invaluable for debugging.

What environment variables are required for production deployment?

For production, configure these required environment variables: SINCH_SERVICE_PLAN_ID, SINCH_API_TOKEN, SINCH_PROJECT_ID, SINCH_WHATSAPP_SENDER_ID (E.164 format), and SINCH_WEBHOOK_SECRET. Never commit .env files to version control. Use your hosting provider's environment variable configuration (e.g., AWS Systems Manager, Heroku Config Vars, Vercel Environment Variables). Ensure the apiUrl in your Sinch config matches your account's region (us or eu). Verify all credentials are correctly loaded on startup using validation in your service constructor.

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