Cloud Code Functions
Integrations
Twilio
9 min
using cloud functions and twilio api to send text message introduction this guide explains how you can use the twilio rest api to send sms after completing this step by step tutorial, you can use your cloud code function to send sms to your device prerequisites to complete this tutorial, you will need an app created at back4app follow the create new app tutorial to learn how to create an app at back4app back4app command line configured with the project follow the setting up cloud code tutorial to learn how to set up cloud code for a project account created in twilio let’s get started! below are some steps you need to follow when writing a function to send sms to a user and phone number to learn how to create or access an account in twilio, check the links given below create a new account log in to your account 1 activate your phone number after logging in or creating a new account, you will be redirected to your project there, on the left, you need to click on the #phone numbers #phone numbers next, tap on the last link ‘getting started’, and then click on the button ‘get your first twilio phone number’ same as shown below after that you will receive your first phone number for your twilio account if you can’t find your phone number, go to #phone numbers and manage numbers 2 get account sid and auth token to find your account sid account sid and auth token auth token , log in to your account, go to your dashboard and click on settings all the important information about your project will be available in that section; as shown in the image below now, you can copy your sid and authentication token for the cloud code 3 install module from twilio after configuring the environment for the command line interface in your computer, create a file called package json, and inside this file, you need to install the twilio module, like 1 { 2 "dependencies" { 3 "twilio" " " 4 } 5 } 4 implement cloud code parse server 3 x 1 parse cloud define("sendsms", async(request) => { 2 3 // requiring the values to send 4 let 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 //require the twilio module and create a rest client 12 let client = require('twilio')(accountsid, authtoken); 13 14 return await client messages 15 create({ 16 body getmessage, // any number twilio can deliver to 17 from getphonefrom, // a number you bought from twilio and can use for outbound communication 18 to getphoneto // body of the sms message 19 }); 20 }); parse server 2 x 1 parse cloud define("sendsms",function(request,response){ 2 3 // requiring the values to send 4 var 5 getmessage = request params message, 6 getphoneto = '+target test phone number', 7 getphonefrom = "+your first phone number", 8 accountsid = 'accountsid', 9 authtoken = 'authtoken'; 10 11 12 //require the twilio module and create a rest client 13 var client = require('twilio')(accountsid, authtoken); 14 15 client messages 16 create({ 17 body getmessage, // any number twilio can deliver to 18 from getphonefrom, // a number you bought from twilio and can use for outbound communication 19 to getphoneto // body of the sms message 20 }) 21 then(function(results) { 22 response success(results sid); 23 }) 24 catch(function(error) { 25 response error(error); 26 }) 27 }); 5 test the function “sendsms” you can also test the function in client sdks, but for now, we will use the rest api command to send it curl x post \\ h "x parse application id app id" \\ h "x parse rest api key rest key" \\ h "content type application/json" \\ d '{ "message" "now, i can send sms from cloud code using twilio", "phone" "+target test phone number" }' \\ https //parseapi back4app com/functions/sendsms and the result will be something like as this 6 it’s done! with the guide described above, you’ll be able to use twilio with a cloud code function in back4app and send sms to your customers! in case you need any help or a function/link doesn’t work, please contact our team via chat!