
BrasilAPI-Java Library
Library created to facilitate access to the BrasilAPI API in the Java programming language
I was planning to develop a new project and for that I needed an API that would return street information from the ZIP code. I then remembered a YouTube video by Filipe Deschamps that talked about a system that did exactly what I needed. When searching for this system, whose name is BrasilAPI, I came across the number of people who contribute to it. To improve it, I realized that there were libraries in various programming languages intended to facilitate integration. Then, to my surprise and delight, there wasn't a Java library for integration with the project. Therefore, I had the opportunity to contribute to the project by developing the BrasilAPI-Java library.
Available in the repository: https://github.com/SavioAndres/BrasilAPI-Java, also accessible from the official BrasilAPI repository.
What is BrasilAPI?
BrasilAPI is a project aimed at making public data available through a simplified and fast REST API.
[Image of BrasilAPI] Official website: https://brasilapi.com.br
Repository: https://github.com/BrasilAPI/BrasilAPI
Currently offers the following endpoints:
- Banks (Information about Banks)
- CEP (Information about ZIP Codes)
- CEP V2 (Improved endpoint with query across multiple providers and fallback)
- CNPJ (Information about the National Registry of Legal Entities)
- DDD (Returns the State and Cities containing the area code)
- National Holidays (Returns the date and name of the holiday for a given year)
- FIPE (Information about average vehicle prices)
- IBGE (Information (Regarding States and Municipalities)
- ISBN (Information about books published in Brazil, international book identification system)
- NCM (Common Nomenclature of Mercosur)
- Registro BR (Evaluates a domain in registro.br)
- Taxas (Interest rates and official indices)
The following video by Filipe Deschamps explains how BrasilAPI was conceived:
What does BrasilAPI-Java do?
The library aims to further simplify the connection between the The endpoint is provided by the BrasilAPI system and uses the Java programming language. For example, if you need to obtain information about a company whose CNPJ (Brazilian tax ID) is "06.990.590/0001-23", simply access the endpoint https://brasilapi.com.br/api/cnpj/v1/06990590000123. This will give you the company's data in JSON format. This is where the BrasilAPI-Java library comes in. With it, you simply provide the CNPJ (Brazilian company tax ID) to the static method of the BrasilAPI class and receive the CNPJ object with all attributes populated, like this:
CNPJ cnpj = BrasilAPI.cnpj("06.990.590/0001-23");
String razaoSocial = cnpj.getRazaoSocial();
System.out.println(razaoSocial);
The following video shows how simple it is to use the library; in approximately a minute and a half, I was able to add the library to my project and obtain the street name from the postal code.
Library Resources
The library methods are documented in the code to facilitate programmer understanding. Among the available methods, I need to emphasize those that begin with "set" from the BrasilAPI class, as they are configuration methods to improve the experience when consuming the API.
-
The "BrasilAPI.setEnableLog()" method receives a boolean as a parameter; if true, it activates logging in the console, displaying the entire flow of results obtained from the API and the data processing.
-
The method The
BrasilAPI.setEnableCache()method receives a boolean as a parameter; if true, it enables the cache, reducing the return time of repeated queries performed during the last 10 minutes.
The BrasilAPI.setCacheTime() method receives a Long as a parameter to define the cache lifetime in milliseconds, which by default is 10 minutes, equivalent to 600000 milliseconds. If the BrasilAPI.setEnableCache() method is not set to true, the BrasilAPI.setCacheTime() method enables it.
The BrasilAPI.setCacheTimeMinutes() method works similarly to the BrasilAPI.setCacheTime() method; the difference is in the unit of measurement to be defined. Instead of setting the time in milliseconds, this method receives the time in minutes.
How to install the BrasilAPI-Java Library
The library was compiled in Java version 8 and is compatible with versions 8 or higher.
To install the library in a Maven project, simply include the following dependency in the pom.xml file.
<dependencies>
<dependency>
<groupId>com.github.SavioAndres</groupId>
<artifactId>BrasilAPI-Java</artifactId>
<version></version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
You only need to include the latest version in the <version> element. The version at the time of this article is v1.0.5, but to avoid using an outdated version, it is strongly recommended that you check the latest version in the BrasilAPI-Java repository.