Start a new topic

JS | Reseller API: how to get a list of all call-tracking phone numbers from your customers

Here is a short HTML script that uses JavaScript & jQuery to query the reseller API, read the JSON structure and connect to each customer account to retrieve the list of associated call-tracking phone numbers. The output is a HTML table that you can copy-paste in a spreadsheet editor, such as Excel.


<html>
<head>
    <title>Call-tracking numbers</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<script type="text/javascript">

var apiKey = "";
var token = "";
var url = "https://api.agendize.com/api/2.0/resellers/accounts?apikey=" + apiKey + "&token=" + token;
var xhr = new XMLHttpRequest();
var resultApi = [];
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    var subresult = [];
    if (xhr.readyState === 4 && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        for (i = 0; i < json.items.length; i++) {
            if (json.items[i].deleted == undefined) {
                subresult = [];
                subresult.push(json.items[i].id);
                subresult.push(json.items[i].email);
                subresult.push();
                subresult.push("");
                subresult.push("");
                subresult.push("");
                subresult.push("");
                subresult.push("");
                resultApi.push(subresult);
                
                $.ajax({
                    type: 'GET',
                    url: "https://api.agendize.com/api/2.0/calls/calltrackings?apikey=" + apiKey + "&token=" + json.items[i].ssoToken,
                    dataType: 'json',
                    success: function(data) {
                  var items = [];
                $.each(data.items, function(i,item){
                        subresult = [];
                        subresult.push("");
                        subresult.push("");
                        subresult.push("");
                        subresult.push(item.trackingPhoneNumber);
                        subresult.push(item.phoneNumber);
                        subresult.push(item.name);
                        subresult.push("");
                        subresult.push("");
                        resultApi.push(subresult);
                  });
                },
                    data: {},
                    async: false
                });
            }
        }
        
        process(resultApi);
    }
}
xhr.send();

function process(resultApi) {
    document.write("<body><table>");
    document.write('<thead style="font-weight:bold;background-color:#eee;"><td>Account</td><td>Tracking phone number</td><td>Target phone number</td><td>Phone number name</td></thead>');
    document.write("<tbody>");
    odd = false;
    var companyId;
    var lang;
    var phone;
    for (i = 0; i < resultApi.length; i++) {
        document.write("<tr");
        if (odd) {      
                document.write(' style="background-color:#eee;"');
        }
        odd = !odd;
        document.write(">");
        if (resultApi[i][0].length > 0) {
            companyId = resultApi[i][0];
        }
        document.write("<td>" + resultApi[i][1] + "</td>");
        document.write("<td>" + resultApi[i][3] + "</td>");
        document.write("<td>" + resultApi[i][4] + "</td>");
        document.write("<td>" + resultApi[i][5] + "</td>");
        document.write("</tr>");
    }
    document.write("</tbody>");
    document.write("</table></body></html>");
}
</script>


Login or Signup to post a comment