opencnpj 0.2.1
opencnpj: ^0.2.1 copied to clipboard
A robust, pure Dart client for the OpenCNPJ API. Easily query, validate, and format Brazilian company data (CNPJ) in Flutter and Dart apps.
[OpenCNPJ Banner]
OpenCNPJ Dart Client #
An unofficial, robust, and pure Dart client library for the OpenCNPJ API. Easily query, validate, and format Brazilian company data (CNPJ) in your Dart and Flutter applications.
π§π· Leia em PortuguΓͺs
π Table of Contents #
- Features
- Installation
- Usage
- Examples
- Supported Fields
- API Limitations
- Author
- Contributing
- Disclaimer
- License
β¨ Features #
- π Easy to Use: Simple, async API to fetch company details.
- π‘οΈ Robust Validation: Validate CNPJ format and check digits locally before hitting the API.
- π¨ Formatting: Built-in utilities to format CNPJs (
XX.XXX.XXX/XXXX-XX). - π¦ Strongly Typed: Full support for all OpenCNPJ fields, including Partners (
QSA) and CNAEs. - β‘ Performance: Zero-dependency on Flutter (runs on server, CLI, and web).
- π Safety: Input sanitization and specific exceptions for predictable error handling.
π¦ Installation #
Add this to your pubspec.yaml:
dependencies:
opencnpj: ^0.2.0
Or run:
dart pub add opencnpj
π» Usage #
Basic Query #
Import the package and instantiate the client. You can fetch company data using a formatted or unformatted CNPJ string.
import 'package:opencnpj/opencnpj.dart';
void main() async {
final client = OpenCNPJ();
try {
// Fetch company data
final company = await client.search('06.990.590/0001-23');
print('π’ Company: ${company.razaoSocial}');
print('π State: ${company.uf}');
print('πΌ Status: ${company.situacaoCadastral}');
// Access nested data (Partners/QSA)
if (company.qsa.isNotEmpty) {
print('π₯ Partners:');
for (final partner in company.qsa) {
print(' - ${partner.nomeSocio} (${partner.qualificacaoSocio})');
}
}
} on NotFoundException {
print('β Company not found.');
} on InvalidCNPJException {
print('β Invalid CNPJ format.');
} catch (e) {
print('β Error: $e');
}
}
Validation #
Validate a CNPJ locally to avoid unnecessary API calls. Uses strict mathematical validation (check digits).
import 'package:cpf_cnpj_validator/cnpj_validator.dart';
bool isValid = CNPJValidator.isValid('06.990.590/0001-23'); // true
bool isInvalid = CNPJValidator.isValid('11.111.111/1111-11'); // false
Formatting #
Format a raw CNPJ string for display in your UI.
import 'package:opencnpj/opencnpj.dart';
String formatted = OpenCNPJ.formatCnpj('06990590000123');
print(formatted); // 06.990.590/0001-23
π± Examples #
This repository includes fully functional examples to help you get started:
Flutter Web Example #
A responsive, mobile-first web application that auto-fills company registration forms using CNPJ search.
-
Source:
example/flutter_example -
Run:
cd example/flutter_example flutter run -d chrome -
Demo:
[Demo]
Shelf Server Example #
A backend REST API built with Dart Shelf that proxies requests to OpenCNPJ and implements a CRUD for widgets.
- Source:
example/server - Run:
cd example/server dart run bin/server.dart
β¨ Features #
- π Easy to Use: Simple, async API to fetch company details.
- π‘οΈ Robust Validation: Validate CNPJ format and check digits locally before hitting the API.
- π¨ Formatting: Built-in utilities to format CNPJs (
XX.XXX.XXX/XXXX-XX). - π¦ Strongly Typed: Full support for all OpenCNPJ fields, including Partners (
QSA) and CNAEs. - β‘ Performance: Zero-dependency on Flutter (runs on server, CLI, and web).
- π Safety: Input sanitization and specific exceptions for predictable error handling.
π¦ Installation #
Add this to your pubspec.yaml:
dependencies:
opencnpj: ^0.2.0
Or run:
dart pub add opencnpj
π» Usage #
Basic Query #
Import the package and instantiate the client. You can fetch company data using a formatted or unformatted CNPJ string.
import 'package:opencnpj/opencnpj.dart';
void main() async {
final client = OpenCNPJ();
try {
// Fetch company data
final company = await client.search('06.990.590/0001-23');
print('π’ Company: ${company.razaoSocial}');
print('π State: ${company.uf}');
print('πΌ Status: ${company.situacaoCadastral}');
// Access nested data (Partners/QSA)
if (company.qsa.isNotEmpty) {
print('π₯ Partners:');
for (final partner in company.qsa) {
print(' - ${partner.nomeSocio} (${partner.qualificacaoSocio})');
}
}
} on NotFoundException {
print('β Company not found.');
} on InvalidCNPJException {
print('β Invalid CNPJ format.');
} catch (e) {
print('β Error: $e');
}
}
Validation #
Validate a CNPJ locally to avoid unnecessary API calls. Uses strict mathematical validation (check digits).
import 'package:cpf_cnpj_validator/cnpj_validator.dart';
bool isValid = CNPJValidator.isValid('06.990.590/0001-23'); // true
bool isInvalid = CNPJValidator.isValid('11.111.111/1111-11'); // false
Formatting #
Format a raw CNPJ string for display in your UI.
import 'package:opencnpj/opencnpj.dart';
String formatted = OpenCNPJ.formatCnpj('06990590000123');
print(formatted); // 06.990.590/0001-23
π Supported Fields #
The Company model maps 1:1 with the OpenCNPJ response. Key fields include:
| Field | Description |
|---|---|
cnpj |
The 14-digit company ID. |
razaoSocial |
Legal name. |
nomeFantasia |
Trade name (optional). |
situacaoCadastral |
Registration status (e.g., ATIVA). |
cnaePrincipal |
Main economic activity code. |
qsa |
List of partners (Quadra de SΓ³cios). |
telefones |
List of contact numbers. |
address |
Fields like logradouro, bairro, municipio, uf. |
β οΈ API Limitations & Data Source #
- Rate Limit: The official API allows 50 requests per second per IP.
- Data Source: Receita Federal (Brazilian Federal Revenue).
- Update Frequency: Monthly. Data may not reflect real-time changes (e.g., a company opened yesterday).
- Offline/Static: The API serves static files; if a CNPJ isn't in the monthly dump, it returns 404.
π¨βπ» Author #
Cristiano ArΓͺdes Costa Senior Software Engineer specializing in Flutter, Dart, and mobile development with 15+ years of experience.
- π Portfolio: aredes.me
- πΌ LinkedIn: linkedin.com/in/cristianoaredes
- π§ Email: cristianoaredes@icloud.com
Other Open Source Projects #
Check out my other open-source contributions:
- MCP DadosBR - Model Context Protocol server for Brazilian public data (CNPJ/CEP validation, Tavily search) with 62+ tools
- MCP CΓ’mara - MCP server for Brazilian Chamber of Deputies legislative data with 62+ tools for bills, deputies, and votes
- Super-App Flutter Sample - Modular Flutter architecture example with GoRouter, GetIt, and BLoC pattern
- MCP Mobile Server - Android/iOS/Flutter build automation and CI/CD tooling via MCP
- AnythingToLLMs.txt - Universal document converter for LLM processing
π€ Contributing #
Contributions are welcome!
- Fork this repository.
- Create your feature branch (
git checkout -b feature/amazing-feature). - Commit your changes (
git commit -m 'Add some amazing feature'). - Push to the branch (
git push origin feature/amazing-feature). - Open a Pull Request.
βοΈ Disclaimer #
This library is an unofficial open-source project and is not affiliated with the OpenCNPJ.org team. For official API documentation, visit opencnpj.org.
π License #
This project is licensed under the MIT License - see the LICENSE file for details.