Twilio API Send SMS Flutter Example

In this article, we are going to integrate twilio api to send sms from twilio console in flutter application. You can also check out flutter firebase related articles.

In this flutter example, first we create an twilio account to access sms api feature to send a message in flutter application. Using twilio platform, we can also make and receive calls same as sms. After creating twilio account, we get AccountSID and Auth Token which are required in our flutter code to send a message.

Using twilio, we can also send push notifications to users who are registered. We can also verify user phone number with twilio api. Twilio API also supports call tracking if you want. Initially we get $15.00 amount in our twilio account after signup for trial purpose. We can also get trial number from our twilio dashboard to test send and receive sms functionality.

 

Create Twilio Account

 
twilio api send sms
 
 
twilio api send sms
 
 

Add required package in pubspec.yaml file

To integrate twilio api with our flutter application to send sms, we must add twilio_flutter package in our pubspec.yaml file.

twilio_flutter: ^0.0.9
 
 

Initialize Twilio SMS Client

We can initialize TwilioFlutter class with some required information such as Account SID, Auth Token and our twilio number which we get from our twilio dashboard.

TwilioFlutter myTwilioFlutter;

@override
void initState() {
myTwilioFlutter = TwilioFlutter(
    accountSid: ‘***************************************’,
    authToken: ‘*************************************’,
    twilioNumber: ‘******************’
);
super.initState();
}

 
 

Twilio API Send SMS

We can send a message to verified phone number by using sendSMS method of TwilioFlutter class. We can also customize message text by using messageBody parameter of sendSMS method.

void sendTwilioSMS() async {
    myTwilioFlutter.sendSMS(toNumber: ‘*************’, messageBody: ‘Flutter Twilio SMS Test’);
}
 
 

Twilio API Get SMS

We can get sms details from twilio sdk with two ways. First we can get all sms details using getSmsList method of twilio sdk. Second if we have message id of particular sms then we can also get details of that sms separately.

void getTwilioSMS() async {
    var getSMSData = await myTwilioFlutter.getSmsList();
    print(getSMSData);
    await myTwilioFlutter.getSMS(‘***************************’);
}
 
 

Twilio SMS App UI

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(‘Flutter Twilio SMS Screen’)),
body: Center(
child: ElevatedButton(
    style: ElevatedButton.styleFrom(primary: Colors.redAccent, padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15)),
    child:Text(‘Send SMS through Twilio’, style: TextStyle( color: Colors.white, fontSize: 25)),
    onPressed: sendTwilioSMS,
),
),
);
}
 
 
 

2 thoughts on “Twilio API Send SMS Flutter Example

Leave a Reply