Expertise technical

Migrate from Twilio to a Moroccan SMS provider: step by step guide

Migrate twilio Moroccan SMS provider: Technical guide with code examples for developers in Morocco.

Migrate from Twilio to a Moroccan SMS provider: step by step guide
Table of Contents
  1. Many Moroccan startups (Yassir, Glovo, Chari) start their adventure using Twilio to send their [transactional SMS (OTP)](/fr/guides/otp-authentication-marocco/).
  2. Preparation: Mapping of functions
  3. Development (Adapting the payload API)
  4. Progressive Deployment (Canary Release / Double Run)
  5. Managing the Fallback (High Availability)

Many Moroccan startups (Yassir, Glovo, Chari) start their adventure using Twilio to send their [transactional SMS (OTP)](/fr/guides/otp-authentication-marocco/).

However, when a Moroccan company scales and reaches critical volumes (over 50,000 SMS per month), Twilio poses three major problems: 1. The price (USD invoice): The Twilio SMS to Morocco (+212) is structurally very expensive (often over 0.80 MAD) and exposes you to the risk of exchange.2. Deliverability: international SMS traffic to Morocco is severely filtered by the operators' anti-spam firewalls when the sender ID is not registered locally.

Here’s the engineering plan (Runbook) to migrate your streams to a sovereign local API (such as SendSMS) without any service disruption.

Preparation: Mapping of functions

Before touching the code, make sure that the new provider supports your critical streams.

  • Sender ID: Twilio allows you to use an Alphanumeric Sender ID without much verification. In Morocco, you will need to officially register your Sender ID with the local provider before migrating, otherwise the operators will block your SMS (regulatory timeline for the ANRT and operators: 15 to 30 days, documents: RC and ICE).
  • Delivery Reports (DLR) Webhooks: If your application listens to Twilio's 'Delivered' or 'Failed' status to update a database, you will need to map the new JSON format returned by the local API.
  • Concatenation (Long SMS): Make sure that the target API automatically handles the concatenation of SMS > 160 characters as Twilio does.

Development (Adapting the payload API)

The advantage of SMS is that the HTTP protocol is universal.The migration of a Twilio code to a Standard REST API (EnvoiSMS) is often a matter of a few lines.

Example of Twilio (Node.js)```javascript
const client = require('twilio')(accountSid, authToken);
client.messages.create({
body: 'Votre code OTP est 1234',
from: 'MA_MARQUE',
to: '+2126XXXXXXXX'
})
.then(message => console.log(message.sid));

bash

**Example SendSMS (Node.js - Fetch):**```javascript
const response = await fetch('https://api.envoisms.ma/v1/sms/send', {
    method: 'POST',
    headers: {
        'Authorization': `Bearer ${API_KEY}`,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        recipient: '+2126XXXXXXXX',
        sender_id: 'MA_MARQUE',
        message: 'Votre code OTP est 1234'
    })
});
const data = await response.json();

Progressive Deployment (Canary Release / Double Run)

The worst mistake of a CTO is to make a “Bascule Big Bang” (turn off Twilio and turn on the new provider instantly in production).

Use a Progressive Rollout approach: 1. Day 1 (10% of traffic): Configure your backend to route 10% of SMS requests (e.g. only customers whose ID ends in 1) to the Moroccan API. Leave 90% on Twilio. 2. Day 2 (Observation): Analyze logs. Is the OTP conversion rate the same? Do Delivery Reports (DLRs) go up correctly from Pending' to Delivered'? 3. Day 3 (50%): Switch half of traffic. 4. Day 7 (100%): Twilio is disabled and kept as a single backup solution (Fallback).

Managing the Fallback (High Availability)

Even if a local platform such as EnvoiSMS offers local routes with automatic failover, the telecom ecosystem can suffer breakdowns (fiber cutting, IAM maintenance).

Keep your Twilio account open (without subscription, in Pay As You Go). Encode a logic of Retry Automatic: If the request to the SendSMS API fails with a code 500 HTTP or a timeout > 5 seconds, your system catches the error (catch) and returns the same SMS via the Twilio API. This gives you a backup route in case of an incident, while keeping most of your traffic on the local provider.

Why choose EnvoiSMS for your business?

Carrier Delivery

Local routes to IAM, Orange and Inwi, with automatic failover between routes and delivery status sent by webhook.

Cost Optimization

WhatsApp Business API from 0.65 MAD per message with optimal ROI.

Sovereign Data (CNDP)

Full compliance with Moroccan personal data protection regulations (CNDP).

How do I track routing on the IAM, Inwi and Orange networks?
Our local routes to Maroc Telecom (IAM), Orange and inwi, with automatic failover between routes, return a carrier delivery status for every message, by webhook. Delivery time depends on the recipient's carrier and network.
How does the platform manage the format of Moroccan numbers?
EnvoiSMS automatically normalizes the numbers entered (06..., 07..., 00212...) to the E.164 standard (+212) to avoid any delivery failure.

Suggested Articles

SMS Formatting in Morocco: Line Breaks, GSM-7 vs UCS-2 Encoding and Cost Optimization
technical

SMS Formatting in Morocco: Line Breaks, GSM-7 vs UCS-2 Encoding and Cost Optimization

WhatsApp Business Formatting in Morocco: Enriched Text, Emojis and Interactive Buttons
technical

WhatsApp Business Formatting in Morocco: Enriched Text, Emojis and Interactive Buttons

One-Tap Autofill & 1-Click WhatsApp OTP Authentication in Morocco
technical

One-Tap Autofill & 1-Click WhatsApp OTP Authentication in Morocco