A powerful FastAPI-based analytics platform for tracking sales, expenses, and generating comprehensive financial insights with chart-ready data formats.
- User Authentication & Authorization - Secure JWT-based authentication
- Sales Management - Track and manage sales transactions
- Expense Tracking - Monitor business expenses by category
- Financial Analytics - Comprehensive financial summaries and insights
- Chart-Ready Data - All analytics endpoints return data optimized for frontend charting libraries
- Time-Based Analysis - Monthly, weekly, and custom date range analytics
- Top Selling Items - Track best-performing products
- Expense Breakdown - Categorized expense analysis
- Backend: FastAPI (Python 3.10+)
- Database: SQLAlchemy ORM with Alembic migrations
- Authentication: JWT tokens with bcrypt password hashing
- Testing: Pytest with comprehensive test coverage
- API Documentation: Automatic OpenAPI/Swagger documentation
- Python 3.10 or higher
- PostgreSQL (or your preferred SQL database)
- pip (Python package manager)
-
Clone the repository
git clone <repository-url> cd analytics-api
-
Create a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the root directory:DATABASE_URL=postgresql://username:password@localhost/analytics_db SECRET_KEY=your-secret-key-here ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30
-
Run database migrations
alembic upgrade head
-
Start the development server
uvicorn app.main:app --reload
The API will be available at http://localhost:8000
Once the server is running, you can access:
- Interactive API Docs:
http://localhost:8000/docs - ReDoc Documentation:
http://localhost:8000/redoc
POST /api/v1/auth/login- User loginPOST /api/v1/auth/register- User registration
GET /api/v1/users/me- Get current user profilePUT /api/v1/users/me- Update user profile
GET /api/v1/sales/- List all salesPOST /api/v1/sales/- Create new saleGET /api/v1/sales/{id}- Get sale by IDPUT /api/v1/sales/{id}- Update saleDELETE /api/v1/sales/{id}- Delete sale
GET /api/v1/expenses/- List all expensesPOST /api/v1/expenses/- Create new expenseGET /api/v1/expenses/{id}- Get expense by IDPUT /api/v1/expenses/{id}- Update expenseDELETE /api/v1/expenses/{id}- Delete expense
GET /api/v1/analytics/summary/- Financial summary (chart-ready)GET /api/v1/analytics/monthly-summary- Monthly analyticsGET /api/v1/analytics/weekly-summary- Weekly analyticsGET /api/v1/analytics/top-selling-items- Top performing productsGET /api/v1/analytics/expense-breakdown- Expense analysis by category
All analytics endpoints return data in a standardized format optimized for charting libraries like Chart.js, D3.js, etc.:
{
"labels": ["Jan", "Feb", "Mar"],
"datasets": [
{
"label": "Income",
"data": [1200, 1500, 1800]
},
{
"label": "Expenses",
"data": [800, 900, 1000]
}
]
}Run the test suite:
# Run all tests
pytest
# Run with coverage
pytest --cov=app
# Run specific test file
pytest tests/test_analytics.py
# Run tests with verbose output
pytest -vThe application uses the following main models:
- User - User authentication and profile information
- Sale - Sales transactions with items, quantities, and amounts
- Expense - Business expenses with categories and amounts
Database migrations are managed with Alembic. To create a new migration:
alembic revision --autogenerate -m "Description of changes"
alembic upgrade head- JWT-based authentication
- Password hashing with bcrypt
- User-specific data isolation
- Input validation and sanitization
- SQL injection prevention through ORM
# Dockerfile example
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]- Set up a production database
- Configure environment variables
- Run migrations:
alembic upgrade head - Start with a production ASGI server:
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker
- Fork the repository
- Create a 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