Search for a virtual number using .NET Core

Use this guide to create a .NET Core application for use with the Numbers API and search for an available Sinch virtual number.

Note:

Before you can get started, you need the following already set up:

Steps:
  1. Set up your .NET Core application.
  2. Search for an available virtual number for SMS, Voice or both.

Set up your .NET Core application

  1. Create a new folder where you want your app project. Then, open a terminal or command prompt to that location.
  2. Create a new .NET Core console app with the following command:
    Copy
    Copied
    dotnet new console
  3. Add the Newtonsoft.Json nuget package.
    Copy
    Copied
    dotnet add package Newtonsoft.Json

Modify your application

  1. Open the Program.cs file in your project folder. Replace all of the code with the "Search for a virtual number" code.

    Search for a virtual number

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Text;
    
    public class Program
    {
      public static async Task Main(string[] args)
      {
        using (var client = new HttpClient())
        {
          var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"YOUR_username:YOUR_password"));
          client.DefaultRequestHeaders.Add("Authorization", "Basic "+ base64String);
          var ProjectId = "YOUR_ProjectId";
          var request = await client.GetAsync("https://numbers.api.sinch.com/v1/projects/" + ProjectId + "/availableNumbers?regionCode=US&type=LOCAL");
          var response = await request.Content.ReadAsStringAsync();
    
          Console.WriteLine(response);
        }
      }
    }
    
    
  2. Save the file.

Search for an available virtual number

Fill in your parameters

  1. Replace the following values in the Program.cs file:
ParameterYour value
YOUR_usernameyour client_id or key_id found in the Sinch Sinch Customer Dashboard.
YOUR_passwordYour client_secret or key_secret. This is generated upon new key creation. Generate a new key if you have lost your secret key.
YOUR_ProjectIDThe project ID found in the Customer Dashboard.
regionCodeThe two letter abbreviation of the country for which you'd like a number. ISO 3166–1 alpha–2.
typeThe type of number you would like to rent. Available options are: MOBILE, LOCAL, or TOLL_FREE. Note that 10DLC numbers should be set to LOCAL.
  1. Save the file.

Build and run your project

  1. Before executing your code, you must first compile your application. Execute the following command:
Copy
Copied
dotnet build
  1. Now you can execute the code. Run the following command:
Copy
Copied
dotnet run

Response

These steps should return a JSON list of numbers available to rent.

Copy
Copied
{
  "availableNumbers": [
    {
      "phoneNumber": "+12089087284",
      "regionCode": "US",
      "type": "LOCAL",
      "capability": ["SMS"],
      "setupPrice": {
        "currencyCode": "USD",
        "amount": "0.00"
      },
      "monthlyPrice": {
        "currencyCode": "USD",
        "amount": "2.00"
      },
      "paymentIntervalMonths": 1
    }
  ]
}

Next steps

Copy the phoneNumber you would like to use and rent your virtual number using the Numbers API.

Additional resources

Was this page helpful?

Search for a virtual number

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;

public class Program
{
  public static async Task Main(string[] args)
  {
    using (var client = new HttpClient())
    {
      var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"YOUR_username:YOUR_password"));
      client.DefaultRequestHeaders.Add("Authorization", "Basic "+ base64String);
      var ProjectId = "YOUR_ProjectId";
      var request = await client.GetAsync("https://numbers.api.sinch.com/v1/projects/" + ProjectId + "/availableNumbers?regionCode=US&type=LOCAL");
      var response = await request.Content.ReadAsStringAsync();

      Console.WriteLine(response);
    }
  }
}