Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find cost of call at disconnect? *Twilio API*

Tags:

twilio

Is there a way to get the amount spent on a twilio call at the end of that call using the twilio API?

like image 227
Oliver Leopold Avatar asked Jan 24 '26 09:01

Oliver Leopold


1 Answers

Yes.

You can execute a script at the end of the call. You can find the cost of that call in this script.

  • If you are using REST API, then specify a StatusCallback URL and that script will be executed at the end of the call. (Refer here for more details)
  • If you are using Dial TwiML to make calls, specify an action parameter. (Refer here for more details)

Now in this url, you will get the Sid of the dialed call as a parameter. Using this call sid, you can make a REST API request to get the details of that particular call.

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
$sid = "{{ACC SID}}"; 
$token = "{{ auth_token }}"; 
$client = new Services_Twilio($sid, $token);

$call_sid = $_REQUEST['DialCallSid']; //if you are using dial
//*************** OR *************
$call_sid = $_REQUEST['CallSid']; //if you are using REST API

//Get the details of the call using rest API from the call_sid
$call = $client->account->calls->get($call_sid);

$price = $call->Price; //get the cost of the call
$price_usit = $call->PriceUnit; // get the currency in cost is charged

?>

Please note that twilio might take some time to populate these values. So if you are not getting the correct values, please try to put a sleep before requesting call resource.

like image 157
Akhil Balakrishnan Avatar answered Jan 27 '26 01:01

Akhil Balakrishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!