Skip to content

Getting Started with Phone Numbers SDK

This SDK helps you to check validity of any phone number in the world. It uses Google's libphonenumber as backing logic

In order to get started with Hubtel.PhoneNumbers, do the following activities

Install-Package Hubtel.PhoneNumbers.Sdk

The code snippet is a command that is used to install Hubtel.PhoneNumbers package in a .NET project.

C#
    Install-Package Hubtel.PhoneNumbers

Add to IServiceCollection in Program.cs or Startup.cs

services.AddHubtelPhoneNumberSdk();

  • This line is adding the Hubtel Phone Number SDK to the application's services. This is a method provided by the Hubtel.PhoneNumbers package that sets up any necessary services for the SDK to function, such as HTTP clients or other dependencies.

  • Once added, the SDK provides libraries and tools that make it easier for developers to perform operations related to phone numbers, such as validation, formatting, and parsing.

c#
      services.AddHubtelPhoneNumberSdk();

Inject and consume service methods

private readonly IPhoneNumberParser _phoneNumberParser;

  • This line declares a private, read-only field named _phoneNumberParser of type IPhoneNumberParser. This is presumably an interface provided by the Hubtel Phone Number SDK for parsing phone numbers.

if (_phoneNumberParser.IsPhoneNumberValid("<phone_number>","GH",out var correctNumber)) {...}

  • This line calls the IsPhoneNumberValid method of the _phoneNumberParser object. This method checks if a given phone number is valid for a specific country, in this case, Ghana ("GH").

The IsPhoneNumberValid method takes three arguments:

  • The phone number to validate. In this case, a placeholder <phone_number> is used.

  • The country code for the country in which the phone number is being validated. In this case, "GH" is used, which is the ISO country code for Ghana.

  • An out parameter named correctNumber that will hold the correctly formatted phone number if the input phone number is valid. The if statement will execute the code inside its block if the phone number is valid.

c#
    private readonly IPhoneNumberParser _phoneNumberParser;    

    if (_phoneNumberParser.IsPhoneNumberValid("<phone_number>","GH",out var correctNumber))
            {
                
            }