A professional REST API built with Flask, following best practices of modular architecture, clean architecture, and design patterns. This project serves as a template for building scalable and maintainable APIs.
- β Modular architecture (Blueprints) for maximum scalability
- β Layered separation: Controller, Service, Repository, Model
- β Data validation with Marshmallow
- β Database migrations with Flask-Migrate
- β Environment variables with python-dotenv
- β Correct HTTP verbs implementation (GET, POST, PUT, PATCH, DELETE)
- β Global error handling
- β Production-ready structure
- Python 3.10+
- pip (Python package manager)
- virtualenv (recommended)
- Git
- Flask - Web framework
- Flask-SQLAlchemy - Database ORM
- Flask-Migrate - Database migrations
- Flask-Marshmallow - Serialization and validation
- python-dotenv - Environment variables
- SQLite (development) / PostgreSQL (production)
my_api_flask/
βββ .env # Environment variables
βββ config.py # Centralized configuration
βββ run.py # Entry point
βββ requirements.txt # Dependencies
βββ .gitignore
βββ src/
βββ __init__.py # Application factory
βββ extensions.py # Flask extensions
βββ utils/ # Flask extensions
βββ modules/ # Feature-based modules
βββ users/ # Users module
βββ __init__.py
βββ controller.py # Routes (Blueprints)
βββ service.py # Business logic
βββ model.py # Data model
βββ schema.py # Marshmallow validation
βββ repository.py # Data access layer
# ssh
git clone git@github.com:alexdevzz/flask-basic-rest-api.git
# or https
git clone https://github.com/alexdevzz/flask-basic-rest-api.git
cd your-repository# On macOS/Linux
python -m venv .venv
source venv/bin/activate
# On Windows
python -m venv .venv
venv\Scripts\activatepip install -r requirements.txtCreate a .env file in the project root:
# Flask configuration
FLASK_APP=run.py
FLASK_ENV=development
FLASK_DEBUG=1
SECRET_KEY=your-super-secret-key-change-in-production
# Database configuration
DATABASE_URL=sqlite:///app.db
# For PostgreSQL:
# DATABASE_URL=postgresql://username:password@localhost/my_db
# API configuration
API_TITLE="Rest API Example"
API_VERSION=v1# Initialize migrations (first time only)
flask db init
# Create the first migration
flask db migrate -m "Initial migration: create users table"
# Apply the migration
flask db upgrade# Method 1: Using flask run
flask run
# Method 2: Running run.py
python run.pyThe API will be available at http://localhost:5000
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/health |
API health check |
| GET | /api/v1/users/ |
Get all users |
| GET | /api/v1/users/{id} |
Get user by ID |
| POST | /api/v1/users/ |
Create a new user |
| PUT | /api/v1/users/{id} |
Completely replace a user |
| PATCH | /api/v1/users/{id} |
Partially update a user |
| DELETE | /api/v1/users/{id} |
Delete a user (soft delete) |
curl http://localhost:5000/api/v1/healthcurl -X POST http://localhost:5000/api/v1/users/ \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@email.com",
"age": 30
}'curl -X PUT http://localhost:5000/api/v1/users/1 \
-H "Content-Type: application/json" \
-d '{
"name": "John Carlos Doe",
"email": "johncarlos@email.com",
"age": 31
}'curl -X PATCH http://localhost:5000/api/v1/users/1 \
-H "Content-Type: application/json" \
-d '{
"name": "John Carlos"
}'Client (HTTP Request)
β
[Controller] - Handles HTTP requests, initial validation
β
[Service] - Business logic, application rules
β
[Repository] - Data access, database queries
β
[Model] - Data representation (SQLAlchemy)
β
Database
- Controller: Receives the request, extracts parameters, performs basic validation
- Service: Applies business logic (checking duplicate emails, permissions, etc.)
- Repository: Executes database queries
- Model: Defines the data structure
- Response: Controller returns JSON to the client
To run tests (when implemented):
pytest tests/- Change
FLASK_ENV=productionin.env - Use a robust database (PostgreSQL recommended)
- Set a secure
SECRET_KEY - Use Gunicorn or uWSGI as WSGI server
- Configure Nginx as reverse proxy
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:8000 "run:app"Contributions are welcome. Please:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Alexdevzz - Software Engineer and Backend Developer - Go to my GitHub
- To the Flask community for excellent tools
- To SQLAlchemy and Marshmallow contributors
If you have questions or suggestions, feel free to contact me:
- Email: alexdev.workenv@gmail.com
- LinkedIn: alexdevzz
β If this project was helpful to you, don't forget to give it a star on GitHub β
# Installation
python -m venv venv
source venv/bin/activate # (macOS/Linux)
pip install -r requirements.txt
# Database migrations
flask db init
flask db migrate -m "message"
flask db upgrade
flask db downgrade
python setup_db.py --init
# Run
flask run
python run.py
# Check endpoints
curl http://localhost:5000/api/health