You can quickly see how the Number Lookup API works by looking up a number using the API.
Before you can get started, you need the following already set up:
- Set all Number Lookup API configuration settings.
- .NET 7.0 or later SDK and .NET Runtime and a familiarity with how to create an app.
Create a new project folder and open a command prompt. Execute the following command to create a new .NET console application:
dotnet new consoleThis creates a new console application and project.
In your project folder, open the Program.cs file and paste the provided "Program.cs" code into the file, replacing all the existing content.
This tutorial uses basic authentication for testing purposes. We recommend using a signed request for authentication in a production environment. You can follow the steps in this guide, but use the code samples from here to use request signing authentication instead.
// Use this code to look up a phone number using the Number Lookup API.
using System.Text;
using System.Text.Json;
using static System.Net.Mime.MediaTypeNames;
class Program
{
private const string _number = "<REPLACE_WITH_NUMBER>";
/*
The app key and secret from one of your Number Lookup Apps, found here https://dashboard.sinch.com/number-lookup/apps
*/
private const string _key = "<REPLACE_WITH_APP_KEY>";
private const string _secret = "<REPLACE_WITH_APP_SECRET>";
private const string _sinchUrl = "https://number-lookup.api.sinch.com/v1/lookups";
static async Task Main(string[] args)
{
await LookupNumberWithBasicAuth();
}
private static async Task LookupNumberWithBasicAuth()
{
using (var _client = new HttpClient())
{
var requestBody = LookupNumberRequestBody();
var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_key}:{_secret}"));
var requestMessage = new HttpRequestMessage(HttpMethod.Post, _sinchUrl);
requestMessage.Headers.TryAddWithoutValidation("authorization", "basic " + base64String);
requestMessage.Content = requestBody;
var request = await _client.SendAsync(requestMessage);
Console.WriteLine(await request.Content.ReadAsStringAsync());
request.EnsureSuccessStatusCode();
}
}
public static StringContent LookupNumberRequestBody()
{
var myData = new
{
number = _number
};
return new StringContent(
JsonSerializer.Serialize(myData),
Encoding.UTF8,
Application.Json
);
}
}Before you can run the code, you need to update some more values so you can connect to your Sinch account. Update the following parameters with your own values:
| Parameter | Your value |
|---|---|
_key | The key found on your Sinch dashboard. |
_secret | The secret found on your Sinch dashboard. |
_number | The phone number that you want to look up in E.164 format. |
Save the file.
Before executing your code, you must first compile your application. Execute the following command:
dotnet buildNow you can execute the code and look up your phone number. Run the following command:
dotnet runYou should receive a response in your console with details about the phone number you specified.
If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.