Gabby10
Gabby10

Reputation: 171

How to connect caller and reciever using Twilio API in Flutter

I just setup Twilio API in my flutter project and I've been able to make outbound calls to international phone numbers, but i have a problem and i hope someone can point me in the right direction. When a call is sent from my flutter app and it successfully registers as a call on the recievers's phone, how do i connect my flutter app to engage in a live call with recipient after call is accepted?

Here's the code that performs API call below

Future<http.Response> accessTwilioAPI() async {
    http.Response response = await http.post(
      Uri.https("api.twilio.com",
          "/2010-04-01/Accounts/ACCOUNT_SID_GOES_HERE/Calls/.json"),
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' +
            toAuthCredentials("ACCOUNT_SID_GOES_HERE",
                "AUTH_TOKEN_GOES_HERE")
      },
      body: {
        //body goes here

        "From": "TWILIO_NUMBER",
        "To": "RECIPIENT",
        "Url":
            "TWIML_BIN_URL",
        "duration": "30",
      },
    );

    print(response.body);
  }

UPDATE So i've been able to establish a two way communication but its not exactly what i want yet. what i did was to place a call first to my phone number (not twilio phone number) then i used the Dial TwiML verb to connect call to another person, it works but the limitation of this approach is that caller would have to recieve a call before it is routed to reciever.

Upvotes: 0

Views: 2000

Answers (1)

philnash
philnash

Reputation: 73029

Twilio developer evangelist here.

You can make calls directly from native mobile applications using the Twilio Voice SDK. There are SDKs available for iOS, Android and JavaScript. The Voice SDK allows you to make and receive calls from within your application and can hook into native UI, like CallKit on iOS.

In terms of Flutter, you can either integrate the platform yourself (this Stack Overflow answer has a description of how to do so in Android) or check out the community built Flutter wrappers for the native SDKs. There's flutter_twilio_voice or flutter_voice.


In response to the code you have so far, this is a bad idea for a few reasons. Firstly, as you've discovered, the calls are made outside of the context of the application. Secondly, making API requests from within the application means that your application will contain or expose your API credentials. If a malicious user were to decompile the app, they could extract your credentials and abuse your account.

Upvotes: 1

Related Questions