CNPJws
English
English
  • Overview
  • Data Models
    • Countries
    • States
    • Cities
    • Legal Natures
    • Company Sizes
    • Economic Activities
    • Corporate Qualifications
    • Partners
    • Simples Nacional and MEI
    • Establishments
    • State Registrations
    • Suframa
    • Tax Regimes
    • Registration Statuses
    • Error Messages
  • Changelog
  • API Reference
    • API - Commercial
      • Consulting CNPJ in the Commercial API
      • Consulting by CNPJ Root
      • Company Search
      • Consulting Monthly Request Consumption
      • Validating Suframa Registration
    • API - Public
      • Consulting CNPJ in the Public API
      • Validating Suframa Registration in the Public API
      • Limitations
  • BLOG
    • What is CNPJ
    • Consult CNPJ with Java
    • Consult CNPJ for Free with Excel
    • Consult CNPJ with PHP
    • Trigger to Consult CNPJ in Protheus
    • Keep Customer and Supplier Records Updated
    • Consult CNPJ with Javascript and Node.JS
    • Consult CNPJ with ADVPL (Protheus)
    • CNPJ.ws Project
Powered by GitBook
On this page
  • Prerequisites
  • Setting Up the Environment
  • Developing the Query Class
  • Explanation:
  • Testing the Query
  • Conclusion

Was this helpful?

  1. BLOG

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.

PreviousWhat is CNPJNextConsult CNPJ for Free with Excel

Last updated 4 months ago

Was this helpful?

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 API to access detailed information about companies registered with Receita Federal.

provides a public API for queries where you can make up to 3 queries per minute. .

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.

See you soon!

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

CNPJ.ws
CNPJ.ws
See the documentation
CNPJ.ws