> For the complete documentation index, see [llms.txt](https://docs.cnpj.ws/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cnpj.ws/en/blog/consultar-cnpj-java.md).

# Consult CNPJ with 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](https://www.cnpj.ws) API to access detailed information about companies registered with Receita Federal.

[CNPJ.ws](https://www.cnpj.ws) provides a public API for queries where you can make up to 3 queries per minute. [See the documentation](https://docs.cnpj.ws/referencia-de-api/api-publica/consultando-cnpj).

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:

```xml
<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:

```java
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](https://www.cnpj.ws/).

See you soon!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cnpj.ws/en/blog/consultar-cnpj-java.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
