Tutorials

How to Send MMS Messages with Sinch in Node.js Express: Complete Tutorial

Learn how to send MMS multimedia messages using Sinch API in Node.js Express. Step-by-step guide covering MMS setup, mt_media type, file size limits (1 MB), supported image/video formats, error handling, and production deployment.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 7, 2026 β€’ 25 min read

Developer Quick-Start: Learn how to send MMS multimedia messages using Sinch API in Node.js Express. Step-by-step guide covering MMS setup, mt_media type, file size limits (1 MB), supported image/video formats, error handling, and production deployment. 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 build a production-ready Node.js application using Express to send MMS (Multimedia Messaging Service) messages via the Sinch API. This comprehensive tutorial covers project setup, MMS implementation, API creation, error handling, security best practices, and deployment.

By the end, you'll have a functional Express API endpoint that sends MMS messages with images, videos, or audio files using the Sinch XMS API and your Sinch account credentials.

What Will You Build with Sinch MMS?

Goal: Create a reliable backend service that sends MMS messages programmatically using Sinch.

Problem Solved: Automate sending rich media messages (images, audio, video) to users – essential for notifications, marketing campaigns, appointment reminders with QR codes, customer support with visual instructions, e-commerce order confirmations with product images, and event invitations with venue photos.

When to Use MMS vs. SMS:

Use Case Choose MMS Choose SMS
Visual content required βœ“ Product images, QR codes, maps βœ—
Plain text sufficient βœ— βœ“ Maximum deliverability
Branding/marketing βœ“ Rich visual experience βœ—
Cost-sensitive βœ— Higher per-message cost βœ“ Lower cost
Time-sensitive alerts βœ— Slightly slower delivery βœ“ Faster delivery

Common Use Cases:

  • Appointment Reminders: Send calendar invites with location maps
  • Marketing Campaigns: Deliver product images with promotional offers
  • Customer Support: Provide visual troubleshooting guides
  • Order Confirmations: Include product images and shipping QR codes
  • Event Invitations: Share venue photos and ticket barcodes

Technologies:

  • Node.js v16+: JavaScript runtime for building scalable server-side applications
  • Express v4.18+: Minimal and flexible Node.js web application framework for building APIs
  • Axios v1.6+: Promise-based HTTP client for making requests to the Sinch API
  • dotenv v16+: Module to load environment variables from a .env file
  • Winston v3.11+: Versatile logging library
  • express-validator v7+: Middleware for request input validation
  • express-rate-limit v7+: Middleware for basic API rate limiting
  • helmet v7+: Middleware for setting security-related HTTP headers
  • axios-retry v4+: Plugin to automatically retry failed Axios requests
  • Sinch MMS JSON API: XMS API endpoint for sending MMS messages via the /batches endpoint with mt\_media type

Compatibility Notes:

  • Requires Node.js 16 or higher for native ES modules support
  • Express 4.x and 5.x both supported
  • All dependencies support Node.js LTS versions

System Architecture:

