This is the second article in this step-by-step series about Vonage's Number Insight API within an ASP.NET web application.
In the first article, we walked through everything we need to know about the Number Insight API, setting up the ASP.NET application and getting ready to explore how to get insights about a phone number.
In this article, we will continue from where we left off in the first article for a deep dive into Number Insight Basic API. We will use the API to retrieve some basic information about a phone number such as its international and national formats and the country where the number is registered and then present it to the user.
Number Insight Basic API is a free synchronous, easy-to-use RESTful web service. For any phone number you can:
Retrieve the international and local format.
Know the country where the number is registered.
Hands-on code
In NumberInsightController.cs, let's start by creating the method that would allow the navigation to the basic insights view.
[HttpGet]
public ActionResult Basic()
{
return View();
}
We will now add that view entitled Basic.cshtml under the NumberInsight views folder.
@using (Html.BeginForm("Basic", "NumberInsight", FormMethod.Post))
{
<br>
<input type="text" name="number" id="number" placeholder="Phone Number">
<input type="submit" value="Get More Information">
}
This view will serve as a placeholder for the user to input a phone number.
In order to get some basic insights about that phone number, we will add another method to 'NumberInsightController.cs' with a string parameter representing the phone number and make a call to Number Insight Basic API.
[HttpPost]
public ActionResult Basic(string number)
{
var results = Client.NumberInsight.RequestBasic(new NumberInsight.NumberInsightRequest
{
Number = number,
});
Session["requestID"] = results.RequestId;
Session["iNumber"] = results.InternationalFormatNumber;
Session["nNumber"] = results.NationalFormatNumber;
Session["status"] = results.StatusMessage;
Session["country"] = results.CountryName;
Session["countryCode"] = results.CountryCode;
return RedirectToAction("BasicResults");
}
Notice that after fetching the insights, we need to show them to the user. We are going to create a BasicResults method in the controller and a BasicResults.cshtml view for that matter.
[HttpGet]
public ActionResult BasicResults()
{
ViewBag.requestID = Session["requestID"];
ViewBag.iNumber = Session["iNumber"];
ViewBag.nNumber = Session["nNumber"];
ViewBag.status = Session["status"];
ViewBag.country = Session["country"];
ViewBag.countryCode = Session["countryCode"];
return View();
}
@{
<h1>Number Insight Basic Results</h1>
<hr style="height:2px;border:none;color:#333;background-color:black">
<h2>Request ID: @ViewBag.requestID</h2>
<h2>Status: @ViewBag.status</h2>
<h2>International Number: @ViewBag.iNumber</h2>
<h2>National Number: @ViewBag.nNumber</h2>
<h2>Country: @ViewBag.country</h2>
<h2>Country Code: @ViewBag.countryCode</h2>
}
Now, let's run the app and make a call to Number Insight Basic API. You can use one of the Vonage numbers provided for testing.
Recap
In this article, we learnt how to use Vonage's Number Insight Basic API to retrieve the international and national formats of a phone number and the country where the number is registered. Then we showed this information on a view within an ASP.NET application. In the next article of the series, we will learn how to get Standard Number Insights within an ASP.NET app. Stay tuned!
Vonage Number Insight getting started guide for ASP.NET
Getting Started with Vonage Number Insight APIs and ASP.NET.
How to Get Basic Number Insights Within an ASP.NET App
Rabeb was a Developer Advocate at Nexmo focusing on cloud communication APIs and helping developers get the best experience possible when building their apps. Other than writing code for a living, Rabeb advocates for bringing more women and minorities into tech, thus her involvement with Women Who Code and different tech communities. She leads the Women Who Code Network in Bristol.