SDKs overview
Integrating with Brazil's digital government services, primarily through the Gov.br platform, involves utilizing various Software Development Kits (SDKs) and libraries. These tools abstract the complexities of API interactions, authentication flows, and data formatting, allowing developers to focus on application logic. The landscape includes official SDKs maintained by government entities or their partners, alongside a range of community-driven libraries that extend functionality or provide alternative language support. These resources are crucial for developers building applications that require access to public services, official data, or citizen authentication within the Brazilian digital ecosystem.
The Gov.br platform serves as a central hub for federal government services and information. Its underlying architecture often leverages standard web technologies and protocols, making it accessible through common programming paradigms. While a comprehensive, single 'Brazil SDK' does not exist due to the federated nature of government services, specific initiatives and departments provide their own integration tools. Developers seeking to interact with specific services, such as digital identity verification or specific public data sets, will typically find dedicated documentation and libraries for those particular endpoints. The emphasis is on secure and standardized access, often relying on protocols like OAuth 2.0 and OpenID Connect for identity and authorization.
Official SDKs by language
Official SDKs and libraries related to Brazilian government services are often developed and maintained by the specific agencies responsible for those services or by contractors working on their behalf. While a single, overarching SDK for all of Gov.br is not prevalent, several key services offer dedicated programmatic interfaces. These generally focus on areas such as digital identity, tax information, and specific public datasets. The primary languages supported reflect common enterprise and web development stacks used in Brazil.
The table below outlines some of the commonly available official or semi-official integration packages, their primary language, installation methods, and general maturity level. These tools aim to simplify interactions with specific APIs, ensuring compliance with security and data standards set by the Brazilian government.
| Language | Package/Library | Install Command (Example) | Maturity | Description |
|---|---|---|---|---|
| Python | govbr-sso-client |
pip install govbr-sso-client |
Stable | Client for Gov.br Single Sign-On (SSO) integration, handling OAuth 2.0 and OpenID Connect flows for user authentication. |
| Java | govbr-id-sdk |
Maven/Gradle dependency | Stable | SDK for integrating with the Gov.br digital identity system, enabling secure user authentication and profile access. |
| JavaScript (Node.js) | @govbr/sso-js |
npm install @govbr/sso-js |
Stable | JavaScript library for client-side and Node.js applications to interact with Gov.br SSO services. |
| Python | receitaws-python |
pip install receitaws-python |
Community-supported (semi-official) | Wrapper for the ReceitaWS API, providing company and CNPJ data from the Brazilian Federal Revenue. |
Installation
Installation procedures for Brazil-related SDKs and libraries typically follow standard practices for their respective programming language ecosystems. Developers should consult the specific documentation for each package, usually found on official government developer portals or public code repositories. The general steps involve using package managers to download and integrate the libraries into a project.
Python
For Python libraries, the primary package installer is pip. To install a package like govbr-sso-client, you would execute the following command in your terminal:
pip install govbr-sso-client
It is recommended to use Python virtual environments to manage dependencies for different projects.
Java
Java projects commonly use build automation tools like Maven or Gradle. To include a Java SDK, you would add the dependency to your project's pom.xml (for Maven) or build.gradle (for Gradle) file. For example, a Maven dependency might look like this:
<dependency>
<groupId>br.gov.govbr</groupId>
<artifactId>govbr-id-sdk</artifactId>
<version>1.0.0</version>
</dependency>
Ensure you replace 1.0.0 with the latest stable version available in the official documentation.
JavaScript (Node.js)
For JavaScript-based SDKs, especially those targeting Node.js environments, the Node Package Manager (npm) or Yarn are the standard tools. To install a package like @govbr/sso-js:
npm install @govbr/sso-js
# or
yarn add @govbr/sso-js
These commands will download the package and its dependencies into your project's node_modules directory.
Quickstart example
This quickstart example demonstrates a basic integration with the Gov.br Single Sign-On (SSO) system using a hypothetical Python library, illustrating the common steps involved in authenticating a user and retrieving basic profile information. This example assumes you have registered your application with Gov.br and obtained client credentials (Client ID and Client Secret).
Python Gov.br SSO Client Example
This snippet outlines how to initiate an authentication flow and handle the callback, typically in a web application context using a framework like Flask or Django.
from flask import Flask, redirect, url_for, session, request
from govbr_sso_client import GovBrSSOClient
import os
app = Flask(__name__)
app.secret_key = os.urandom(24) # Replace with a strong, permanent secret key
# Configure Gov.br SSO Client
CLIENT_ID = os.environ.get('GOVBR_CLIENT_ID', 'YOUR_GOVBR_CLIENT_ID')
CLIENT_SECRET = os.environ.get('GOVBR_CLIENT_SECRET', 'YOUR_GOVBR_CLIENT_SECRET')
REDIRECT_URI = os.environ.get('GOVBR_REDIRECT_URI', 'http://localhost:5000/callback')
govbr_sso = GovBrSSOClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
@app.route('/')
def index():
if 'user_info' in session:
return f"Hello, {session['user_info'].get('name', 'User')}! <a href='/logout'>Logout</a>"
else:
return "<a href='/login'>Login with Gov.br</a>"
@app.route('/login')
def login():
auth_url = govbr_sso.get_authorization_url()
return redirect(auth_url)
@app.route('/callback')
def callback():
code = request.args.get('code')
if code:
try:
token_response = govbr_sso.exchange_code_for_token(code)
access_token = token_response['access_token']
user_info = govbr_sso.get_user_info(access_token)
session['user_info'] = user_info
return redirect(url_for('index'))
except Exception as e:
return f"Authentication failed: {e}"
return "No authorization code received."
@app.route('/logout')
def logout():
session.pop('user_info', None)
# Optionally, implement Gov.br logout endpoint if available
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
To run this example:
- Install Flask and the Gov.br SSO client:
pip install Flask govbr-sso-client - Set your environment variables for
GOVBR_CLIENT_ID,GOVBR_CLIENT_SECRET, andGOVBR_REDIRECT_URI. - Run the application:
python your_app.py - Navigate to
http://localhost:5000/in your browser.
This example demonstrates the core flow: redirecting the user for authentication, receiving an authorization code, exchanging it for an access token, and then using the token to fetch user profile data. For production environments, robust error handling, secure storage of secrets, and comprehensive session management are essential.
Community libraries
Beyond the official offerings, the developer community in Brazil has contributed a variety of libraries that facilitate interaction with public data and services. These libraries often fill gaps, provide alternative implementations, or focus on specific niche areas not directly covered by official SDKs. Community libraries are particularly prevalent in areas requiring data parsing, integration with specific local payment gateways, or tools for working with publicly available datasets (e.g., from IBGE - Instituto Brasileiro de Geografia e Estatística).
Key areas where community libraries thrive include:
- Brazilian Document Validators: Libraries for validating CPF (Cadastro de Pessoas Físicas) and CNPJ (Cadastro Nacional da Pessoa Jurídica) numbers, which are essential identifiers in Brazil. Examples include Python packages like
brazilian-documents. - CEP (Postal Code) Lookup: Tools for querying Brazilian postal code databases, often integrating with public APIs like ViaCEP.
- NFe (Nota Fiscal Eletrônica) Integration: While complex, some community efforts aim to simplify the generation and management of electronic invoices, though this often involves highly specialized and regulated processes.
- Financial Data APIs: Libraries that wrap unofficial or community-driven APIs for accessing stock market data or other financial information relevant to the Brazilian market.
Developers should exercise due diligence when using community libraries, verifying their maintenance status, security practices, and adherence to official API specifications. Contributions to these projects are often welcomed, fostering a collaborative development environment around Brazilian digital services.