App Services, Functions, Twilio

Hi, I am trying to write a function to send a SMS using Twilio.
I have been succesful doing this using Third Party Services by selecting the Twilio service. During this process you need to enter your Account SID and Auth Token, no problem. However Third Party Services are to be deprecated in favor of creating HTTP endpoints that use external dependencies in functions.
I am able to add the twilio dependency (named twilio) and have used:

const twilio = context.services.get("twilio");

and the send() function with the to, from and body information, but of cource this does not work.
Question, how do I apply the Account SID and Auth Token information, I can not find the information or an example.

Hi Glenn,

Thanks for posting and welcome to the community forum!

Although third party services is deprecated you can still use Twilio in app services functions.

Please first Install the Twilio package as an external dependency.

You can now import the package in your function and use Twilio as per the example in their documentation.

exports = async function(arg){
 
const accountSid = context.values.get("twilio_accountSID");
const authToken = context.values.get("twilio_authToken");
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'testing alert',
     from: '+000000',
     to: '+000000'
   })
  .then(message => console.log(message.sid));

};

Note that I have used App Services Secrets to safely store the accountSID and auth code and retrieving it in the function via context.values.get().

I’ve tried this code and it worked for me.

Regards
Manny

Hi Manny,
many thanks for the quick and concise response. This works for me; I can now go ahead and learn additional functionality.

1 Like