RFB-RS - ETL and API for Brazilian Federal Revenue (Receita Federal Brasileira) data in Rust π¦
A Rust implementation inspired by minha-receita, providing tools to download, transform, and serve Brazilian company data from the Federal Revenue.
- Download: Efficiently download large ZIP files from Federal Revenue using chunked HTTP requests
- Transform: Process and transform CSV data using Polars DataFrames
- Database: Load data into PostgreSQL with proper indexing
- API: REST API to query company information by CNPJ
- Privacy: Optional privacy mode to mask sensitive personal information
- Rust 1.70 or higher
- PostgreSQL 12+ (optional, for database functionality)
- ~200GB disk space for complete dataset
git clone https://github.com/italoag/rfb-rs
cd rfb-rs
cargo build --releaseThe binary will be available at target/release/rfb
Download all required files from Federal Revenue:
rfb download --directory data --parallel 4Options:
-d, --directory: Directory to save files (default:data)-s, --skip-existing: Skip already downloaded files-p, --parallel: Number of parallel downloads (default: 4)-r, --restart: Restart all downloads from beginning
Verify downloaded ZIP files:
rfb check --directory dataOptions:
-d, --directory: Directory with downloaded files-x, --delete: Delete corrupted files
Extract and process the downloaded data:
rfb transform --directory data --output outputOptions:
-d, --directory: Directory with downloaded files-o, --output: Output directory for processed data-p, --privacy: Enable privacy mode (masks CPF and personal data)
Create database tables:
export DATABASE_URL="postgres://user:password@localhost/rfb"
rfb db createDrop database tables:
rfb db dropOptions:
-d, --database-url: PostgreSQL connection URL (or use DATABASE_URL env var)-s, --schema: PostgreSQL schema name (default:public)
Start the REST API server:
export DATABASE_URL="postgres://user:password@localhost/rfb"
rfb api --port 8080Options:
--host: Host to bind to (default:127.0.0.1)-p, --port: Port to bind to (default: 8080)-d, --database-url: Database connection URL
API Endpoints:
GET /cnpj/{cnpj}- Get company information by CNPJGET /health- Health checkGET /metrics- Prometheus metrics
The system processes several types of data:
Main company data including:
- CNPJ (company ID)
- Trade name and legal name
- Address and contact information
- Registration status
- Economic activity (CNAE)
- Tax regime (Simples Nacional, MEI)
Company partners and stakeholders:
- Partner name and identification
- Entry date
- Qualification
- Legal representatives
Reference data for codes:
- Countries (PaΓses)
- Cities (MunicΓpios)
- Legal natures (Naturezas JurΓdicas)
- Qualifications (QualificaΓ§Γ΅es)
- Economic activities (CNAEs)
- Registration status motives (Motivos)
rfb-rs/
βββ src/
β βββ download/ # Download module
β β βββ federal_revenue.rs
β β βββ downloader.rs
β β βββ check.rs
β βββ transform/ # Transform module
β β βββ company.rs
β β βββ partner.rs
β β βββ cnae.rs
β β βββ tax_regime.rs
β β βββ lookups.rs
β β βββ transformer.rs
β βββ db/ # Database module
β β βββ postgres.rs
β β βββ schema.rs
β βββ api/ # API module
β β βββ server.rs
β β βββ handlers.rs
β βββ lib.rs
β βββ main.rs
βββ Cargo.toml
- Clap: Command-line argument parsing
- Polars: High-performance DataFrames for data transformation
- Diesel: Type-safe PostgreSQL ORM
- Reqwest: HTTP client for downloads
- Tokio: Async runtime
- Actix-web: HTTP server (planned)
- Tracing: Structured logging
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE.md file for details.
- minha-receita by @cuducos - The original Go implementation that inspired this project
- Receita Federal do Brasil - For providing open data
- minha-receita - Original Go implementation
- brasil.io - Similar data access service
- CI_CD.md - Complete CI/CD pipeline documentation
- QUICK_START.md - Quick start guide for development and deployment
- DEVELOPMENT.md - Developer guide
- IMPLEMENTATION.md - Architecture comparison with minha-receita
- CHANGELOG.md - Version history
This project is not officially affiliated with the Brazilian Federal Revenue. The data is publicly available and provided by the government.