Consult CNPJ with ADVPL (Protheus)
Consult CNPJ in Protheus using the CNPJ.ws API with ADVPL
The Totvs Protheus system is the leading ERP in the Brazilian market and can be easily and simply integrated with the CNPJ.ws API to consult customers or suppliers through the CNPJ.
Protheus uses Totvs' proprietary language, ADVPL (Advanced Protheus Language), which is a standard xBase programming language (Clipper, Visual Objects, and later Fivewin).
For this integration, we will build a generic communication class that can be used in various types of projects. You can also find this project on GitHub.
Remember that you can use our free API to consult CNPJ, but with a limitation of 3 queries per minute, or you can purchase one of our commercial plans.
With the integration, we can use the API to query the address, state registration, registration status, CNAE, and many other information that may be important when registering a customer or supplier, a legal entity, through the CNPJ.
Integration of CNPJ.ws with Totvs Protheus ERP
Let's get started, below is the class declaration:
We will have 6 methods in our class:
new: Responsible for instantiating the class;
setError: Internal method for generating errors;
consultarCNPJ: Responsible for querying the CNPJ using the CNPJ.ws API;
getResponse: Returns the API response in JSON format;
getError: Returns the error message;
Now let's develop the new
method, which is our constructor method. Note that this method receives the parameter lTest
(the default value is false) and yes, we will run tests with our integration class.
Also note that we have a CN_TOKEN parameter, which we must create if we are using the Commercial API (without restrictions) and fill it with the Token that was sent to your email.
In the new
method we use the consoleLog
method, so let's develop it now. This method aims to standardize the messages emitted by the class.
If the CN_VERBO parameter is set to .T., the class will print all messages in the Protheus console.log.
Below is the setError
method used by the class to generate error messages:
Now, let's develop the method responsible for communicating with the CNPJ.ws API, the consultarCNPJ
method. Here we will use the FWREST class to communicate with the API. This method receives the CNPJ as a parameter and performs the query in the API.
The method returns a boolean, if everything goes well it will return true, if an error occurs the return will be false, see below:
Below we have the method responsible for returning the JSON that the CNPJ.ws API returned in the query:
Below we have the method responsible for returning the error message if the query is not successful:
Great, we have built our class, now is a good time to develop a routine to use our class:
If you need support to implement this integration in your environment, we recommend Apply System, our partner, which has consultants specialized in system integration!
This post is quite long, isn't it? So I will leave a part 2 using TL++ to create a test for the integration class, but the source code is already on GitHub if you want to take a look!
To learn more about our plans and payment methods, visit CNPJ.ws.
See you soon!
Last updated
Was this helpful?