npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

medusa-twilio-sms

v2.0.2

Published

Twilio SMS notification provider for Medusa v2

Readme

Medusa Twilio SMS Plugin (v2 Compatible)

A production-ready Twilio SMS notification provider for Medusa v2 that enables sending SMS notifications to customers.

Features

  • Send SMS notifications using Twilio's messaging API
  • Support for MMS (multimedia messages) via mediaUrl
  • Full integration with Medusa v2's notification module architecture
  • Type-safe implementation using TypeScript
  • Support for both direct phone numbers and messaging service SIDs
  • Automatic retry logic with configurable attempts
  • Comprehensive error handling and validation
  • E.164 phone number format validation
  • Production-grade logging
  • Media URL validation for MMS

Prerequisites

  • Medusa v2 backend
  • Twilio account with:
    • Account SID
    • Auth Token
    • Either a phone number for sending SMS OR a Messaging Service SID

Installation

yarn add medusa-twilio-sms

Configuration

Add the following environment variables to your .env file:

Option 1: Using a Phone Number (recommended)

TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_FROM_NUMBER=+1234567890

Option 2: Using a Messaging Service SID

TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_MESSAGE_SERVICE_SID=MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In your medusa-config.ts:

import { defineConfig } from '@medusajs/framework/utils'

export default defineConfig({
  projectConfig: {
    // ... other config
  },
  modules: [
    {
      resolve: "@medusajs/medusa/notification",
      options: {
        providers: [
          {
            resolve: "medusa-twilio-sms",
            id: "twilio-sms",
            options: {
              channels: ["sms"],
              account_sid: process.env.TWILIO_ACCOUNT_SID,
              auth_token: process.env.TWILIO_AUTH_TOKEN,
              // Use either from_number OR messaging_service_sid
              from_number: process.env.TWILIO_FROM_NUMBER,
              // messaging_service_sid: process.env.TWILIO_MESSAGE_SERVICE_SID,
            }
          }
        ]
      }
    }
  ]
})

Usage

In a Workflow

import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk";
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk";
import { Modules } from "@medusajs/framework/utils";

const sendSmsStep = createStep(
  "send-sms-notification",
  async ({ phoneNumber, message }, { container }) => {
    const notificationModuleService = container.resolve(Modules.NOTIFICATION);

    await notificationModuleService.createNotifications({
      to: phoneNumber,
      channel: "sms",
      content: {
        text: message
      }
    });

    return new StepResponse({ success: true });
  }
);

export const sendSmsWorkflow = createWorkflow(
  "send-sms-workflow",
  ({ phoneNumber, message }) => {
    sendSmsStep({ phoneNumber, message });
    return new WorkflowResponse({ success: true });
  }
);

With Media (MMS)

await notificationModuleService.createNotifications({
  to: "+1234567890",
  channel: "sms",
  content: {
    text: "Check out this image!"
  },
  data: {
    mediaUrl: ["https://example.com/image.jpg"]
  }
});

Options

| Option | Type | Required | Description | |--------|------|----------|-------------| | account_sid | string | Yes | Your Twilio Account SID | | auth_token | string | Yes | Your Twilio Auth Token | | from_number | string | No* | The Twilio phone number to send from | | messaging_service_sid | string | No* | Your Twilio Messaging Service SID | | max_retries | number | No | Maximum retry attempts for failed sends (default: 3) | | retry_delay | number | No | Base delay in ms between retries (default: 1000) |

*Note: You must provide either from_number OR messaging_service_sid, but not both.

Production Considerations

Phone Number Validation

The plugin validates all phone numbers to ensure they're in E.164 format (+1234567890). This helps prevent errors and ensures compatibility with Twilio's API.

Error Handling

The plugin distinguishes between different types of errors:

  • Validation errors (20000-20999): These are not retried as they indicate invalid input
  • Network/temporary errors: These are automatically retried with exponential backoff

Retry Logic

Failed SMS sends are automatically retried with configurable attempts and delays:

{
  max_retries: 3,        // Default: 3 attempts
  retry_delay: 1000      // Default: 1 second base delay
}

Logging

The plugin provides comprehensive logging at different levels:

  • INFO: Successful operations
  • DEBUG: Detailed operation flow
  • WARN: Non-critical issues (e.g., invalid media URLs)
  • ERROR: Failed operations with details

Security Best Practices

  1. Environment Variables: Always store sensitive credentials in environment variables
  2. Token Security: Never commit auth tokens to version control
  3. Phone Number Privacy: Be mindful of logging phone numbers in production
  4. Rate Limiting: Consider implementing rate limiting at the application level

Testing

Run the test suite:

npm test
# or
npm run test:coverage

Monitoring

For production environments, monitor:

  • SMS delivery rates
  • Error rates and types
  • Retry patterns
  • Response times

Troubleshooting

Common Issues

  1. "The requested resource was not found"

    • Verify your Account SID is correct
    • Ensure your account has SMS capabilities enabled
  2. "Invalid phone number"

    • Ensure phone numbers are in E.164 format (+1234567890)
    • Verify the recipient's country is supported
  3. "Authentication error"

    • Double-check your auth token
    • Ensure no extra spaces in credentials

License

MIT