Look up CNPJ in Java

This guide shows how to look up a CNPJ with the CNPJAPI REST API in Java, using the standard library's HttpClient (java.net.http, Java 11+). The response comes as JSON, with fields in PascalCase (RazaoSocial, SituacaoCadastral, ...).

Prerequisites

  • Java 17+ (LTS). java.net.http.HttpClient has been native since Java 11; the arrow switch used in error handling requires Java 14+.
  • A JSON library to deserialize the response (Jackson, Gson, ...).
  • An API key from CNPJAPI. Create your account at https://app.cnpjapi.com.br and generate the key (see Authentication).

Simple lookup

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

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

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://api.cnpjapi.com.br/" + cnpj))
        .header("Authorization", "Bearer " + apiKey)
        .GET()
        .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

if (response.statusCode() == 200) {
    // Deserialize response.body() with your JSON library (e.g., Jackson):
    // Empresa empresa = new ObjectMapper().readValue(response.body(), Empresa.class);
    System.out.println(response.body());
}

The client.send(...) and Thread.sleep(...) calls throw checked exceptions (IOException, InterruptedException): run them inside a method with throws or wrap them in try/catch.

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:

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

switch (response.statusCode()) {
    case 429 -> {
        int espera = response.headers().firstValue("Retry-After")
                .map(Integer::parseInt).orElse(60);
        Thread.sleep(espera * 1000L);
        // retry...
    }
    case 404 -> System.out.println("CNPJ não encontrado na base pública");
    case 200 -> { /* process response.body() */ }
    default -> throw new RuntimeException("Falha na consulta: HTTP " + response.statusCode());
}

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):

HttpRequest ieRequest = HttpRequest.newBuilder()
        .uri(URI.create("https://api.cnpjapi.com.br/consulta/ie/" + cnpj + "?uf=SP"))
        .header("Authorization", "Bearer " + apiKey)
        .GET()
        .build();

HttpResponse<String> ieResponse = client.send(ieRequest, HttpResponse.BodyHandlers.ofString());

if (ieResponse.statusCode() == 200) {
    // Deserialize ieResponse.body() with your JSON library and iterate "resultados":
    // each item has uf, ie, indicador, situacao, tipo, ...
    System.out.println(ieResponse.body());
}

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.