Skip to content Skip to sidebar Skip to footer

Send Thousands Of Sms With Twilio

I would like to send ~50,000 SMS with Twilio, and I was just wondering if my requests are going to crash if I loop through a phone number array of this size. The fact is that Twili

Solution 1:

Twilio developer evangelist here.

API Limits

First up, a quick note on our limits. With a single number, Twilio has a limit of sending one message per second. You can increase that by adding more numbers, so 10 numbers will be able to send 10 messages per second. A short code can send 100 messages per second..

We also recommend that you don't send more than 200 messages on any one long code per day.

Either way I recommend using a messaging service to send messages like this.

Finally, you are also limited to 100 concurrent API requests. It's good to see other answers here talking about making requests sequentially rather than asynchronously as that will eat up the memory on your server as well as start to find requests are turned down by Twilio.

Passthrough API

We now have an API that allows you to send more than one message with a single API call. It's known as the passthrough API, as it lets you pass many numbers through to the Notify service. You need to turn your numbers into "bindings" and send them via a Notify service, which also uses a messaging service for number pooling.

The code looks a bit like this:

constTwilio = require('twilio');    
const client = newTwilio(accountSid, authToken);
const service = client.notify.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

service.notifications
  .create({
    toBinding: [
      JSON.stringify({
        binding_type: 'sms',
        address: '+15555555555',
      }),
      JSON.stringify({
        binding_type: 'facebook-messenger',
        address: '123456789123',
      }),
    ],
    body: 'Hello Bob',
  })
  .then(notification => {
    console.log(notification);
  })
  .catch(error => {
    console.log(error);
  })

The only drawbacks in your situation is that every message needs to be the same and the request needs to be less than 1 megabyte in size. We've found that typically means about 10,000 numbers, so you might need to break up your list into 5 API calls.

Let me know if that helps at all.

Solution 2:

There are two factors here.

  • You need to consider Twilio Api usage Limits.
  • Performing 50.000 parallel http requests (actually your code do it) is not a good idea: you will have memory problems.

Twilio sms limits change based on source and destination.

You have two solution:

Perform 50k http requests sequentially

    phoneNumbers.forEach(asyncfunction(phNb){
      try {
        let m = await client.messages.create({
          body: msgCt,
          to: phNb,
          from: ourPhone
        })
        console.log(a)
      } catch(e) {
        console.log(e)
      } 
    })

Perform 50k http requests concurrently with concurrency level

This is quite easy to do with the awesome bluebird sugar functions. Anyway, the twilio package uses native promise. You can use async module with mapLimit method for this purpose

Solution 3:

You send your requests asynchronous due to non-blocking forEach body calls, I guess it's fastest for the Client. But the question is: does Twilio allow such a load from a single source? it needs to be tested... And if no, you should build some kind of requests queue, e.g. promise based, something like

function sendSync(index = 0) {
  if(index === phoneNumbers.length) {
    return;
  }
  client.messages.create({
      body: msgCt,
      to: phoneNumbers[index],
      from: ourPhone
  })
  .then(function(msg) {
      console.log(msg.sid);
      sendSync(index + 1);
  })
  .catch(function(err) {
      console.log(err);
  });
}

sendSync();

Or if you like async/await –

asyncfunctionsendSync() {
  for (let phNb of phoneNumbers) {
    try {
      let msg = await client.messages.create({
        body: msgCt,
        to: phNb,
        from: ourPhone
      });
      console.log(msg);
    } catch(err) {
      console.log(err);
    } 
  })
}

sendSync();

Post a Comment for "Send Thousands Of Sms With Twilio"