C# API Connection

I was just want to share my code example. Because when I start work with Companies House API I couldn’t find any information on c#. Any need any help or have any questions please let us know.

 public static string MainURL = "https://api.company-information.service.gov.uk";
    private string Data = "";
    public static readonly HttpClient HttpClient = new HttpClient(); 

public async Task GetSingleCompanyDetails(string CompanyNumber)
var request = new HttpRequestMessage(HttpMethod.Get, MainURL + “/company/” + CompanyNumber);
var APIKEY = System.Text.Encoding.UTF8.GetBytes(“YOURAPIKEY:”);
string val = System.Convert.ToBase64String(APIKEY);
request.Headers.Authorization = new AuthenticationHeaderValue(“basic”, val);
HttpResponseMessage response = await HttpClient.SendAsync(request);
if (response.StatusCode != HttpStatusCode.OK)
Data = response.ToString();
Data = $"{await response.Content.ReadAsStringAsync()}";

        CompanyDetails CompanyDetails = JsonSerializer.Deserialize<CompanyDetails>(Data); //  <CompanyDetails>(Data);
        return CompanyDetails;
1 Like

There is already a C# library that is pretty easy to use called “CompaniesHouse.NET”, this can be found at: https://github.com/kevbite/CompaniesHouse.NET or NuGet Gallery | CompaniesHouse 7.11.3 if anyone wants to look into it.

There is also this one, by my company. All open source and free to use:

There’s example usage on the Github: