Get Available Twilio Number List Using C#

In this article, I will know you that how to get an available Twilio number list to buy using c#. So let’s start it. First, create one .Net framework MVC application with the appropriate name. Here I use the name “TwilioNumbersAPI”. After this install the Twilio library with the latest version. In this article

Create Your Twilio Account and get the API Credentials from here.

Twilio Cedentials

Add key of the Account SID and Auth Token in your web config file.

Webconfig Keys

In this article, I’m going to use total 3 different Twilio APIs to get a number list

  1. AvailablePhoneNumberLocal resource
  2. AvailablePhoneNumberTollFree resource
  3. AvailablePhoneNumberMobile resource

1) AvailablePhoneNumber Local resource

This AvailablePhoneNumberLocal lets us search for locally available phone numbers which are available for purchase. Here I have created the below function to get locally available phone numbers.

public JsonResult GetLocalResourceNumberList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> LocalNumbers = new List<Numbersre>();
                var local = LocalResource.Read(pathCountryCode: "US");
                foreach (var number in local)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = string.IsNullOrEmpty(number.Region) ? "" : number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    LocalNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = LocalNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }

In the above code, I’m using the country code to get the number list. You can add other filters also like areaCode, contains, smsEnabled, mmsEnabled, voiceEnabled, limit etc.

For Example:

var local = LocalResource.Read(areaCode: 256, pathCountryCode: "US", limit: 10);

The above API call returns the list of those numbers which has area code 256 and country is Us and we define a limit of 10 so it will return only 10 numbers.

2) AvailablePhoneNumber TollFree resource

This AvailablePhoneNumberTollFree lets us search for toll-free available phone numbers which are available for purchase. Here I have created the below function to get toll-free available phone numbers.

public JsonResult GetTollFreeNumberList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> TollFreeNumbers = new List<Numbersre>();
                var tollFree = TollFreeResource.Read(pathCountryCode: "US");
                foreach (var number in tollFree)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = string.IsNullOrEmpty(number.Region) ? "" : number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    TollFreeNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = TollFreeNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }

Same as the above API you can pass other filters.

3) AvailablePhoneNumber Mobile resource

This AvailablePhoneNumberMobile lets us search for mobile phone numbers which are available for purchase. Here I have created the below function to get toll-free available phone numbers.

public JsonResult GetMobileResourceList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> mobileNumbers = new List<Numbersre>();
                var mobile = MobileResource.Read(pathCountryCode: "US");
                foreach (var number in mobile)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = string.IsNullOrEmpty(number.Region) ? "" : number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    mobileNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = mobileNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }

Here Is My Full Controller Code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Web.Mvc;
using Twilio;
using Twilio.Rest.Api.V2010.Account.AvailablePhoneNumberCountry;

namespace TwilioNumbersAPI.Controllers
{
    public class HomeController : Controller
    {
        string TwilioSID = ConfigurationManager.AppSettings["TwilioSID"].ToString();
        string TwilioTOKEN = ConfigurationManager.AppSettings["TwilioTOKEN"].ToString();
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetLocalResourceNumberList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> LocalNumbers = new List<Numbersre>();
                var local = LocalResource.Read(areaCode: 256, pathCountryCode: "US", limit: 10);
                foreach (var number in local)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    LocalNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = LocalNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult GetMobileResourceList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> mobileNumbers = new List<Numbersre>();
                var mobile = MobileResource.Read(pathCountryCode: "US");
                foreach (var number in mobile)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    mobileNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = mobileNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult GetTollFreeNumberList()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                TwilioClient.Init(TwilioSID, TwilioTOKEN);

                List<Numbersre> TollFreeNumbers = new List<Numbersre>();
                var tollFree = TollFreeResource.Read(pathCountryCode: "GB");
                foreach (var number in tollFree)
                {
                    Numbersre Num = new Numbersre();
                    Num.FriendlyName = number.FriendlyName.ToString();
                    Num.Region = number.Region;
                    Num.IsoCountry = number.IsoCountry;
                    TollFreeNumbers.Add(Num);
                }
                return Json(new { IsSuccess = true, Numbers = TollFreeNumbers }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
            }
        }
        public class Numbersre
        {
            public string FriendlyName { get; set; }
            public string Region { get; set; }
            public string IsoCountry { get; set; }
        }
    }
}

Full View

@{
    ViewBag.Title = "Twilio Numbers";
}
<div class="row">
    <br /><br />
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-center">
        <small style="color:brown;">Click on the below button to get Number List</small>
        <br /><br />
        <button class="btn btn-primary" id="localresource">Local resource</button>
        <button class="btn btn-primary" id="tollFreeResource">TollFree resource</button>
        <button class="btn btn-primary" id="mobileResource">Mobile resource</button>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-center">
        <br /><br /><br /><br />
        <div id="NumberGrid">

        </div>
    </div>
</div>


@section scripts{
    <script src="~/Scripts/Custom/TwilioNumbers.js"></script>
}

Script

I have created one js file named TwilioNumbers.js.

$("#localresource").click(function () {
    AjaxCall("GetLocalResourceNumberList");
});
$("#tollFreeResource").click(function () {
    AjaxCall("GetTollFreeNumberList");
});
$("#mobileResource").click(function () {
    AjaxCall("GetMobileResourceList");
});
function AjaxCall(ControllerMethod) {
    $(".loaderWrap").show();
    $.ajax({
        method: "POST",
        url: "/Home/" + ControllerMethod,
        success: function (res) {
            if (res.IsSuccess == true) {
                var data = res.Numbers;
                if (data.length > 0) {
                    LoadGrid(data);
                }
                else {
                    alert("No number found");
                }
            }
            else {
                alert("Something went wrong");
            }
            $(".loaderWrap").hide();
        },
        error: function () {
            $(".loaderWrap").hide();
            alert("Something went wrong");
        }
    });
}
function LoadGrid(data) {
    $("#NumberGrid").empty();
    var html = ``;
    html += `<table class="table table-hover table-striped table-borderless">`;
    html += `<tr>`;
    html += `<th class="text-center">Phone Number</th>`;
    html += `<th class="text-center">Region</th>`;
    html += `<th class="text-center">Country</th>`;
    html += `</tr>`;
    for (var i = 0; i < data.length; i++) {
        html += `<tr>`;
        html += `<td>${data[i].FriendlyName}</td>`;
        html += `<td>${data[i].Region}</td>`;
        html += `<td>${data[i].IsoCountry}</td>`;
        html += `</tr>`;
    }
    html += `</table>`;
    $("#NumberGrid").html(html);
}

Final Output

Output

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Footer Logo

Subscribe

Select Categories