Consult CNPJ with Java

Learn how to consult company data through the CNPJ using the CNPJ.ws API, with data from Receita Federal, Sintegra, and Suframa using Java.

If you are looking for an efficient and free way to consult CNPJ data directly from your Java application, you have come to the right place! In this guide, we will explore how you can integrate your Java application with the CNPJ.ws API to access detailed information about companies registered with Receita Federal.

CNPJ.ws provides a public API for queries where you can make up to 3 queries per minute. See the documentation.

We will divide this tutorial into sections for easier understanding. Follow along!

Prerequisites

Java: Make sure you have the JDK installed on your machine. IDE: A Java development environment, such as Eclipse or IntelliJ IDEA.

Setting Up the Environment

Create a new Java project in your favorite IDE.

Add the OkHttp library: We will use the OkHttp library to facilitate HTTP requests. Add the dependency to your pom.xml file if you are using Maven:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>

Developing the Query Class

Create a new Java class called ConsultaCNPJ. In this class, we will implement the method to perform the CNPJ query:

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class ConsultaCNPJ {

    private static final String URL_BASE = "https://publica.cnpj.ws/cnpj/";
    //Caso use a API Comercial a URL é diferente, veja a doc:

    public static void main(String[] args) {
        try {
            String cnpj = "27865757000102";
            String jsonResponse = consultaCNPJ(cnpj);
            System.out.println(jsonResponse);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String consultaCNPJ(String cnpj) throws IOException {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(URL_BASE + cnpj)
                .get()
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
}

Explanation:

  • OkHttpClient: We use this class to send HTTP requests.

  • Request: Here we build our request, defining the URL with the CNPJ number we want to query.

  • Response: We store the API response to process it later.

Testing the Query

Run the ConsultaCNPJ class and check the console. You should see the JSON response containing detailed information about the queried CNPJ.

Conclusion

In this guide, you learned how to perform CNPJ queries for free using the CNPJ.WS public API in a Java application. Use this functionality to enrich your Java projects with accurate and up-to-date business data.

To learn more about our plans and payment methods, visit CNPJ.ws.

See you soon!

Last updated

Was this helpful?