// .NET 10 console/backend sample. Set JIMO_MAPS_BASE_URL and JIMO_MAPS_KEY. // Keep server keys on a trusted backend, never in distributed app binaries. using System.Net.Http.Json; using System.Text.Json; var baseUrl = Environment.GetEnvironmentVariable("JIMO_MAPS_BASE_URL") ?? throw new InvalidOperationException("Set JIMO_MAPS_BASE_URL"); var apiKey = Environment.GetEnvironmentVariable("JIMO_MAPS_KEY") ?? throw new InvalidOperationException("Set JIMO_MAPS_KEY to a server key"); using var client = new HttpClient { BaseAddress = new Uri(baseUrl.TrimEnd('/') + "/"), Timeout = TimeSpan.FromSeconds(30) }; client.DefaultRequestHeaders.Add("X-JimoMaps-Key", apiKey); using var accessResponse = await client.GetAsync("api/map/access"); accessResponse.EnsureSuccessStatusCode(); var access = await accessResponse.Content.ReadFromJsonAsync(); Console.WriteLine(access); if (args.Contains("--navigation")) { if (!access.GetProperty("navigation").GetBoolean()) throw new InvalidOperationException("Navigation is not enabled for this key"); using var routeResponse = await client.GetAsync("api/map/route?start=36.8219,-1.2921&end=36.814,-1.265"); routeResponse.EnsureSuccessStatusCode(); var result = await routeResponse.Content.ReadFromJsonAsync(); if (result.GetProperty("code").GetString() != "Ok" || result.GetProperty("routes").GetArrayLength() == 0) throw new InvalidOperationException("No driving route"); var route = result.GetProperty("routes")[0]; Console.WriteLine($"{route.GetProperty("distance").GetDouble()/1000:F1} km; {route.GetProperty("duration").GetDouble()/60:F0} min"); }