Search code examples
twilio

Choose between Phone number and AlphaNumeric Sender Id when send SMS in Messaging Service


I added a phone number and an Alphanumeric Sender Id to a Messaging Service. And my app has a logic to choose between sending by a Phone number or AlphaSenderId.

Could you please provide me API in Messaging Service to have the ability to choose between the Phone number and AlphaSenderId when sending an SMS?


Solution

  • The messaging service will pick the sender automatically based on the country you send the message to. From the doc:

    If you add an Alpha Sender to your Twilio Messaging Service, Twilio will select the Alphanumeric Sender ID automatically when you send a message to a supported country, unless you also have a short code number in that same country.

    I.e. you cannot influence this manually when you send a message via such an API call:

    client.messages
      .create({
         body: 'Revenge of the Sith was clearly the best of the prequel trilogy.',
         from: 'MG9752274e9e519418a7406176694466fa',
         to: '+441632960675'
       })
      .then(message => console.log(message.sid));
    

    But you could do something like this:

    const useMS = false;
    client.messages
      .create({
         body: 'Revenge of the Sith was clearly the best of the prequel trilogy.',
         from: useMS ? 'MG9752274e9e519418a7406176694466fa' :  '+441632960670',
         to: '+441632960675'
       })
      .then(message => console.log(message.sid));
    

    or use the messagingServiceSid property in combination with from:

    client.messages
      .create({
        from: useAlpha ? 'AlphaSenderID' :  '+440000000',
        messagingServiceSid: "MGxxxxxx",
        body: 'Revenge of the Sith was clearly the best of the prequel trilogy.',
        to: '+441632960675'
       })
      .then(message => console.log(message.sid));