A comprehensive RESTful API for managing financial transactions built with Python, FastAPI, PostgreSQL and Docker
- ✅ Full CRUD operations for financial transactions
- ✅ Transaction filtering and pagination
- ✅ Transaction statistics and summaries
- ✅ PostgreSQL database with SQLAlchemy ORM
- ✅ Database migrations with Alembic
- ✅ Docker containerization
- ✅ AWS S3 integration for file storage
- ✅ RESTful API with OpenAPI documentation
- ✅ Type-safe with Pydantic models
- Python 3.11+
- FastAPI - Modern, fast web framework
- PostgreSQL - Relational database
- SQLAlchemy - ORM for database operations
- Alembic - Database migration tool
- Docker & Docker Compose - Containerization
- AWS S3 - File storage service
- Pydantic - Data validation
financial_transaction_API/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application entry point
│ ├── api/
│ │ └── v1/
│ │ ├── router.py # API router
│ │ └── endpoints/
│ │ └── transactions.py # Transaction endpoints
│ ├── core/
│ │ ├── config.py # Application configuration
│ │ └── database.py # Database connection
│ ├── models/
│ │ └── transaction.py # SQLAlchemy models
│ ├── schemas/
│ │ └── transaction.py # Pydantic schemas
│ └── services/
│ └── aws_service.py # AWS S3 service
├── alembic/ # Database migrations
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker image definition
├── requirements.txt # Python dependencies
└── README.md # This file
- Docker and Docker Compose installed
- Python 3.11+ (for local development)
-
Clone the repository (if applicable) or navigate to the project directory
-
Create a
.envfile from the example:cp .env.example .env
-
Update
.envfile with your configuration:DATABASE_URL=postgresql://postgres:postgres@db:5432/financial_db SECRET_KEY=your-secret-key-here AWS_ACCESS_KEY_ID=your-aws-access-key AWS_SECRET_ACCESS_KEY=your-aws-secret-key AWS_REGION=us-east-1 AWS_S3_BUCKET_NAME=your-bucket-name
-
Start the services:
docker-compose up -d
-
Run database migrations:
docker-compose exec api alembic upgrade head -
Access the API:
- API: http://localhost:8000
- Interactive API docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
-
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 PostgreSQL database:
- Install PostgreSQL locally or use Docker:
docker run --name postgres_db -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=financial_db -p 5432:5432 -d postgres:15-alpine
- Install PostgreSQL locally or use Docker:
-
Create
.envfile with your local database URL:DATABASE_URL=postgresql://postgres:postgres@localhost:5432/financial_db SECRET_KEY=your-secret-key-here
-
Run database migrations:
alembic upgrade head
-
Start the development server:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
alembic revision --autogenerate -m "Description of changes"alembic upgrade headalembic downgrade -1POST /api/v1/transactions/- Create a new transactionGET /api/v1/transactions/- List all transactions (with pagination and filters)GET /api/v1/transactions/{id}- Get a specific transactionPUT /api/v1/transactions/{id}- Update a transactionDELETE /api/v1/transactions/{id}- Delete a transactionGET /api/v1/transactions/stats/summary- Get transaction statistics
page- Page number (default: 1)page_size- Items per page (default: 10, max: 100)transaction_type- Filter by type:income,expense,transferstatus- Filter by status:pending,completed,failed,cancelledcategory- Filter by category string
curl -X POST "http://localhost:8000/api/v1/transactions/" \
-H "Content-Type: application/json" \
-d '{
"amount": 1000.50,
"currency": "USD",
"transaction_type": "income",
"description": "Salary payment",
"category": "Salary",
"from_account": "Company ABC",
"to_account": "My Account"
}'curl "http://localhost:8000/api/v1/transactions/?page=1&page_size=10&transaction_type=income"curl "http://localhost:8000/api/v1/transactions/stats/summary"- id: Unique identifier
- amount: Transaction amount (positive number)
- currency: Currency code (ISO 4217, default: USD)
- transaction_type:
income,expense, ortransfer - status:
pending,completed,failed, orcancelled - description: Transaction description
- category: Transaction category
- from_account: Source account
- to_account: Destination account
- reference_number: Unique reference number (auto-generated if not provided)
- metadata: Additional JSON data
- created_at: Timestamp of creation
- updated_at: Timestamp of last update
The API includes AWS S3 integration for file storage. To use it:
-
Configure AWS credentials in
.env:AWS_ACCESS_KEY_ID=your-access-key AWS_SECRET_ACCESS_KEY=your-secret-key AWS_REGION=us-east-1 AWS_S3_BUCKET_NAME=your-bucket-name
-
The AWS service is available in
app/services/aws_service.pyand can be used to:- Upload files to S3
- Download files from S3
- Delete files from S3
You can test the API using the interactive documentation at /docs or using tools like curl or Postman.
- Database: Consider using AWS RDS for PostgreSQL
- Application: Deploy to AWS ECS, EKS, or EC2
- Load Balancer: Use AWS Application Load Balancer
- Secrets: Use AWS Secrets Manager for sensitive configuration
- Monitoring: Set up CloudWatch for logging and monitoring
Make sure to set secure values for:
SECRET_KEY- Use a strong, random secret keyDATABASE_URL- Use a secure database connection stringDEBUG- Set toFalsein production- AWS credentials - Use IAM roles when possible
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
For issues and questions, please open an issue in the repository.