A production-ready asynchronous REST API built with FastAPI, featuring CRUD operations for notes management. The API includes advanced features like search, filtering, pagination, and is fully containerized with Docker.
- π Asynchronous API - Built with FastAPI and async/await patterns
- π PostgreSQL Database - Production-grade database with asyncpg driver
- π Search & Filter - Unified search with pagination support
- π CRUD Operations - Complete Create, Read, Update, Delete functionality
- π³ Docker Support - Fully containerized with Docker Compose
- π§ͺ Testing - Comprehensive test suite with pytest
- π API Documentation - Auto-generated OpenAPI/Swagger docs
- π¨ Vue Frontend - Optional modern frontend with Vite
- Quick Start
- Local Installation
- Vue Frontend
- API Endpoints
- Testing
- Documentation
- Contributing
- Troubleshooting
The fastest way to get started is using Docker Compose:
- Docker (20.10+)
- Docker Compose (v2.0+)
-
Clone the repository
git clone https://github.com/KenMwaura1/Fast-Api-example.git cd Fast-Api-example -
Start the application
docker-compose up -d --build
-
Access the API
- API: http://localhost:8002/notes
- Swagger Docs: http://localhost:8002/docs
- ReDoc: http://localhost:8002/redoc
Note: If you've previously run Docker Compose, reset the database volume:
docker-compose down -v && docker-compose up -d --build
- Python 3.13+
- PostgreSQL 12+
- pip or uv package manager
-
Clone the repository
git clone https://github.com/KenMwaura1/Fast-Api-example.git cd Fast-Api-example -
Create and activate virtual environment
python3 -m venv venv # Linux/Mac source venv/bin/activate # Windows .\venv\Scripts\activate
-
Install dependencies
cd src pip install -r requirements.txt -
Configure database
Create a PostgreSQL database:
CREATE DATABASE fast_api_dev; CREATE USER hello_fastapi WITH PASSWORD 'your_password'; GRANT ALL PRIVILEGES ON DATABASE fast_api_dev TO hello_fastapi;
Or update the
DATABASE_URLinsrc/app/.env:DATABASE_URL=postgresql://user:password@localhost/dbname
-
Run the application
cd .. ./run.sh -
Access the API
- API: http://localhost:8002/notes
- Swagger Docs: http://localhost:8002/docs
A modern Vue 3 frontend built with Vite is included for testing the API.
- Node.js 16+ or 18+ (LTS recommended)
- npm or yarn
-
Navigate to frontend directory
cd vue-client -
Install dependencies
npm install # or yarn install -
Start development server
npm run dev # or yarn dev -
Access the frontend
- Frontend: http://localhost:5173
The frontend displays notes with their completion status and formatted creation dates.
| Method | Endpoint | Description | Query Parameters |
|---|---|---|---|
| GET | /notes |
List all notes | skip, limit, search, completed |
| POST | /notes |
Create a note | - |
| GET | /notes/{id} |
Get a note | - |
| PUT | /notes/{id} |
Update a note | - |
| DELETE | /notes/{id} |
Delete a note | - |
| Method | Endpoint | Description |
|---|---|---|
| GET | /ping |
Health check |
# Get all completed notes with pagination
curl "http://localhost:8002/notes?completed=true&skip=0&limit=10"
# Search notes
curl "http://localhost:8002/notes?search=fastapi"
## π§ͺ Testing
The project includes a comprehensive test suite using pytest with database mocking.
### Run Tests
```bash
# From the project root
export PYTHONPATH=$PYTHONPATH:$(pwd)/src
./venv/bin/python -m pytest src
# With coverage
./venv/bin/python -m pytest src --cov=src --cov-report=html
# Run specific test file
./venv/bin/python -m pytest src/tests/test_notes.pytests/test_ping.py- Health check endpoint teststests/test_notes.py- CRUD operations and filtering teststests/conftest.py- Shared fixtures and test configuration
- Swagger UI: http://localhost:8002/docs (Interactive API documentation)
- ReDoc: http://localhost:8002/redoc (Alternative documentation view)
- Framework: FastAPI 0.115.8
- Python: 3.13
- Database: PostgreSQL with asyncpg
- ORM: SQLAlchemy 1.4.50
- Server: Uvicorn with uvloop
- Testing: pytest 7.4.3
- Frontend: Vue 3 + Vite
Fast-Api-example/
βββ src/
β βββ app/
β β βββ api/ # API routes
β β β βββ notes.py # Notes endpoints
β β β βββ ping.py # Health check
β β β βββ crud.py # Database operations
β β β βββ models.py # Pydantic models
β β βββ db.py # Database configuration
β β βββ main.py # Application entry point
β βββ tests/ # Test suite
β βββ Dockerfile # Docker configuration
β βββ requirements.txt # Python dependencies
βββ vue-client/ # Vue frontend
βββ docker-compose.yml # Docker Compose config
βββ run.sh # Local run script
Create a .env file in src/app/:
DATABASE_URL=postgresql://hello_fastapi:password@localhost/fast_api_dev
ENVIRONMENT=developmentContributions are welcome! Please follow these steps:
- 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
- Follow PEP 8 style guide
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting PR
The project uses GitHub Actions for CI/CD with two main workflows:
- CodeQL Analysis - Security scanning and code quality checks
- Docker Build & Push - Automated Docker image builds
Pre-built Docker images are available at:
To use GitHub Actions, add these secrets to your repository:
For Docker Hub:
DOCKER_USERNAME- Your Docker Hub usernameDOCKER_PASSWORD- Your Docker Hub password or access token
For GitHub Packages:
CR_PAT- Personal Access Token withwrite:packagesscopeCR_USERNAME- Your GitHub username
Note: You can remove or comment out the Docker push steps if you don't need image publishing.
docker pull kenmwaura1/fast-api-example:latest- Official Tutorial - Complete step-by-step guide
- API Documentation - Comprehensive API endpoint reference
- Development Guide - Setup and development workflow
- Contributing Guide - Guidelines for contributing
- FastAPI Documentation
- Docker Documentation
If you encounter errors installing packages (especially pydantic-core or greenlet):
-
Ensure you have Python 3.13 or 3.12 (not 3.14+, which has compatibility issues)
python --version
-
Install build dependencies (Linux/Mac):
# Ubuntu/Debian sudo apt-get install build-essential libssl-dev libffi-dev python3-dev # Mac brew install build-essential openssl
-
Upgrade pip, setuptools, and wheel:
pip install --upgrade pip setuptools wheel
-
Verify PostgreSQL is running:
# Check status sudo systemctl status postgresql # Start if not running sudo systemctl start postgresql
-
Verify credentials in
.env:# Check database exists psql -U hello_fastapi -d fast_api_dev -c "SELECT 1"
-
Check database connection string:
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
If using Docker Compose:
# Check if database container is running
docker-compose ps
# View database logs
docker-compose logs db
# Restart database
docker-compose restart db# Find process using port 8000
lsof -i :8000
# Kill the process (replace PID with actual process ID)
kill -9 <PID>
# Or use a different port
PORT=8001 ./run.sh-
Ensure you're using Python 3.13:
- Check
src/Dockerfileline 2
- Check
-
Clear Docker cache:
docker system prune -a docker-compose down -v
-
Rebuild:
docker-compose up -d --build
# Check container logs
docker-compose logs web
# Or for specific container
docker logs <container_id>The API might not be running at the expected URL:
- Verify the port: Check
./run.shfor PORT setting - Verify the host: Usually
http://localhost:8000 - Check that the API actually started without errors
Usually means your request data doesn't match expected format:
- Check field lengths: title (3-255), description (3-1000)
- Ensure required fields are included
- Verify JSON format is correct
Check the application logs:
# For local development
# Errors should show in the terminal where you ran ./run.sh
# For Docker
docker-compose logs web# Set PYTHONPATH correctly
export PYTHONPATH=$PYTHONPATH:$(pwd)/src
pytest src -vTests use mocked database, so this usually means the test fixture isn't working:
# Check conftest.py exists
cat src/tests/conftest.py
# Run with verbose output
pytest src -v -s-
Check database performance:
-- Log in to database psql -U hello_fastapi -d fast_api_dev -- Check table size SELECT pg_size_pretty(pg_total_relation_size('notes'));
-
Check query logs (if enabled)
-
Verify indexes exist on frequently queried columns
- Reduce
limitparameter in queries (max is 100) - Check for memory leaks in custom code
- Monitor using
docker stats(for Docker deployment)
-
Verify API is running:
curl http://localhost:8000/ping
-
Check CORS configuration in
.env:ALLOWED_ORIGINS=http://localhost:5173,http://localhost
-
Check frontend URL in browser matches one of allowed origins
-
Check the documentation:
- API.md - API reference
- DEVELOPMENT.md - Development guide
-
Search existing issues:
-
Open a new issue with:
- Clear description of the problem
- Steps to reproduce
- Error messages and logs
- Environment details (OS, Python version, etc.)
-
Ask for help:
- GitHub Discussions
- Stack Overflow (tag with
fastapi,postgresql)
This project is licensed under the MIT License.
Kennedy Mwaura
- Twitter: @Ken_Mwaura1
- GitHub: @KenMwaura1
If you find this project helpful, consider:
Built with β€οΈ using FastAPI

