Is there a way to get the amount spent on a twilio call at the end of that call using the twilio API?
You can execute a script at the end of the call. You can find the cost of that call in this script.
StatusCallback URL and that script will be executed at the end of the call. (Refer here for more details)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With