Sávio Andres

Banner postagem: BrasilAPI-Java Library

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 immediately remembered a YouTube video by Filipe Deschamps that talked about a system that did exactly what I needed. Searching for this system, called BrasilAPI, I discovered the number of people contributing to its improvement and realized that there were libraries in various programming languages ​​to facilitate integration. 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.

Official website: https://brasilapi.com.br
Repository: https://github.com/BrasilAPI/BrasilAPI

Currently, it offers the following endpoints:

  • Banks (Information about Banks)
  • CEP (Information about Postal 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 about 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)
  • Taxes (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 endpoint provided by the BrasilAPI system and the Java programming language. For example, let's say you need to obtain information about a company whose CNPJ (Brazilian tax ID) is "06.990.590/0001-23". To do this, simply access the endpoint https://brasilapi.com.br/api/cnpj/v1/06990590000123. This will give you the company's data in JSON format. That's where the BrasilAPI-Java library comes in. With it, you simply provide the CNPJ 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 "BrasilAPI.setEnableCache()" method receives a boolean as a parameter; if true, it enables caching, 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 the same way as the "BrasilAPI.setCacheTime()" method; the difference is in the unit of measurement to be defined. Instead of set 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.