Home / .NET integration guide
.NET integration guide
Call the LatLongAPI from .NET with a JSON POST request, server-side license configuration, and HTTP error handling.
Configure your application
Use the .NET 10 SDK. Create a console application, then replace Program.cs with the example below. The example uses built-in HttpClient and JSON support, with no additional packages.
dotnet new console --name LatLongExample --framework net10.0
cd LatLongExampleIn a Bash or compatible shell, set LATLONG_API_BASE_URL to your assigned HTTPS origin and LATLONG_API_LICENSE to your license. For testing, use the test origin below with a test license. Replace YOUR_TEST_LICENSE with the license issued for that environment. Keep the license outside source control and browser code.
export LATLONG_API_BASE_URL="https://v1-dev.latlongapi.com"
export LATLONG_API_LICENSE="YOUR_TEST_LICENSE"Request an address
using System.Net.Http.Json;
using System.Text.Json.Serialization;
internal static class Program
{
private static async Task Main()
{
string baseUrl = Environment.GetEnvironmentVariable("LATLONG_API_BASE_URL")
?? throw new InvalidOperationException("Set LATLONG_API_BASE_URL");
string license = Environment.GetEnvironmentVariable("LATLONG_API_LICENSE")
?? throw new InvalidOperationException("Set LATLONG_API_LICENSE");
using var client = new HttpClient
{
BaseAddress = new Uri(baseUrl.TrimEnd('/') + "/"),
Timeout = TimeSpan.FromSeconds(10)
};
var request = new ReverseRequest(51.5007, -0.1246, license);
using var response = await client.PostAsJsonAsync("api/v1/reverse", request);
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadFromJsonAsync<ReverseResponse>()
?? throw new InvalidOperationException("The response body was empty");
Console.WriteLine(data.Result.ShortAddress);
}
}
internal sealed record ReverseRequest(
[property: JsonPropertyName("lat")] double Latitude,
[property: JsonPropertyName("long")] double Longitude,
[property: JsonPropertyName("api")] string License);
internal sealed record ReverseResult(
[property: JsonPropertyName("short_address")] string ShortAddress);
internal sealed record ReverseResponse(
[property: JsonPropertyName("result")] ReverseResult Result);
Run the example
dotnet runUse the result
The example prints result.short_address. Read result.long_address for a longer description. Check nullable city and country fields before displaying them.
Handle failures in your service
Set a timeout, catch request errors, and record the status and request identifier without logging the license. Retry temporary failures with bounded backoff. A 401 requires a valid license; a 422 requires correcting the input.