Expertise developer

Node.js SMS API Morocco: Full Integration Tutorial (TypeScript)

Integrate SMS delivery in Morocco using Node.js, TypeScript, and EnvoiSMS REST API. Covers Axios/Fetch, +212 validation, and Express webhook listeners.

Node.js SMS API Morocco: Full Integration Tutorial (TypeScript)

1. Installation & Environment Configuration

Install the official package or use standard fetch / axios. Store your EnvoiSMS API key (env_live_...) in a secure .env configuration file.

2. Sending Transactional SMS with Fetch / Axios

Dispatching an outbound SMS is straightforward:

typescript
const res = await fetch('https://api.envoisms.ma/v1/messages', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.ENVOISMS_API_KEY}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    recipient: '+212612345678',
    message: 'Your verification code is 492019.',
    sender_id: 'MYBRAND'
  })
});
const data = await res.json();

3. Validating Moroccan Phone Numbers (+212)

Use libraries like libphonenumber-js to ensure the recipient number belongs to Moroccan mobile ranges (prefixes 6 and 7 under ANRT allocations) before triggering the API request.

4. Setting Up Express Webhook Endpoint for DLR Statuses

Configure an Express route to process async delivery receipts. Verify the cryptographic signature using crypto.createHmac('sha256', secret).update(rawBody).digest('hex').

5. Production Scaling: Concurrency and Rate Limits

EnvoiSMS supports high throughput with queue workers (BullMQ, Celery). Monitor balance quotas via GET /v1/billing/quota to avoid service interruptions.

💡 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).

Q.Is there an official npm package for EnvoiSMS?
Yes, install `envoisms` via npm or use native Fetch API with our typed request payloads.
Q.How does EnvoiSMS handle failed carrier deliveries?
Failed deliveries trigger status updates (`failed`) with specific error codes, and unused balance is refunded based on delivery status policies.

Suggested Articles

Laravel SMS Notification Channel for Morocco: Complete Guide
developer

Laravel SMS Notification Channel for Morocco: Complete Guide

Inbound Webhooks: Connect TikTok & Meta Lead Ads Directly to WhatsApp for Instant Conversion
developer

Inbound Webhooks: Connect TikTok & Meta Lead Ads Directly to WhatsApp for Instant Conversion

WhatsApp Flows: Native In-Chat Forms for COD Order Verification in Morocco
developer

WhatsApp Flows: Native In-Chat Forms for COD Order Verification in Morocco