A virtual wallet simulation API built with FastAPI, featuring role-based access control, scheduled transactions, and comprehensive transaction management.
- User Management: Registration, authentication (JWT), and profile management
- Multi-Wallet Support: Users can have multiple wallets with different currencies
- Transaction Types: Deposits, withdrawals, and transfers (in/out)
- Scheduled Transactions: One-time scheduled or recurring transactions (daily, weekly, monthly)
- Transaction Management: Create, view, and cancel transactions with cursor-based pagination
- Role-Based Access Control (RBAC):
- Regular users: Manage their own wallets and transactions
- Staff users: Access and manage all wallets and transactions
- Superusers: All staff permissions plus ability to modify wallet balances directly
- Monetary Precision: Uses Decimal type with 6 decimal places to prevent floating-point errors
- Async/Await: Fully asynchronous for high performance
- Database: PostgreSQL with async SQLAlchemy 2.0
- Background Tasks: Celery + Redis for processing scheduled/recurring transactions
- Database Migrations: Alembic for schema management
- Comprehensive Testing: Integration and unit tests with pytest
- Type Safety: Full type hints with strict MyPy checking
- Code Quality: Ruff linting with strict rules
- Docker Support: Multi-container setup with Docker Compose
fastapi-wallet/
βββ app/
β βββ core/
β β βββ config.py # Settings and configuration
β β βββ database.py # Database session management
β β βββ security.py # Password hashing and JWT
β βββ models/
β β βββ base.py # Base model with UUID and timestamps
β β βββ user.py # User model with roles
β β βββ wallet.py # Wallet model with balance
β β βββ transaction.py # Transaction model with scheduling
β βββ schemas/
β β βββ user.py # Pydantic schemas for users
β β βββ wallet.py # Pydantic schemas for wallets
β β βββ transaction.py # Pydantic schemas for transactions
β β βββ pagination.py # Cursor-based pagination schemas
β βββ services/
β β βββ user_service.py # User business logic
β β βββ wallet_service.py # Wallet business logic
β β βββ transaction_service.py # Transaction business logic
β βββ routers/
β β βββ auth.py # Registration and login endpoints
β β βββ users.py # User management endpoints
β β βββ wallets.py # Wallet CRUD endpoints
β β βββ transactions.py # Transaction endpoints
β βββ tasks/
β β βββ celery_app.py # Celery configuration
β β βββ transaction_tasks.py # Scheduled transaction processor
β βββ dependencies.py # FastAPI dependencies (auth, RBAC)
β βββ main.py # FastAPI application entry point
βββ alembic/
β βββ versions/ # Database migration files
β βββ env.py # Alembic environment config
β βββ script.py.mako # Migration template
βββ tests/
β βββ conftest.py # Pytest fixtures
β βββ test_auth_integration.py
β βββ test_users_integration.py
β βββ test_wallets_integration.py
β βββ test_transactions_integration.py
β βββ test_role_permissions.py # Unit tests for RBAC
βββ docker-compose.yml # Multi-container Docker setup
βββ Dockerfile # Application container
βββ .env.example # Environment variables template
βββ pyproject.toml # Poetry dependencies and config
βββ CLAUDE.md # Claude Code guidelines
βββ FUTURE.md # Future feature ideas
βββ README.md # This file
- Python 3.12.3+
- Poetry 1.8+
- Docker & Docker Compose (optional, recommended)
# Clone repository
git clone <your-repo-url>
cd fastapi-wallet
# Copy environment variables
cp .env.example .env
# Start all services (PostgreSQL, Redis, FastAPI, Celery)
docker-compose up --build
# Run database migrations (in another terminal)
docker-compose exec app alembic upgrade head
# Create a superuser (optional)
docker-compose exec app python -m app.scripts.create_superuserThe API will be available at http://localhost:8000
# Install Poetry (if not installed)
curl -sSL https://install.python-poetry.org | python3 -
# Clone repository
git clone <your-repo-url>
cd fastapi-wallet
# Install dependencies
poetry install
# Copy environment variables and configure
cp .env.example .env
# Edit .env with your database credentials
# Start PostgreSQL and Redis (via Docker)
docker-compose up -d db redis
# Run database migrations
poetry run alembic upgrade head
# Start the FastAPI server
poetry run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# In separate terminals, start Celery worker and beat
poetry run celery -A app.tasks.celery_app worker --loglevel=info
poetry run celery -A app.tasks.celery_app beat --loglevel=infoPOST /api/auth/register- Register new user (auto-creates default wallet)POST /api/auth/login- Login and get JWT token
GET /api/users/me- Get current user profilePATCH /api/users/me- Update current userDELETE /api/users/me- Soft delete current user
GET /api/wallets- List user's walletsPOST /api/wallets- Create new walletGET /api/wallets/{id}- Get wallet by IDPATCH /api/wallets/{id}- Update wallet
POST /api/transactions- Create transaction (immediate/scheduled/recurring)GET /api/transactions?wallet_id={id}- List transactions (cursor-paginated)GET /api/transactions/{id}- Get transaction by IDPATCH /api/transactions/{id}/cancel- Cancel pending transaction
GET /api/docs- Swagger UIGET /api/redoc- ReDocGET /health- Health check
- Minimum 8 characters
- At least 1 number
- At least 1 uppercase letter
- Minimum amount: β¬1.00
- Amount must be positive
- Wallet balance must remain >= 0
- Currency: 3-letter code (e.g., EUR, USD)
- Balance uses Decimal with 6 decimal places
- Balance cannot be negative
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=app --cov-report=html
# Run specific test file
poetry run pytest tests/test_auth_integration.py
# Run tests with output
poetry run pytest -v -s# Lint code
poetry run ruff check .
# Auto-fix linting issues
poetry run ruff check --fix .
# Type checking
poetry run mypy .# Create a new migration
poetry run alembic revision --autogenerate -m "description"
# Apply migrations
poetry run alembic upgrade head
# Rollback migration
poetry run alembic downgrade -1
# View migration history
poetry run alembic history# Build and run with Docker Compose
docker-compose -f docker-compose.prod.yml up --build -d
# Or run directly with Uvicorn
poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4- Change
SECRET_KEYin.env - Set
DEBUG=False - Use strong database credentials
- Enable HTTPS
- Set up monitoring and logging
- Configure backup strategy
- Set up rate limiting
- Review CORS settings
Monetary values use Decimal to prevent floating-point precision errors:
# Bad (float precision issues)
0.1 + 0.2 = 0.30000000000000004
# Good (exact decimal precision)
Decimal("0.1") + Decimal("0.2") = Decimal("0.3")- Security: Prevents ID enumeration attacks
- Distributed Systems: No collision in distributed databases
- Privacy: Harder to guess or predict IDs
- Consistency: No missing items when data changes
- Performance: More efficient for large datasets
- Ordering: Based on
updated_atfor most recent items first
- Audit Trail: Maintain history for compliance
- Data Integrity: Preserve relationships (user β wallet β transactions)
- Recovery: Ability to restore deleted accounts
See FUTURE.md for planned features including:
- Currency conversion with live exchange rates
- Transaction fees
- Multi-factor authentication (MFA)
- Advanced analytics and reporting
- Webhook notifications
- And more...
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
MIT