Bootstrap a Vonage ASP.NET Core App to Get Number Insights
Published on October 16, 2024

Introduction

In this blog post, I'll show you how to use the Number Insight API within the .NET ecosystem. We'll build an ASP.NET Core Web API application that allows users to specify a phone number and retrieve details like carrier, country, and more. We’ll output information about a phone number from basic to advanced so you can choose the right one for your project. 

The complete source code for this tutorial is available on GitHub.

Prerequisites

To follow along with this tutorial, you’ll need the following:

DT API Account

To complete this tutorial, you will need a DT API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the DT API Dashboard.

Project Setup

Let’s set up the project. I’ll break it down into just 4 steps: 

  1. Create an ASP.NET Core Web API Project

  2. Install the Vonage Library

  3. Add your API credentials

  4. Register the Vonage Client

Create ASP.NET Core Web API Project

Use your preferred method, whether it's the .NET CLI (dotnet new webapi) or Visual Studio's project creation wizard, to create a new ASP.NET Core Web API project.

Install Vonage library via NuGet Package

Open your project's Package Manager Console or use the .NET CLI and run the following command:

Install-Package Vonage

Add API Key and API Secret in the appsettings.json Configuration File

Navigate to your appsettings.json file and add the following, replacing the placeholders with your actual Vonage API credentials:      

"vonage": {

    "Api.Key": "your_vonage_api_key",

    "Api.Secret": "your_vonage_api_secret"

  }

}

Register the Vonage Client to the IServiceCollection in the Program.cs

In your Program.cs file, add the following code to thebuilder.Servicessection to register the Vonage client for dependency injection: builder.Services.AddVonageClientScoped(builder.Configuration); 

Create a New Controller 

You can create or rename the one that comes by default with the project template and require the numberInsightClient as a parameter in the constructor to get a reference from the DI ServiceCollection.

public NumberInsightsController(INumberInsightClient numberInsightClient) {

 _numberInsightClient = numberInsightClient; }

Add The Endpoints

The Number Insight API offers three levels of detail. You can check a feature comparison table.

Add the three endpoints that perform the Basic, Standard, and Advanced Lookups. For example:

var result = await _numberInsightClient.GetNumberInsightBasicAsync(new Vonage.NumberInsights.BasicNumberInsightRequest() { Number = phoneNumber });

Run the Application and Test

Start your ASP.NET Core project from Visual Studio to run the app or use the .NET CLI (dotnet run).

You can test the endpoints via the automatically created OpenAPI/Swagger page. You can usually access the Swagger UI by navigating to /swagger.

This image shows three endpoints: basic, standard, and advanced. The user inputs a phone number in the `phoneNumber` field, and the response information is shown.Number Insights Swagger Example

Conclusion

You have reached the end of this tutorial! We built a phone number lookup app using Vonage’s Number Insight API within the ASP.NET Core framework. 

This app allows users to perform basic, standard, and advanced lookups, providing valuable information about a phone number. 

Go ahead and expand on what you've learned today. Let us know what you build on our Vonage Community Slack, or message us on X!

Amanda CavallaroDeveloper Advocate

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.