Look up CNPJ in C# / .NET

This guide shows how to look up a CNPJ with the CNPJAPI REST API in C# (.NET), using HttpClient. The response comes as JSON, with fields in PascalCase (RazaoSocial, SituacaoCadastral, ...) - which fits nicely with .NET's naming convention.

Prerequisites

Simple lookup

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;

const string cnpj = "00776574000156"; // only the 14 digits, without punctuation
const string apiKey = "cnpj_sua_chave";

using var http = new HttpClient();
http.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", apiKey);

using var resposta = await http.GetAsync($"https://api.cnpjapi.com.br/{cnpj}");
resposta.EnsureSuccessStatusCode();

await using var stream = await resposta.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);
var raiz = doc.RootElement;

Console.WriteLine(raiz.GetProperty("RazaoSocial").GetString());
Console.WriteLine(raiz.GetProperty("SituacaoCadastral").GetProperty("Descricao").GetString());

Handling errors and rate limits

When you exceed the per-minute limit or the monthly quota, the API responds 429 with a Retry-After header (seconds). Treat it as recoverable, without EnsureSuccessStatusCode:

using var resposta = await http.GetAsync($"https://api.cnpjapi.com.br/{cnpj}");

if ((int)resposta.StatusCode == 429)
{
    var espera = resposta.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(60);
    await Task.Delay(espera);
    // retry...
}
else if (resposta.StatusCode == System.Net.HttpStatusCode.NotFound)
{
    Console.WriteLine("CNPJ não encontrado na base pública");
}
else if (resposta.IsSuccessStatusCode)
{
    var json = await resposta.Content.ReadAsStringAsync();
    using var doc = JsonDocument.Parse(json);
    Console.WriteLine(doc.RootElement.GetProperty("RazaoSocial").GetString());
}

State Registration (IE, premium)

On a plan that includes State Registration (IE), look up a company's IE from the official SEFAZ source. Pass uf for a single state (1 credit) or omit it for the nationwide sweep (3 credits):

using var resposta = await http.GetAsync(
    $"https://api.cnpjapi.com.br/consulta/ie/{cnpj}?uf=SP");
resposta.EnsureSuccessStatusCode();

await using var stream = await resposta.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);

foreach (var ie in doc.RootElement.GetProperty("resultados").EnumerateArray())
{
    Console.WriteLine($"{ie.GetProperty("uf").GetString()} " +
        $"{ie.GetProperty("ie").GetString()} " +
        $"{ie.GetProperty("situacao").GetString()}");
}

Full contract (fields, coverage, credits) at Look up the State Registration.

Next steps

Create your free account at https://app.cnpjapi.com.br and make your first lookup in minutes.