Before you start
Use .NET 8 or later and set COMPANYAPI_KEY in your environment. In a long-running application use IHttpClientFactory or a properly reused HttpClient.
Make the request
This console example explicitly handles a missing company, applies a timeout and propagates other HTTP failures.
// .NET 8+. Server-side only.
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
var key = Environment.GetEnvironmentVariable("COMPANYAPI_KEY")
?? throw new InvalidOperationException("Set COMPANYAPI_KEY");
using var client = new HttpClient {
BaseAddress = new Uri("https://api.companyapi.be/"),
Timeout = TimeSpan.FromSeconds(10)
};
client.DefaultRequestHeaders.Add("X-Api-Key", key);
using var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(10));
using var response = await client.GetAsync("companies/0801523965", cancellation.Token);
if (response.StatusCode == HttpStatusCode.NotFound) {
Console.WriteLine("Company not found");
return;
}
response.EnsureSuccessStatusCode();
var company = await response.Content.ReadFromJsonAsync<JsonElement>(cancellation.Token);
Console.WriteLine(company.GetProperty("enterpriseNumber").GetString());Map only what you need
Use nullable properties for optional fields. Preserve code objects and their available descriptions. Add bounded retry logic where your workload needs it.
