- Python 3.9+
- PostgreSQL 15+ (or use Docker)
- Redis 7+ (optional, or use Docker)
cd fraud_detection
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt# Copy environment template
cp .env.example .env
# Edit .env with your configuration
nano .env# Start PostgreSQL with Docker
docker run -d \
--name dafu-postgres \
-e POSTGRES_USER=dafu \
-e POSTGRES_PASSWORD=dafu_secure_password \
-e POSTGRES_DB=dafu \
-p 5432:5432 \
postgres:15-alpine# Create database
createdb dafu
# Initialize tables
cd fraud_detection
python -c "from src.api.database import init_db; init_db()"cd core/features/fraud_detection/src/api
python main.pyThe API will be available at: http://localhost:8000
cd fraud_detection
uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --workers 4Once running, access:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI Schema: http://localhost:8000/openapi.json
The CLI is available as a Python module:
# From the fraud_detection directory
python -m src.api.cli <command>
# Or create an alias for convenience
alias dafu-api='python -m src.api.cli'# Register a new user
python -m src.api.cli auth register
# Login
python -m src.api.cli auth login
# Show current user
python -m src.api.cli auth whoami
# Logout
python -m src.api.cli auth logout# List recent logs
python -m src.api.cli logs list [limit]
# Show log statistics
python -m src.api.cli logs stats [hours]# List reports
python -m src.api.cli reports list [limit]
# Create a new report
python -m src.api.cli reports create
# View report details
python -m src.api.cli reports view <report_id>
# Show report statistics
python -m src.api.cli reports stats# List products
python -m src.api.cli products list [limit]
# List high-risk products
python -m src.api.cli products high-risk [limit]
# Show product statistics
python -m src.api.cli products statsCLI:
python -m src.api.cli auth registerAPI:
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"email": "john@example.com",
"password": "SecurePassword123",
"full_name": "John Doe",
"company": "ACME Corp"
}'CLI:
python -m src.api.cli auth login
# Enter username and password when promptedAPI:
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{
"username": "johndoe",
"password": "SecurePassword123"
}'Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"refresh_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer",
"expires_in": 86400
}# Set token in environment
export DAFU_TOKEN="your-access-token-here"
# Use in requests
curl -X GET "http://localhost:8000/api/v1/auth/me" \
-H "Authorization: Bearer $DAFU_TOKEN"CLI:
python -m src.api.cli auth generate-api-keyAPI:
curl -X POST "http://localhost:8000/api/v1/auth/api-key" \
-H "Authorization: Bearer $DAFU_TOKEN"# Check API health
curl http://localhost:8000/health
# Root endpoint
curl http://localhost:8000/| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/auth/register |
Register new user |
| POST | /api/v1/auth/login |
User login |
| POST | /api/v1/auth/logout |
User logout |
| GET | /api/v1/auth/me |
Get current user |
| POST | /api/v1/auth/change-password |
Change password |
| POST | /api/v1/auth/api-key |
Generate API key |
| GET | /api/v1/auth/users |
List all users (Admin) |
| GET | /api/v1/auth/users/{id} |
Get user by ID (Admin) |
| DELETE | /api/v1/auth/users/{id} |
Delete user (Admin) |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/logs/ |
Create log entry |
| GET | /api/v1/logs/ |
Get logs (Analyst+) |
| GET | /api/v1/logs/my-logs |
Get user's logs |
| GET | /api/v1/logs/stats |
Get log statistics (Analyst+) |
| GET | /api/v1/logs/{id} |
Get log by ID (Analyst+) |
| DELETE | /api/v1/logs/cleanup |
Cleanup old logs (Admin) |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/reports/ |
Create report |
| GET | /api/v1/reports/ |
Get user's reports |
| GET | /api/v1/reports/all |
Get all reports (Analyst+) |
| GET | /api/v1/reports/stats |
Get report statistics |
| GET | /api/v1/reports/{id} |
Get report by ID |
| PUT | /api/v1/reports/{id} |
Update report |
| DELETE | /api/v1/reports/{id} |
Delete report |
| POST | /api/v1/reports/{id}/retry |
Retry failed report |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/products/ |
Create product (Analyst+) |
| GET | /api/v1/products/ |
Get products |
| GET | /api/v1/products/high-risk |
Get high-risk products (Analyst+) |
| GET | /api/v1/products/low-stock |
Get low-stock products |
| GET | /api/v1/products/stats |
Get product statistics |
| GET | /api/v1/products/{id} |
Get product by ID |
| GET | /api/v1/products/sku/{sku} |
Get product by SKU |
| PUT | /api/v1/products/{id} |
Update product (Analyst+) |
| DELETE | /api/v1/products/{id} |
Delete product (Analyst+) |
| PUT | /api/v1/products/{id}/fraud-risk |
Update fraud risk (Analyst+) |
| POST | /api/v1/products/{id}/stock |
Update stock (Analyst+) |
# 1. Register
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"username": "analyst1",
"email": "analyst@company.com",
"password": "SecurePass123",
"full_name": "Senior Analyst"
}'
# 2. Login
TOKEN=$(curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{"username": "analyst1", "password": "SecurePass123"}' \
| jq -r '.access_token')
# 3. Get user info
curl -X GET "http://localhost:8000/api/v1/auth/me" \
-H "Authorization: Bearer $TOKEN"# Create a log entry
curl -X POST "http://localhost:8000/api/v1/logs/" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"level": "info",
"message": "User logged in successfully",
"module": "authentication",
"endpoint": "/api/v1/auth/login",
"method": "POST",
"status_code": 200,
"response_time_ms": 45.2
}'
# Get recent logs
curl -X GET "http://localhost:8000/api/v1/logs/my-logs?limit=10" \
-H "Authorization: Bearer $TOKEN"
# Get log statistics
curl -X GET "http://localhost:8000/api/v1/logs/stats?hours=24" \
-H "Authorization: Bearer $TOKEN"# Create a fraud detection report
curl -X POST "http://localhost:8000/api/v1/reports/" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Monthly Fraud Analysis",
"report_type": "fraud_detection",
"description": "Comprehensive fraud detection analysis for October 2025",
"config": {
"contamination": 0.1,
"models": ["isolation_forest", "lstm"]
},
"filters": {
"start_date": "2025-10-01",
"end_date": "2025-10-31"
}
}'
# List reports
curl -X GET "http://localhost:8000/api/v1/reports/?limit=20" \
-H "Authorization: Bearer $TOKEN"
# Get report details
curl -X GET "http://localhost:8000/api/v1/reports/1" \
-H "Authorization: Bearer $TOKEN"
# Get report statistics
curl -X GET "http://localhost:8000/api/v1/reports/stats" \
-H "Authorization: Bearer $TOKEN"# Create a product
curl -X POST "http://localhost:8000/api/v1/products/" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"sku": "PROD-12345",
"name": "Premium Widget",
"description": "High-quality widget with advanced features",
"price": 99.99,
"currency": "USD",
"category": "Electronics",
"brand": "TechBrand",
"stock_quantity": 100
}'
# List products
curl -X GET "http://localhost:8000/api/v1/products/?limit=20" \
-H "Authorization: Bearer $TOKEN"
# Get high-risk products
curl -X GET "http://localhost:8000/api/v1/products/high-risk" \
-H "Authorization: Bearer $TOKEN"
# Update product fraud risk
curl -X PUT "http://localhost:8000/api/v1/products/1/fraud-risk" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"fraud_risk_score": 0.75,
"high_risk": true,
"fraud_incidents": 5,
"chargeback_rate": 0.12
}'
# Update stock
curl -X POST "http://localhost:8000/api/v1/products/1/stock?quantity=150" \
-H "Authorization: Bearer $TOKEN"from fraud_detection.src.api.cli_client import DAFUClient, SessionManager
# Initialize client
client = DAFUClient(base_url="http://localhost:8000")
# Register and login
client.register(
username="analyst1",
email="analyst@company.com",
password="SecurePass123",
full_name="Senior Analyst"
)
# Login
result = client.login("analyst1", "SecurePass123")
print(f"Access Token: {result['access_token']}")
# Get current user
user = client.get_current_user()
print(f"Logged in as: {user['username']} ({user['role']})")
# Create a log
log = client.create_log(
level="info",
message="API test completed successfully",
module="testing"
)
# Get logs
logs = client.get_my_logs(limit=10)
for log in logs:
print(f"[{log['level'].upper()}] {log['message']}")
# Create a report
report = client.create_report(
name="Test Report",
report_type="fraud_detection",
description="Test fraud detection report"
)
print(f"Report ID: {report['id']}, Status: {report['status']}")
# Get products
products = client.get_products(limit=10)
for product in products:
print(f"SKU: {product['sku']}, Name: {product['name']}, Risk: {product['fraud_risk_score']}")| Role | Permissions |
|---|---|
| viewer | Read-only access to own data |
| user | Create and manage own resources |
| analyst | Access all data, create reports, manage products |
| admin | Full access, user management, system configuration |
# Check if port 8000 is already in use
lsof -i :8000
# Check database connection
psql -U dafu -d dafu -h localhost
# Check logs
tail -f core/features/fraud_detection/logs/api.log# Verify JWT token
python -c "from jose import jwt; print(jwt.decode('YOUR_TOKEN', 'SECRET_KEY', algorithms=['HS256']))"
# Check session file
cat ~/.dafu_session# Reinitialize database
python -c "from src.api.database import drop_db, init_db; drop_db(); init_db()"
# Check database tables
psql -U dafu -d dafu -c "\dt"- Always use HTTPS in production
- Change default passwords and secret keys
- Use environment variables for sensitive data
- Implement rate limiting for API endpoints
- Regularly backup your database
- Monitor API performance and errors
- Keep dependencies up to date
- Use strong passwords (minimum 12 characters)
- Enable 2FA for admin accounts (if implemented)
- Regularly review access logs
- ✅ Setup and start the API
- ✅ Register your first user
- ✅ Explore the API documentation at
/docs - ✅ Try CLI commands
- ✅ Create your first report
- ✅ Integrate with your fraud detection models
- ✅ Set up monitoring and alerts
- ✅ Deploy to production
For issues, questions, or contributions:
- GitHub Issues: https://github.com/MasterFabric/dafu/issues
- Documentation:
/docsdirectory - API Docs: http://localhost:8000/docs
AGPL-3.0 - See LICENSE file for details