graph LR
    Client[Client Application / cURL] -- HTTP POST Request --> ExpressApp[Node.js/Express API]
    ExpressApp -- Validate Request --> ExpressApp
    ExpressApp -- Call Sinch Service --> SinchService[Sinch MMS Service Logic]
    SinchService -- Build JSON Payload --> SinchService
    SinchService -- POST Request (Axios w/ Retry) --> SinchAPI[Sinch XMS API (/batches)]
    SinchAPI -- Send MMS --> MobileNetwork[Mobile Network]
    MobileNetwork -- Deliver MMS --> UserDevice[User's Mobile Device]
    SinchAPI -- API Response (Success/Failure) --> SinchService
    SinchService -- Process Response --> ExpressApp
    ExpressApp -- HTTP Response (Success/Error) --> Client

Prerequisites:

  • Install Node.js v16+ and npm (or yarn)
  • Create a Sinch account and confirm your plan includes MMS API access. Note: In the US region, contact your Sinch account manager to enable MMS functionality for your Service Plan ID.
  • Provision a phone number (Short Code, Toll-Free, or 10DLC) within your Sinch account. Verify in the Sinch dashboard (usually under "Numbers" β†’ "Your Numbers") that the chosen number has MMS sending capabilities enabled.
  • Obtain your Sinch Service Plan ID and API Token from your Sinch Customer Dashboard under SMS β†’ APIs β†’ REST API Configuration. (Dashboard layouts may vary – look for API credentials or REST API settings if this path differs.)
  • Host your media file (image, audio, video) at a publicly accessible URL. Sinch fetches content from this URL via HTTP GET. The hosting server must return a valid Content-Type header (e.g., image/jpeg, video/mp4) and a Content-Length header.

Media Hosting Solutions:

Choose a reliable hosting solution for your MMS media files:

  • CDN Providers: Cloudflare, AWS CloudFront, Fastly (recommended for production)
  • Cloud Storage: AWS S3, Google Cloud Storage, Azure Blob Storage with public read access
  • Media Services: Cloudinary, Imgix (automatic format optimization)
  • Requirements: HTTPS support, Content-Type headers, Content-Length headers, 99.9%+ uptime

Source: Sinch MMS Support Documentation

MMS File Size Limits and Best Practices:

  • Recommended limit: Keep media files under 1 MB – most MMS providers use this limit
  • Without transcoding: Keep video and image files under 500 KB, with a maximum of 740 KB
  • With Sinch transcoding: Keep files under 1 MB
  • Base64 encoding overhead: Sinch delivers MMS messages in Base64 encoding. To estimate final size, multiply the file size by 1.37 and add 814 bytes for headers
  • Content-Type requirement: Serve all media files with a valid Content-Type header (e.g., text/plain, image/gif, audio/mp3, video/mp4)

File Size Calculation Examples:

Original File Calculation Final Size Status
300 KB image (300 Γ— 1.37) + 0.795 KB ~411.8 KB βœ“ Safe
500 KB image (500 Γ— 1.37) + 0.795 KB ~685.8 KB βœ“ Safe
800 KB video (800 Γ— 1.37) + 0.795 KB ~1,096.8 KB βœ— Exceeds 1 MB
730 KB video (730 Γ— 1.37) + 0.795 KB ~1,000.9 KB βœ— Exceeds 1 MB

What Happens When Files Exceed Limits:

  • Carriers may reject the message entirely
  • Message may downgrade to SMS with a link instead of inline media
  • Delivery may fail silently without error notification
  • Some carriers compress media, reducing quality significantly

Source: Sinch MMS Best Practices

Supported Media Types:

Media Type Formats Recommended Size Max Dimension Notes
Images GIF, JPG, PNG < 500 KB 1024Γ—768 px Use JPEG for photos, PNG for graphics
Audio MP3, WAV < 500 KB N/A MP3 recommended for smaller size
Video 3GP, MP4, MPEG, MPG, AVI, WMV < 500 KB 640Γ—480 px MP4 H.264 codec recommended
Text TEXT/PLAIN < 100 KB N/A Plain text files

Format-Specific Recommendations:

  • Images: Use JPEG with 80% quality for optimal size/quality balance
  • Videos: Use H.264 codec with 30 fps, 640Γ—480 resolution
  • Audio: Use MP3 at 128 kbps bitrate
  • Avoid: BMP, TIFF, uncompressed formats

Source: Sinch MMS Sizes and Limitations

How Do You Set Up Your Node.js Project for Sinch MMS?

Initialize your Node.js project and install the necessary dependencies for sending MMS messages with Sinch.

  1. Create Project Directory: Open your terminal and create a new directory for the project.
   mkdir sinch-mms-sender
   cd sinch-mms-sender
  1. Initialize Node.js Project: Create a package.json file.
   npm init -y
  1. Enable ES Modules: Use import/export syntax by adding "type": "module" to your package.json. Alternatively, rename all .js files to .mjs. Open package.json and add:
   {
     "name": "sinch-mms-sender",
     "version": "1.0.0",
     "description": "",
     "main": "src/server.js",
     "type": "module",
     "scripts": {
     },
   }
  1. Install Dependencies: Install Express, Axios, dotenv, validation, rate limiting, security headers, logging, and retry libraries with pinned versions for production stability.
   npm install express@^4.18.0 axios@^1.6.0 dotenv@^16.0.0 express-validator@^7.0.0 express-rate-limit@^7.0.0 helmet@^7.0.0 winston@^3.11.0 axios-retry@^4.0.0
  1. Install Development Dependencies: Install nodemon for automatic server restarts during development.
   npm install --save-dev nodemon
  1. Create Project Structure: Organize the code for better maintainability.
   mkdir src
   mkdir src/routes
   mkdir src/controllers
   mkdir src/services
   mkdir src/middleware
   mkdir src/config
   touch src/server.js
   touch src/routes/mmsRoutes.js
   touch src/controllers/mmsController.js
   touch src/services/sinchService.js
   touch src/middleware/authMiddleware.js
   touch src/config/logger.js
   touch .env
   touch .gitignore
  1. Configure .gitignore: Prevent sensitive files and unnecessary modules from version control.

.env

   # .gitignore

   node_modules
   .env
   npm-debug.log
   logs/
   *.log
  1. Set Up Environment Variables (.env): Store your sensitive credentials and configuration here. Never commit this file.

.env

   # .env

   # Server Configuration
   PORT=3000
   NODE_ENV=development # Use "production" in deployment

   # Sinch API Credentials & Config
   SINCH_SERVICE_PLAN_ID=YOUR_SERVICE_PLAN_ID_HERE
   SINCH_API_TOKEN=YOUR_API_TOKEN_HERE
   SINCH_MMS_NUMBER=+1YOURSINCHNUMBER # Use E.164 format (e.g., +12223334444)
   # Regional Endpoints: us.sms.api.sinch.com (US) or eu.sms.api.sinch.com (EU)
   SINCH_API_BASE_URL=https://us.sms.api.sinch.com # Choose based on your region

   # Basic API Key for Authentication
   # IMPORTANT: Generate a strong, random key for production!
   # Example generation: openssl rand -base64 32
   INTERNAL_API_KEY=REPLACE_WITH_A_SECURE_RANDOM_KEY

   # Logging Configuration
   LOG_LEVEL=debug # Use "info" or "warn" in production
   # SENTRY_DSN=YOUR_SENTRY_DSN_HERE # Optional: For Sentry integration
  • PORT: Port the Express server listens on
  • NODE_ENV: Environment type (development, production)
  • SINCH_SERVICE_PLAN_ID, SINCH_API_TOKEN: Your credentials from the Sinch Dashboard (see Prerequisites)
  • SINCH_MMS_NUMBER: The Sinch phone number you send MMS from (E.164 format)
  • SINCH_API_BASE_URL: Base URL for the Sinch API region. Regional endpoints: us.sms.api.sinch.com (US) or eu.sms.api.sinch.com (EU). Choose based on your location and data protection requirements.
  • INTERNAL_API_KEY: Crucial: Replace the placeholder with a cryptographically secure random string. This key protects your API endpoint.
  • LOG_LEVEL: Controls logging verbosity

Environment Variable Validation Strategy:

Add startup validation to catch configuration errors early. Create src/config/validateEnv.js:

export function validateEnv() {
    const required = [
        'SINCH_SERVICE_PLAN_ID',
        'SINCH_API_TOKEN',
        'SINCH_MMS_NUMBER',
        'SINCH_API_BASE_URL',
        'INTERNAL_API_KEY'
    ];

    const missing = required.filter(key => !process.env[key]);

    if (missing.length > 0) {
        throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
    }


    if (!process.env.SINCH_MMS_NUMBER.match(/^\+[1-9]\d{1,14}$/)) {
        throw new Error('SINCH_MMS_NUMBER must be in E.164 format (e.g., +12223334444)');
    }


    if (process.env.INTERNAL_API_KEY === 'REPLACE_WITH_A_SECURE_RANDOM_KEY') {
        console.warn('WARNING: Using default INTERNAL_API_KEY. Generate a secure key for production!');
    }
}

Team Onboarding Template (.env.example):

.env

# .env.example - Copy to .env and replace with your actual values

# Server Configuration
PORT=3000
NODE_ENV=development

# Sinch API Credentials (Get from https://dashboard.sinch.com/sms/api/rest)
SINCH_SERVICE_PLAN_ID=YOUR_SERVICE_PLAN_ID_HERE
SINCH_API_TOKEN=YOUR_API_TOKEN_HERE
SINCH_MMS_NUMBER=+1YOURSINCHNUMBER

# Regional Endpoints: us.sms.api.sinch.com (US) or eu.sms.api.sinch.com (EU)
SINCH_API_BASE_URL=https://us.sms.api.sinch.com

# Generate secure key: openssl rand -base64 32
INTERNAL_API_KEY=REPLACE_WITH_A_SECURE_RANDOM_KEY

# Logging
LOG_LEVEL=debug

Source: Sinch SMS API Reference

  1. Add npm Scripts: Add scripts to package.json for running, linting, and testing the server.
   "scripts": {
     "start": "node src/server.js",
     "dev": "nodemon src/server.js",
     "lint": "eslint src/**/*.js",
     "format": "prettier --write src/**/*.js",
     "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
   },

How Do You Implement the Sinch MMS Service in Node.js?

Create a dedicated service to handle interactions with the Sinch MMS API, incorporating logging and retry logic.

src/config/logger.js

import winston from 'winston';
import dotenv from 'dotenv';

dotenv.config();

const { combine, timestamp, json, errors, colorize, printf } = winston.format;

const consoleFormat = combine(
    colorize(),
    timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    printf(({ timestamp, level, message, stack }) => {
        return `${timestamp} ${level}: ${stack || message}`;
    })
);

const fileFormat = combine(
    timestamp(),
    errors({ stack: true }),
    json()
);

const logger = winston.createLogger({
    level: process.env.LOG_LEVEL || 'info',
    format: fileFormat,
    transports: [

        ...(process.env.NODE_ENV !== 'production' ? [
            new winston.transports.Console({
                format: consoleFormat,
                handleExceptions: true,
            })
        ] : []),




    ],
    exitOnError: false,
});

if (process.env.NODE_ENV === 'production' && logger.transports.length === 0) {
    logger.add(new winston.transports.Console({
        format: fileFormat,
        handleExceptions: true,
    }));
     logger.info('Production environment detected, configuring JSON console logging.');
} else if (process.env.NODE_ENV !== 'production') {
     logger.info('Development environment detected, configuring colorful console logging.');
}

export default logger;

Log Rotation and Retention:

For production environments, add log rotation to prevent disk space issues:

import 'winston-daily-rotate-file';

new winston.transports.DailyRotateFile({
    filename: 'logs/application-%DATE%.log',
    datePattern: 'YYYY-MM-DD',
    maxSize: '20m',
    maxFiles: '14d',
    format: fileFormat
})

External Logging Integration:

For production monitoring, integrate with services like Datadog, New Relic, or Sentry:

import '@sentry/node';
import winston from 'winston';

if (process.env.SENTRY_DSN) {
    logger.add(new winston.transports.Sentry({
        dsn: process.env.SENTRY_DSN,
        level: 'error'
    }));
}

src/services/sinchService.js

import axios from 'axios';
import axiosRetry from 'axios-retry';
import dotenv from 'dotenv';
import logger from '../config/logger.js';

dotenv.config();

const SINCH_API_BASE_URL = process.env.SINCH_API_BASE_URL;
const SINCH_SERVICE_PLAN_ID = process.env.SINCH_SERVICE_PLAN_ID;
const SINCH_API_TOKEN = process.env.SINCH_API_TOKEN;
const SINCH_MMS_NUMBER = process.env.SINCH_MMS_NUMBER;

if (!SINCH_API_BASE_URL || !SINCH_SERVICE_PLAN_ID || !SINCH_API_TOKEN || !SINCH_MMS_NUMBER) {
    logger.error("FATAL ERROR: Missing required Sinch environment variables. Check .env file.");
    process.exit(1);
}

const SINCH_XMS_ENDPOINT = `${SINCH_API_BASE_URL}/xms/v1/${SINCH_SERVICE_PLAN_ID}/batches`;

const sinchAxios = axios.create({
    timeout: 30000,
});

axiosRetry(sinchAxios, {
    retries: 3,
    retryDelay: (retryCount, error) => {
        logger.warn(`Retry attempt ${retryCount} for request to ${error.config.url} due to ${error.message}`);
        return retryCount * 1000;
    },
    retryCondition: (error) => {

        const shouldRetry = axiosRetry.isNetworkOrIdempotentRequestError(error) ||
                           (error.response && error.response.status >= 500);
        if (shouldRetry) {
            logger.info(`Condition met for retry: ${error.message}`);
        }
        return shouldRetry;
    },
    shouldResetTimeout: true,
});

export const sendMms = async (
    recipientPhoneNumber,
    mediaUrl,
    mediaType,
    messageText = '',
    subject = '',
    fallbackText = '',
    strictValidation = false
) => {


    const batchPayload = {
        to: [recipientPhoneNumber],
        from: SINCH_MMS_NUMBER,
        body: {
            type: "mt_media",
            url: mediaUrl,
            message: messageText || undefined,
            parameters: {

                ...(subject && { "mms_subject": { default: subject } }),
                ...(fallbackText && { "mms_fallback_text": { default: fallbackText } }),

            }
        },

        ...(strictValidation && { strict_validation: true }),



    };


    if (Object.keys(batchPayload.body.parameters).length === 0) {
        delete batchPayload.body.parameters;
    }

    if (!batchPayload.body.message) {
        delete batchPayload.body.message;
    }

    const headers = {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${SINCH_API_TOKEN}`
    };

    logger.info(`Attempting to send MMS via Sinch XMS to ${recipientPhoneNumber} from ${SINCH_MMS_NUMBER}`);
    logger.debug(`Using Endpoint: ${SINCH_XMS_ENDPOINT}`);
    logger.debug("Request Payload:", { payload: batchPayload });

    try {

        const response = await sinchAxios.post(SINCH_XMS_ENDPOINT, batchPayload, { headers });

        logger.info('Sinch API Response Received:', { status: response.status, data: response.data });


        const batch = response.data?.batches?.[0];
        if (batch && (batch.error_code || batch.status === 'FAILED')) {
             logger.error('Sinch API reported an error or failure status within the batch response:', { batchData: batch });
             const errorMessage = batch.status_details || `Sinch API Error Code: ${batch.error_code || 'N/A'}, Status: ${batch.status}`;
             throw new Error(errorMessage);
        }

        if (response.data?.error) {
             logger.error('Sinch API top-level error:', { errorData: response.data.error });
             throw new Error(`Sinch API Error: ${response.data.error.message || JSON.stringify(response.data.error)}`);
        }


        return response.data;

    } catch (error) {
        logger.error('Error sending MMS via Sinch service:', {
            message: error.message,
            status: error.response?.status,
            responseData: error.response?.data,
            requestConfig: error.config
        });


        let errorMessage = 'Failed to send MMS via Sinch.';
        if (error.response) {
            const errorDetails = error.response.data?.error?.message
                              || error.response.data?.batches?.[0]?.status_details
                              || JSON.stringify(error.response.data);
            errorMessage = `Sinch API request failed with status ${error.response.status}: ${errorDetails}`;
        } else if (error.request) {
            errorMessage = 'Sinch API request failed: No response received from server after retries.';
        } else {
            errorMessage = `Failed to send MMS: ${error.message}`;
        }
        throw new Error(errorMessage);
    }
};

export const sendBulkMms = async (
    recipients,
    mediaUrl,
    mediaType,
    messageText = '',
    subject = '',
    fallbackText = ''
) => {
    const batchPayload = {
        to: recipients,
        from: SINCH_MMS_NUMBER,
        body: {
            type: "mt_media",
            url: mediaUrl,
            message: messageText || undefined,
            parameters: {
                ...(subject && { "mms_subject": { default: subject } }),
                ...(fallbackText && { "mms_fallback_text": { default: fallbackText } }),
            }
        }
    };

    if (Object.keys(batchPayload.body.parameters).length === 0) {
        delete batchPayload.body.parameters;
    }
    if (!batchPayload.body.message) {
        delete batchPayload.body.message;
    }

    const headers = {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${SINCH_API_TOKEN}`
    };

    logger.info(`Attempting to send bulk MMS to ${recipients.length} recipients`);

    try {
        const response = await sinchAxios.post(SINCH_XMS_ENDPOINT, batchPayload, { headers });
        logger.info('Bulk MMS request successful:', { batchId: response.data.id, recipientCount: recipients.length });
        return response.data;
    } catch (error) {
        logger.error('Error sending bulk MMS:', { message: error.message });
        throw new Error(`Failed to send bulk MMS: ${error.message}`);
    }
};

Sinch API Error Codes:

Error Code HTTP Status Description Solution
400 400 Bad Request Invalid JSON payload or missing required fields Verify payload structure matches API docs
401 401 Unauthorized Invalid or missing API token Check SINCH\_API\_TOKEN in .env
402 402 Payment Required Insufficient account balance Add credits to your Sinch account
403 403 Forbidden Service Plan ID lacks MMS permissions Contact Sinch support to enable MMS
404 404 Not Found Invalid Service Plan ID or endpoint URL Verify SINCH\_SERVICE\_PLAN\_ID and base URL
429 429 Too Many Requests Rate limit exceeded Implement exponential backoff, reduce request rate
500 500 Internal Server Error Sinch server error Retry with exponential backoff
503 503 Service Unavailable Temporary service outage Retry after delay, check Sinch status page

Rate Limit Handling:

Implement custom rate limit detection and backoff:

const handleRateLimit = (error) => {
    if (error.response?.status === 429) {
        const retryAfter = error.response.headers['retry-after'] || 60;
        logger.warn(`Rate limit hit. Retry after ${retryAfter} seconds`);
        return new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
    }
    throw error;
};

Explanation:

  1. Logger: Imports the configured winston logger.
  2. Configuration: Loads Sinch credentials and base URL, logging a fatal error and exiting if any are missing.
  3. Endpoint: Defines the URL for the Sinch XMS /batches endpoint using the format {region}.sms.api.sinch.com/xms/v1/{service\_plan\_id}/batches.
  4. Axios Instance & Retry: Creates a dedicated axios instance with 30-second timeout and configures axios-retry to automatically retry requests on network errors or 5xx responses from Sinch, using exponential backoff. Logs retry attempts.
  5. sendMms Function:
  • Takes recipient, media URL, media type, text, subject, fallback text, and strict validation flag.
  • Payload Construction: Builds the JSON object for the /batches endpoint. Key fields include to, from, body.type (must be "mt\_media"), body.url, body.message, and body.parameters (conditionally added).
  • Strict Validation: Optional strict\_validation parameter enables validation against Sinch MMS channel best practices.
  • Media URL Requirements: The URL must be publicly accessible with valid Content-Type and Content-Length headers.
  • Removes empty parameters or message fields if not used.
  • Headers: Sets Content-Type and Authorization: Bearer.
  • Logging: Uses the logger (logger.info, logger.debug, logger.warn, logger.error). Logs payload at debug level.
  • API Call: Uses the sinchAxios instance (with retry) to make the POST request.
  • Response Handling: Logs the response. Checks the response body for specific error codes or failure statuses even if the HTTP status is 2xx. Throws an error if an issue is found.
  • Error Handling: The catch block logs detailed error information and constructs a specific error message before re-throwing it.

Performance Optimization:

  • Use connection pooling with axios.defaults.httpAgent
  • Implement request queuing for high-volume scenarios
  • Cache media URL validations to reduce redundant checks
  • Consider Redis for distributed rate limiting across multiple servers

Source: Sinch Batches API Reference

How Do You Build the Express API for Sending MMS?

Create the Express endpoint that uses the sinchService.

src/middleware/authMiddleware.js

import dotenv from 'dotenv';
import logger from '../config/logger.js';

dotenv.config();

const INTERNAL_API_KEY = process.env.INTERNAL_API_KEY;

if (!INTERNAL_API_KEY || INTERNAL_API_KEY === 'REPLACE_WITH_A_SECURE_RANDOM_KEY') {
    logger.warn('SECURITY WARNING: INTERNAL_API_KEY is not set or is using the default placeholder. The API endpoint is effectively unprotected. Generate a strong key in .env.');
}

export const apiKeyAuth = (req, res, next) => {

    if (!INTERNAL_API_KEY || INTERNAL_API_KEY === 'REPLACE_WITH_A_SECURE_RANDOM_KEY') {
        logger.warn('Proceeding without API key check because INTERNAL_API_KEY is not properly configured.');
        return next();
    }

    const providedKey = req.headers['x-api-key'];

    if (!providedKey) {
        logger.warn('Unauthorized access attempt: Missing X-API-Key header.');
        return res.status(401).json({ message: 'Unauthorized: Missing API Key' });
    }

    if (providedKey !== INTERNAL_API_KEY) {
         logger.warn(`Forbidden access attempt: Invalid API Key provided. Key: ${providedKey.substring(0, 5)}…`);
         return res.status(403).json({ message: 'Forbidden: Invalid API Key' });
    }

    logger.debug('API Key authentication successful.');
    next();
};

Advanced Authentication Options:

For production systems, consider implementing JWT or OAuth2:

import jwt from 'jsonwebtoken';

export const jwtAuth = (req, res, next) => {
    const token = req.headers.authorization?.split(' ')[1];

    if (!token) {
        return res.status(401).json({ message: 'Missing authentication token' });
    }

    try {
        const decoded = jwt.verify(token, process.env.JWT_SECRET);
        req.user = decoded;
        next();
    } catch (error) {
        return res.status(403).json({ message: 'Invalid or expired token' });
    }
};

Rate Limiting Per API Key:

Implement abuse prevention with per-key rate limiting:

import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';

export const apiKeyRateLimit = rateLimit({
    store: new RedisStore({
        client: redisClient,
        prefix: 'rl:apikey:'
    }),
    windowMs: 15 * 60 * 1000,
    max: 100,
    keyGenerator: (req) => req.headers['x-api-key'] || req.ip,
    message: 'Too many requests from this API key. Try again later.'
});

src/controllers/mmsController.js

import { validationResult } from 'express-validator';
import { sendMms } from '../services/sinchService.js';
import logger from '../config/logger.js';

export const handleSendMms = async (req, res) => {

    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        logger.warn('Validation failed for /send MMS request:', { errors: errors.array() });
        return res.status(400).json({ errors: errors.array() });
    }


    const correlationId = `mms-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
    req.correlationId = correlationId;


    const { to, mediaUrl, mediaType, messageText, subject, fallbackText } = req.body;

    try {

        logger.info(`[${correlationId}] Controller received valid request to send MMS to: ${to}`);
        const sinchResponse = await sendMms(
            to,
            mediaUrl,
            mediaType,
            messageText,
            subject,
            fallbackText
        );


        logger.info(`[${correlationId}] MMS request successfully processed by Sinch service.`, { sinchResponse });
        res.status(202).json({
            message: 'MMS request accepted for processing by Sinch.',
            correlationId,
            details: sinchResponse
        });

    } catch (error) {

        logger.error(`[${correlationId}] Error in handleSendMms controller after calling service: ${error.message}`, { stack: error.stack });


        const statusCode = 500;

        res.status(statusCode).json({
             message: 'Failed to process MMS request.',
             correlationId,
             error: error.message
        });
    }
};

Async Queue Implementation:

For high-volume scenarios, implement message queuing with Bull or BullMQ:

import Queue from 'bull';

const mmsQueue = new Queue('mms-sending', {
    redis: { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT }
});

export const handleSendMmsQueued = async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
    }

    const job = await mmsQueue.add({
        to: req.body.to,
        mediaUrl: req.body.mediaUrl,
        mediaType: req.body.mediaType,
        messageText: req.body.messageText,
        subject: req.body.subject,
        fallbackText: req.body.fallbackText
    }, {
        attempts: 3,
        backoff: { type: 'exponential', delay: 2000 }
    });

    res.status(202).json({
        message: 'MMS queued for processing',
        jobId: job.id
    });
};

mmsQueue.process(async (job) => {
    const { to, mediaUrl, mediaType, messageText, subject, fallbackText } = job.data;
    return await sendMms(to, mediaUrl, mediaType, messageText, subject, fallbackText);
});

src/routes/mmsRoutes.js

import express from 'express';
import { body, validationResult } from 'express-validator';
import { handleSendMms } from '../controllers/mmsController.js';
import { apiKeyAuth } from '../middleware/authMiddleware.js';
import logger from '../config/logger.js';

const router = express.Router();

const SUPPORTED_MEDIA_TYPES = ['image', 'audio', 'video', 'pdf', 'contact', 'calendar', 'text'];

const sendMmsValidationRules = [
    body('to')
        .trim()
        .notEmpty().withMessage('Recipient phone number (to) is required.')
        .isString()
        .matches(/^\+[1-9]\d{1,14}$/).withMessage('Recipient phone number must be in E.164 format (e.g., +12223334444).'),
    body('mediaUrl')
        .trim()
        .notEmpty().withMessage('Media URL (mediaUrl) is required.')
        .isURL({ protocols: ['http', 'https'], require_protocol: true })
        .withMessage('Media URL must be a valid HTTP or HTTPS URL. Ensure the URL returns Content-Type and Content-Length headers.'),
    body('mediaType')
        .trim()
        .notEmpty().withMessage('Media type (mediaType) is required.')
        .toLowerCase()
        .isIn(SUPPORTED_MEDIA_TYPES)
        .withMessage(`Invalid media type. Must be one of: ${SUPPORTED_MEDIA_TYPES.join(', ')}. Supported formats: GIF/JPG/PNG (image), MP3/WAV (audio), 3GP/MP4/MPEG/MPG/AVI/WMV (video).`),
    body('messageText')
        .optional({ checkFalsy: true })
        .isString()
        .isLength({ max: 5000 }).withMessage('Message text cannot exceed 5000 characters.'),
    body('subject')
        .optional({ checkFalsy: true })
        .isString()
        .isLength({ max: 80 }).withMessage('Subject cannot exceed 80 characters (40 recommended).'),
    body('fallbackText')
        .optional({ checkFalsy: true })
        .isString()
        .isLength({ max: 160 }).withMessage('Fallback text cannot exceed 160 characters (approx. SMS limit).'),
    body('strictValidation')
        .optional()
        .isBoolean().withMessage('strictValidation must be a boolean value.')
];

const logValidationErrors = (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        logger.debug('Express-validator validation errors:', { errors: errors.array() });
    }
    next();
};

router.post(
    '/send',
    apiKeyAuth,
    sendMmsValidationRules,

    handleSendMms
);

router.get('/status/:batchId', apiKeyAuth, async (req, res) => {
    const { batchId } = req.params;

    try {
        const statusEndpoint = `${process.env.SINCH_API_BASE_URL}/xms/v1/${process.env.SINCH_SERVICE_PLAN_ID}/batches/${batchId}`;
        const response = await axios.get(statusEndpoint, {
            headers: { 'Authorization': `Bearer ${process.env.SINCH_API_TOKEN}` }
        });

        res.json({
            batchId,
            status: response.data.status,
            created: response.data.created_at,
            modified: response.data.modified_at,
            details: response.data
        });
    } catch (error) {
        logger.error(`Failed to fetch batch status for ${batchId}:`, error.message);
        res.status(500).json({ message: 'Failed to fetch batch status', error: error.message });
    }
});

export default router;

Media URL Validation Utility:

Add preemptive validation to catch hosting issues:

import axios from 'axios';

export const validateMediaUrl = async (url) => {
    try {
        const response = await axios.head(url, { timeout: 5000 });

        const contentType = response.headers['content-type'];
        const contentLength = response.headers['content-length'];

        if (!contentType) {
            throw new Error('Media URL does not return Content-Type header');
        }

        if (!contentLength) {
            throw new Error('Media URL does not return Content-Length header');
        }

        const sizeMB = parseInt(contentLength) / (1024 * 1024);
        if (sizeMB > 1) {
            throw new Error(`Media file too large: ${sizeMB.toFixed(2)} MB (max 1 MB)`);
        }

        return { valid: true, contentType, sizeMB };
    } catch (error) {
        return { valid: false, error: error.message };
    }
};

body('mediaUrl').custom(async (value) => {
    const validation = await validateMediaUrl(value);
    if (!validation.valid) {
        throw new Error(validation.error);
    }
    return true;
});

Test Media URLs with cURL:

curl -I https://your-cdn.com/image.jpg

Explanation:

  1. Auth Middleware (authMiddleware.js): Checks for X-API-Key header against .env. Logs warnings if the key is missing or invalid.
  2. Controller (mmsController.js):
  • Uses validationResult to check for errors from express-validator. Returns 400 if invalid.
  • Generates correlation ID for distributed tracing.
  • Extracts validated data.
  • Calls sendMms service function within a try…catch block.
  • Logs actions and outcomes.
  • Returns 202 Accepted on success, including the service response.
  • Catches errors from the service, logs them, and returns 500 Internal Server Error.
  1. Router (mmsRoutes.js):
  • Defines strict validation rules for all input fields using express-validator.
  • Defines the POST /api/mms/send route.
  • Applies middleware sequentially: apiKeyAuth, validation rules, then the controller.
  • Includes GET /status/:batchId endpoint to check delivery status.

How Do You Integrate the Sinch MMS API?

Focus on the specific Sinch integration points.

  1. Credentials:
  • Set SINCH_SERVICE_PLAN_ID, SINCH_API_TOKEN, SINCH_MMS_NUMBER, SINCH_API_BASE_URL in your .env file (locally) and as secure environment variables in your deployment environment. Never commit .env or hardcode credentials.
  • Refer to the Prerequisites section for details on finding these values.
  1. API Endpoint:
  • Use the Sinch XMS API endpoint: ${SINCH\_API\_BASE\_URL}/xms/v1/${SINCH\_SERVICE\_PLAN\_ID}/batches.
  • Regional endpoints: us.sms.api.sinch.com (United States) or eu.sms.api.sinch.com (European Union). Choose based on your location and data protection requirements.
  • Refer to the official Sinch XMS API documentation for the latest specifications.

Source: Sinch SMS API Reference

  1. Authentication:
  • Sinch API uses Bearer Token authentication.
  • Set the Authorization: Bearer ${SINCH\_API\_TOKEN} header in sinchService.js.
  1. Payload:
  • Use the mt\_media type within the /batches structure in your JSON payload. Set the type field to mt_media for MMS messages.
  • To include text with your media, populate the message field of the body object.
  • Use the optional strict\_validation field to enable message validation against Sinch MMS channel best practices.
  • Verify parameter names against the official Sinch XMS API documentation when adding more options.

Source: Sinch MMS Support Documentation

  1. Media URL Requirements:
  • Make the mediaUrl publicly accessible via HTTP GET.
  • Ensure the hosting server returns a Content-Length header. Check using curl -I <your\_media\_url>.
  • Ensure the hosting server returns a correct Content-Type header (e.g., image/jpeg, video/mp4, audio/mp3).
  • File size recommendations:
  • Keep files under 1 MB (most provider limit)
  • Without transcoding: under 500 KB to 740 KB maximum
  • With Sinch transcoding: under 1 MB
  • Base64 encoding overhead: Multiply file size by 1.37 and add 814 bytes for headers to estimate final MMS size.

Source: Sinch MMS Best Practices

  1. Fallback Mechanism:
  • Use the parameters.mms\_fallback\_text field to control the SMS text if MMS fails.
  • Check Sinch documentation for parameters like disable\_fallback\_sms\_link or disable\_fallback\_sms if needed.

Fallback Failure Scenarios:

Understand when and how MMS falls back to SMS:

Scenario Fallback Behavior Testing Method
Non-MMS device Sends SMS with fallback text Test with feature phone
Carrier doesn't support MMS Sends SMS with fallback text Test with MVNO carriers
Media URL unreachable Sends SMS with fallback text Use invalid URL in test
File size exceeds limit May fail silently or send SMS Send 2 MB file
Invalid Content-Type Fails with error Remove Content-Type header

Testing Fallback:

const testFallback = async () => {

    await sendMms('+12223334444', 'https://cdn.example.com/image.jpg', 'image',
        'Check out this image!', '', 'Visit example.com to view');


    await sendMms('+12223334444', 'https://invalid-url-404.example.com/image.jpg', 'image',
        'Check out this image!', '', 'Visit example.com to view');
};
  1. MMS Enablement (US Region):
  • Important: In the US region, contact your Sinch account manager to enable MMS functionality. Verify your Service Plan ID has MMS capabilities activated before attempting to send MMS messages.

Source: Sinch Batches API Documentation

How Do You Handle Errors and Implement Retries for MMS?

  • Error Handling: Implement at multiple levels:
  • Validation (400): express-validator in routes
  • Authentication (401/403): apiKeyAuth middleware
  • Service Errors (Sinch API): Handle in sinchService.js catch block with retries via axios-retry
  • Controller Errors (500): Controller's catch block handles service errors
  • Logging:
  • Use winston for structured, leveled logging (src/config/logger.js)
  • Configure different formats for console (dev) and potential files/services (prod)
  • Include timestamps and stack traces
  • Replace console.\* with logger.\*
  • Retry Mechanisms:
  • Configure axios-retry in sinchService.js
  • Retry network issues or 5xx errors from Sinch with exponential backoff

Monitoring and Alerting:

Implement production monitoring with health checks and alerts:

router.get('/health', async (req, res) => {
    const health = {
        uptime: process.uptime(),
        timestamp: Date.now(),
        status: 'OK',
        checks: {}
    };


    try {
        await axios.head(`${process.env.SINCH_API_BASE_URL}`, { timeout: 5000 });
        health.checks.sinch = 'OK';
    } catch (error) {
        health.checks.sinch = 'DEGRADED';
        health.status = 'DEGRADED';
    }


    const requiredEnv = ['SINCH_SERVICE_PLAN_ID', 'SINCH_API_TOKEN', 'SINCH_MMS_NUMBER'];
    health.checks.config = requiredEnv.every(key => process.env[key]) ? 'OK' : 'ERROR';

    const statusCode = health.status === 'OK' ? 200 : 503;
    res.status(statusCode).json(health);
});

Dead Letter Queue for Failed Messages:

Implement DLQ for messages that fail after all retries:

import Queue from 'bull';

const deadLetterQueue = new Queue('mms-dlq', {
    redis: { host: process.env.REDIS_HOST, port: process.env.REDIS_PORT }
});

catch (error) {
    logger.error(`[${correlationId}] MMS send failed after retries`);


    await deadLetterQueue.add({
        originalRequest: { to, mediaUrl, mediaType, messageText },
        error: error.message,
        timestamp: new Date(),
        correlationId
    });

    res.status(500).json({ message: 'Failed to process MMS request', correlationId });
}

deadLetterQueue.process(async (job) => {
    logger.warn('Processing dead letter queue item', job.data);

});

Frequently Asked Questions About Sending MMS with Sinch

What are the file size limits for Sinch MMS messages?

Keep media files under 1 MB – most MMS providers use this limit. Without transcoding, keep video and image files under 500 KB, with a maximum of 740 KB. With Sinch transcoding enabled, files under 1 MB work best. To estimate the final MMS size, multiply your file size by 1.37 and add 814 bytes for Base64 encoding headers that Sinch uses for MMS delivery.

Example: A 500 KB image becomes (500 KB Γ— 1.37) + 814 bytes = approximately 685.8 KB after encoding.

Which media formats does Sinch support for MMS?

Sinch supports Images (GIF, JPG, PNG), Audio (MP3, WAV), Video (3GP, MP4, MPEG, MPG, AVI, WMV), and Text (TEXT/PLAIN). All media files must be served with a valid Content-Type header (e.g., image/jpeg, video/mp4, audio/mp3) and a Content-Length header from publicly accessible URLs.

How do you calculate Base64 encoding overhead for MMS?

Sinch delivers MMS messages in Base64 encoding. Calculate the final size by multiplying your file size by 1.37 and adding 814 bytes for headers. For example, a 500 KB image becomes approximately (500 KB Γ— 1.37) + 814 bytes = 685.8 KB after encoding. This calculation helps ensure your media stays within the 1 MB MMS provider limit.

What is the mt\_media type in Sinch API?

The mt_media type is a required field in the Sinch XMS API /batches endpoint for sending MMS messages. Set body.type to "mt_media" (mobile-terminated media message) to indicate you're sending MMS rather than plain SMS. This type enables the body.url field where you specify the publicly accessible media URL.

How do you enable MMS functionality in the US region?

In the US region, contact your Sinch account manager to enable MMS functionality for your Service Plan ID. Verify in the Sinch dashboard (under "Numbers" β†’ "Your Numbers") that your chosen phone number has MMS sending capabilities enabled before attempting to send MMS messages. This requirement is specific to the US region.

What headers must media URLs return for Sinch MMS?

Media URLs must return two required headers: Content-Type (e.g., image/jpeg, video/mp4, audio/mp3) to specify the media format, and Content-Length to indicate file size. Test your media URL using curl -I <your_media_url> to verify both headers are present before sending MMS messages.

What are the regional endpoint differences for Sinch?

Sinch offers two regional endpoints: us.sms.api.sinch.com (United States) and eu.sms.api.sinch.com (European Union). Choose based on your location and data protection requirements. Configure the SINCH_API_BASE_URL environment variable with the appropriate regional endpoint. The full XMS API endpoint follows the format {region}.sms.api.sinch.com/xms/v1/{service_plan_id}/batches.

How do you implement retry logic for failed MMS sends?

Use the axios-retry plugin configured in sinchService.js to automatically retry failed requests. The implementation retries network errors or 5xx server responses from Sinch up to 3 times with exponential backoff (1s, 2s, 3s delays). The retry mechanism uses axiosRetry.isNetworkOrIdempotentRequestError(error) to determine which errors warrant retries, ensuring transient failures don't cause message delivery to fail.

How much does sending MMS cost compared to SMS?

MMS messages typically cost 3–10Γ— more than SMS, depending on your Sinch plan and destination country. US domestic MMS costs approximately $0.02–$0.04 per message, while SMS costs $0.005–$0.01. International rates vary significantly by country. Check your Sinch dashboard under "Billing" β†’ "Pricing" for exact rates, and consider implementing cost tracking in your application to monitor spending.

How do you track MMS delivery status and receipts?

Enable delivery reports by setting delivery_report: "FULL" in your batch payload and providing a callback_url where Sinch sends status updates. Implement a webhook endpoint to receive delivery notifications with statuses: Delivered, Failed, Expired, or Rejected. Store batch IDs returned from Sinch API responses and use the /batches/{batchId} GET endpoint to poll message status programmatically.

Can you test MMS sending in sandbox or development mode?

Sinch doesn't offer a dedicated sandbox mode for MMS. For development testing, provision a low-cost phone number and send test messages to your own devices. Keep file sizes minimal during testing to reduce costs. Implement feature flags to disable actual MMS sending in local development, returning mock success responses instead. Use environment variables to toggle between production and test modes.

Use the Sinch MMS JSON API with Express.js and Node.js to create an API endpoint that accepts requests for sending multimedia messages. This involves setting up a Node.js project, installing required dependencies like Axios and Express, and implementing a service to interact with the Sinch API.

The Sinch MMS JSON API, specifically the XMS API endpoint, allows developers to send multimedia messages programmatically. It handles requests containing recipient details, message content, and media URLs for sending MMS messages via the Sinch platform.

Sinch requires a publicly accessible media URL so its servers can directly fetch the media content (images, audio, video) to include in the MMS message. Ensure your media hosting server includes the Content-Length header for Sinch to process correctly.

Use the Sinch MMS API when you need to send rich media content like images, audio, or video along with your messages. SMS is limited to plain text, making MMS essential for marketing campaigns, notifications with visuals, or other communication needing more than text.

The article doesn't explicitly address international MMS. However, it does mention E.164 number formatting for the "SINCH\_MMS\_NUMBER" environment variable, implying international number support might be available. Consult the official Sinch documentation for details on supported countries and regions for sending international MMS messages.

Initialize a Node.js project using npm init, then install necessary packages such as express, axios, dotenv, and winston. Configure environment variables like SINCH\_SERVICE\_PLAN\_ID and SINCH\_API\_TOKEN in a .env file to securely store your Sinch credentials.

Axios, a promise-based HTTP client, is used to make POST requests to the Sinch XMS API. It simplifies sending the MMS payload to Sinch and handles the API responses.

The example code provides error handling at multiple levels. Input validation using express-validator catches bad requests, authentication middleware handles unauthorized access, and a try-catch block in the Sinch service manages errors during API calls. Winston logs error details for debugging.

Winston provides structured logging with different levels (debug, info, warn, error), facilitating efficient debugging and monitoring. It's configured to log timestamps, stack traces, and specific error data for better insight into API interactions and potential issues.

You need a Sinch account with an active MMS API subscription, a provisioned phone number (Short Code, Toll-Free, or 10DLC) enabled for MMS, Sinch Service Plan ID and API Token, and a publicly accessible URL for your media file. Node.js and npm must also be installed.

Secure your integration by storing Sinch credentials as environment variables, never committing your .env file, and using a strong, random API key for authentication (INTERNAL\_API\_KEY) to protect your Express API endpoint. Use HTTPS and consider rate limiting with express-rate-limit and security headers with Helmet.

The article recommends creating separate directories for routes, controllers, services, middleware, and configuration files. Place your main server logic in server.js. This structure promotes code organization and makes future updates or extensions easier.

The article doesn't specify Sinch's rate limits. Refer to the official Sinch documentation for details on rate limits, as exceeding them can lead to throttled or rejected requests. Consider implementing your own rate limiting using express-rate-limit.

The code uses axios-retry to automatically retry failed Axios requests to the Sinch API. It retries on network errors or 5xx server errors with exponential backoff (1s, 2s, 3s delays between retries), increasing resilience to temporary issues.

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