FastAPI application for processing files and storing transaction data in PostgreSQL.
Using Poetry (recommended):
poetry installOr using pip:
pip install fastapi uvicorn sqlalchemy psycopg2-binary python-multipart pydantic pydantic-settingsCreate a PostgreSQL database:
# Login to PostgreSQL
sudo -u postgres psql
# Create database
CREATE DATABASE "macro-analytics";
# Create user (optional)
CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE "macro-analytics" TO your_username;Edit database.py and update the DATABASE_URL with your credentials:
DATABASE_URL = "postgresql://username:password@localhost:5432/macro-analytics"Or use environment variables (recommended):
- Copy
.env.exampleto.env - Update the credentials in
.env
Important: Update the following files with your actual transaction fields:
-
models.py- Update theTransactionmodel:- Add columns matching your data structure
- Example fields are commented out - uncomment and modify as needed
-
schemas.py- Update the Pydantic schemas:- Update
TransactionBase,TransactionCreate, andTransactionResponse - Match the fields you defined in your model
- Update
Edit apis.py in the upload_file function:
- Add your file parsing logic (CSV, JSON, Excel, etc.)
- Map file data to Transaction model fields
- The TODO comments guide you through the implementation
Example for CSV:
import csv
csv_data = io.StringIO(contents.decode('utf-8'))
reader = csv.DictReader(csv_data)
for row in reader:
transaction = Transaction(
transaction_id=row['id'],
amount=float(row['amount']),
# ... map other fields
source_file=file.filename
)
db.add(transaction)
db.commit()# Using uvicorn directly
uvicorn apis:app --reload --host 0.0.0.0 --port 8000
# Or using poetry
poetry run uvicorn apis:app --reload
# Or run the Python file directly
python apis.pyThe API will be available at: http://localhost:8000
POST /upload- Upload and process file
FastAPI provides automatic interactive documentation:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
curl -X POST "http://localhost:8000/upload" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_file.csv"curl -X GET "http://localhost:8000/transactions"curl -X GET "http://localhost:8000/transactions/1"macro-analytics/
├── apis.py # Main FastAPI application
├── database.py # Database configuration and session management
├── models.py # SQLAlchemy models (Transaction table)
├── schemas.py # Pydantic schemas for validation
├── .env.example # Example environment variables
├── pyproject.toml # Poetry dependencies
└── README.md # This file
-
Update Transaction Schema
- Edit
models.pyto add your transaction fields - Edit
schemas.pyto match your model
- Edit
-
Implement File Processing
- Edit the
upload_filefunction inapis.py - Add logic to parse your specific file format
- Map data to Transaction model
- Edit the
-
Add Custom Endpoints (optional)
- Filter transactions by date range
- Get analytics/statistics
- Bulk operations
- Export functionality
-
Security Enhancements (for production)
- Add authentication/authorization
- Implement rate limiting
- Add input validation
- Use environment variables for secrets
-
Error Handling
- Add comprehensive error handling
- Implement logging
- Add data validation
- The database tables are created automatically on application startup
- Use
--reloadflag during development for auto-reloading - Check the logs for database connection issues
- Test your API using the Swagger UI at
/docs