π CidadΓ£o.AI API Layer
π Overview
The API Layer is the primary interface for the CidadΓ£o.AI platform, providing RESTful endpoints for transparency analysis, multi-agent orchestration, and real-time monitoring. Built with FastAPI and async/await patterns for high-performance concurrent processing.
ποΈ Architecture
src/api/
βββ app.py # FastAPI application entry point
βββ auth.py # OAuth2 authentication
βββ oauth.py # OAuth provider integration
βββ websocket.py # Real-time WebSocket communication
βββ middleware/ # Security & logging middleware
β βββ authentication.py # JWT authentication middleware
β βββ logging_middleware.py # Structured request logging
β βββ rate_limiting.py # Rate limiting with Redis
β βββ security.py # Security headers & CORS
βββ routes/ # API endpoints organized by domain
βββ investigations.py # Anomaly detection endpoints
βββ analysis.py # Pattern analysis endpoints
βββ reports.py # Report generation endpoints
βββ health.py # Health checks & monitoring
βββ auth.py # Authentication endpoints
βββ oauth.py # OAuth2 flow endpoints
βββ audit.py # Audit logging endpoints
βββ websocket.py # WebSocket event handlers
π API Endpoints
Core Endpoints
| Endpoint |
Method |
Description |
Authentication |
/ |
GET |
API information |
Public |
/docs |
GET |
Swagger UI documentation |
Public |
/health |
GET |
Basic health check |
Public |
/health/detailed |
GET |
Comprehensive system status |
Public |
/health/live |
GET |
Kubernetes liveness probe |
Public |
/health/ready |
GET |
Kubernetes readiness probe |
Public |
Authentication
| Endpoint |
Method |
Description |
/auth/login |
POST |
User authentication |
/auth/refresh |
POST |
Token refresh |
/auth/logout |
POST |
Session termination |
/auth/oauth/google |
GET |
Google OAuth2 flow |
/auth/oauth/github |
GET |
GitHub OAuth2 flow |
Investigations π
| Endpoint |
Method |
Description |
Agent |
/api/v1/investigations/start |
POST |
Start anomaly investigation |
InvestigatorAgent |
/api/v1/investigations/{id} |
GET |
Get investigation results |
- |
/api/v1/investigations/{id}/status |
GET |
Check investigation progress |
- |
/api/v1/investigations/stream |
GET |
Stream real-time results |
InvestigatorAgent |
Anomaly Types Supported:
price - Price anomalies using statistical methods
vendor - Vendor concentration analysis
temporal - Suspicious timing patterns
payment - Payment irregularities
duplicate - Duplicate contract detection
pattern - Custom pattern matching
Analysis π
| Endpoint |
Method |
Description |
Agent |
/api/v1/analysis/trends |
POST |
Spending trend analysis |
AnalystAgent |
/api/v1/analysis/patterns |
POST |
Pattern correlation analysis |
AnalystAgent |
/api/v1/analysis/efficiency |
POST |
Efficiency metrics calculation |
AnalystAgent |
/api/v1/analysis/{id} |
GET |
Get analysis results |
- |
Analysis Types:
spending_trends - Linear regression trend analysis
vendor_patterns - Vendor behavior analysis
organizational_behavior - Cross-org pattern comparison
seasonal_analysis - Seasonal pattern detection
efficiency_metrics - Performance indicators
correlation_analysis - Multi-dimensional correlations
Reports π
| Endpoint |
Method |
Description |
Agent |
/api/v1/reports/generate |
POST |
Generate investigation report |
ReporterAgent |
/api/v1/reports/{id} |
GET |
Retrieve generated report |
- |
/api/v1/reports/{id}/download |
GET |
Download report (PDF/HTML) |
- |
Report Formats:
json - Structured data format
markdown - Human-readable markdown
html - Web-formatted report
pdf - Professional PDF document (planned)
Audit & Security π‘οΈ
| Endpoint |
Method |
Description |
/audit/events |
GET |
Audit event history |
/audit/security |
GET |
Security event analysis |
/audit/compliance |
GET |
Compliance status |
π Security Features
Authentication & Authorization
Authentication: Bearer <jwt_token>
X-API-Key: <api_key>
- Google OAuth2
- GitHub OAuth2
Security Middleware Stack
1. SecurityMiddleware
2. LoggingMiddleware
3. RateLimitMiddleware
4. AuthenticationMiddleware
5. TrustedHostMiddleware
Rate Limiting
- 60 requests/minute
- 1000 requests/hour
- 10000 requests/day
investigations: 10/minute
analysis: 20/minute
reports: 5/minute
π Request/Response Models
Investigation Request
{
"query": "Analyze contracts from Ministry of Health 2024",
"data_source": "contracts",
"filters": {
"year": "2024",
"orgao": "20000",
"valor_min": 100000
},
"anomaly_types": ["price", "vendor", "temporal"],
"include_explanations": true,
"stream_results": false
}
Investigation Response
{
"investigation_id": "uuid4-string",
"status": "completed",
"query": "Analyze contracts...",
"data_source": "contracts",
"started_at": "2025-01-24T10:00:00Z",
"completed_at": "2025-01-24T10:05:30Z",
"anomalies_found": 23,
"total_records_analyzed": 15420,
"results": [
{
"anomaly_id": "uuid4-string",
"type": "price",
"severity": "high",
"confidence": 0.92,
"description": "Price 340% above expected range",
"explanation": "Statistical analysis shows...",
"affected_records": [...],
"suggested_actions": [...]
}
],
"summary": "Found 23 anomalies across 15,420 records...",
"confidence_score": 0.87,
"processing_time": 330.5
}
π Async Processing Patterns
Background Tasks
@router.post("/investigations/start")
async def start_investigation(
request: InvestigationRequest,
background_tasks: BackgroundTasks
):
investigation_id = str(uuid4())
background_tasks.add_task(
run_investigation,
investigation_id,
request
)
return {"investigation_id": investigation_id, "status": "started"}
Real-time Streaming
@router.get("/investigations/stream")
async def stream_investigation(investigation_id: str):
async def generate():
async for result in investigate_with_streaming(investigation_id):
yield f"data: {json.dumps(result)}\n\n"
return StreamingResponse(generate(), media_type="text/plain")
π¦ Error Handling
Custom Exception Hierarchy
CidadaoAIError (base)
βββ ValidationError (400)
βββ DataNotFoundError (404)
βββ AuthenticationError (401)
βββ UnauthorizedError (403)
βββ RateLimitError (429)
βββ LLMError (503)
βββ TransparencyAPIError (502)
βββ AgentExecutionError (500)
Error Response Format
{
"status": "error",
"status_code": 400,
"error": {
"error": "ValidationError",
"message": "Invalid data source provided",
"details": {
"field": "data_source",
"allowed_values": ["contracts", "expenses", "agreements"]
}
},
"request_id": "uuid4-string",
"timestamp": "2025-01-24T10:00:00Z"
}
π Monitoring & Observability
Health Checks
GET /health
{
"status": "healthy",
"timestamp": "2025-01-24T10:00:00Z",
"version": "1.0.0",
"uptime": 86400.5,
"services": {
"transparency_api": {"status": "healthy", "response_time": 0.145},
"database": {"status": "healthy", "response_time": 0.003},
"redis": {"status": "healthy", "response_time": 0.001}
}
}
Audit Logging
Audit Event Types:
- AUTHENTICATION_SUCCESS/FAILURE
- API_ACCESS
- INVESTIGATION_STARTED/COMPLETED
- REPORT_GENERATED
- SECURITY_VIOLATION
- DATA_ACCESS
Structured Logging
{
"timestamp": "2025-01-24T10:00:00Z",
"level": "INFO",
"logger": "api.routes.investigations",
"message": "investigation_started",
"investigation_id": "uuid4-string",
"user_id": "user123",
"data_source": "contracts",
"anomaly_types": ["price", "vendor"],
"processing_time": 0.045
}
π§ͺ Testing Strategy
Test Categories
pytest tests/unit/api/
pytest tests/integration/api/
pytest tests/e2e/api/
pytest tests/performance/api/
Test Configuration
@pytest.fixture
async def test_client():
with TestClient(app) as client:
yield client
@pytest.fixture
def authenticated_headers():
token = create_test_jwt()
return {"Authorization": f"Bearer {token}"}
π§ Configuration
Environment Variables
HOST=0.0.0.0
PORT=8000
WORKERS=4
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/cidadao_ai
REDIS_URL=redis://localhost:6379/0
TRANSPARENCY_API_KEY=your_api_key
GROQ_API_KEY=your_groq_key
SECRET_KEY=your-super-secret-key
JWT_SECRET_KEY=your-jwt-secret
CORS_ORIGINS=["http://localhost:3000", "https://cidadao.ai"]
Feature Flags
ENABLE_FINE_TUNING=false
ENABLE_AUTONOMOUS_CRAWLING=false
ENABLE_ADVANCED_VISUALIZATIONS=false
ENABLE_ETHICS_GUARD=true
π Development
Local Development
pip install -r requirements/base.txt
uvicorn src.api.app:app --reload --host 0.0.0.0 --port 8000
make dev
Docker Development
docker build -f Dockerfile.api -t cidadao-api:dev .
docker-compose -f docker-compose.dev.yml up api
Code Quality
black src/api/
ruff check src/api/
mypy src/api/
bandit -r src/api/
make lint
π API Documentation
Interactive Documentation
- Swagger UI:
/docs - Interactive API explorer
- OpenAPI Schema:
/openapi.json - Machine-readable spec
Authentication for Documentation
1. Click "Authorize" button
2. Enter: Bearer <your_jwt_token>
3. Test endpoints with authentication
π€ Integration Patterns
Frontend Integration
const response = await fetch('/api/v1/investigations/start', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: 'Analyze suspicious contracts',
data_source: 'contracts',
anomaly_types: ['price', 'vendor']
})
});
Webhook Integration
@app.post("/webhook/investigation-complete")
async def handle_investigation_complete(payload: dict):
investigation_id = payload["investigation_id"]
results = payload["results"]
This API layer provides a robust, secure, and scalable interface for the CidadΓ£o.AI platform, enabling efficient access to transparency analysis capabilities through well-designed RESTful endpoints.