A robust RESTful API for Todo List management built with Flask, SQLAlchemy, and Marshmallow. Features comprehensive CRUD operations, advanced search and filtering, secure environment management, and extensive testing coverage.
- β CRUD Operations: Create, Read, Update, Delete todos
- π Search & Filter: Search by keyword, priority, status, and deadline
- β Data Validation: Comprehensive input validation with Marshmallow
- ποΈ Factory Pattern: Clean application structure with Flask factory pattern
- π API Documentation: Complete Postman collection for testing
- π Environment Configuration: Secure environment variable management
- Framework: Flask 3.0+
- Database: SQLAlchemy with SQLite
- Serialization: Marshmallow & Marshmallow-SQLAlchemy
- CORS: Flask-CORS for cross-origin requests
- Rate Limiting: Flask-Limiter for API rate limiting
- Environment: python-dotenv for configuration
- Testing: pytest with Flask-Testing
- Python 3.8+
- pip (Python package installer)
-
Clone the repository
git clone https://github.com/xdendix/todolist-backend-flask.git cd todolist-backend-flask -
Create virtual environment
python -m venv todolist_backend_venv source todolist_backend_venv/bin/activate # On Windows: todolist_backend_venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables securely
-
Copy the example environment file:
cp instance/.env.example instance/.env
-
Generate a secure SECRET_KEY:
python -c "import secrets; print(secrets.token_hex(32))" -
Edit
instance/.envand replace the SECRET_KEY value with the generated key. -
Important Security Notes:
- Never commit your
.envfile to version control. - The
.envfile is included in.gitignoreto prevent accidental commits. - Keep your SECRET_KEY and other sensitive data private.
- Use different keys for development and production environments.
- Never commit your
-
-
Run the application
python app.py
The API will be available at: http://127.0.0.1:5000
- GET
/health- Check API status
- GET
/api/todos/- Get all todos (with pagination)- Query parameters:
page(default: 1),per_page(default: 10, max: 100)
- Query parameters:
- POST
/api/todos/- Create new todo - GET
/api/todos/<id>- Get todo by ID - PUT
/api/todos/<id>- Update todo - DELETE
/api/todos/<id>- Delete todo
- GET
/api/todos/search- Search todos with filters- Query parameters:
q(keyword),prioritas,status,deadline,deadline_from,deadline_to,page,per_page
- Query parameters:
POST /api/todos/
Content-Type: application/json
{
"judul": "Belajar Flask",
"prioritas": "High",
"status": false,
"deadline": "2025-12-31"
}GET /api/todos/search?q=flask&prioritas=High&status=belum%20selesai&deadline_from=2025-01-01&deadline_to=2025-12-31id: Integer (Primary Key)judul: String (Required, Unique)status: Boolean (Default: false)prioritas: String (High/Medium/Low)deadline: Date (Optional)created_at: DateTime (Auto-generated)updated_at: DateTime (Auto-updated)
- judul: Cannot be empty or whitespace only
- prioritas: Must be "High", "Medium", or "Low" (case insensitive)
- deadline: Must be valid date format (YYYY-MM-DD)
- status: Boolean value
-
Import Collection
- Import
TodoList_API_Postman_Collection.jsoninto Postman
- Import
-
Setup Environment
- Create environment variable:
base_url=http://127.0.0.1:5000
- Create environment variable:
-
Start Testing
- Run requests in order or as needed
The project includes comprehensive unit and integration tests using pytest.
python run_tests.pypython run_tests.py test_create_todo_successpytest --cov=todo_app --cov-report=html- Unit Tests: Test individual functions and methods
- Integration Tests: Test API endpoints and database interactions
- Fixtures: Pre-configured test data and app instances
todolist-backend-flask/
βββ app.py # Application entry point
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ TODO.md # Development tasks
βββ POSTMAN_TESTING_GUIDE.md # Testing documentation
βββ TodoList_API_Postman_Collection.json # Postman collection
βββ Postman_Import_Instructions.md # Import guide
βββ instance/ # Instance-specific config
β βββ .env.example # Example environment variables file (do not commit)
β βββ .env # Local environment variables file (ignored by git)
βββ todo_app/ # Main application package
β βββ __init__.py # Flask app factory
β βββ extensions.py # Flask extensions
β βββ models.py # Database models
β βββ schemas.py # Marshmallow schemas
β βββ todos/ # Todos blueprint
β βββ routes.py # API routes
βββ todolist_backend_venv/ # Virtual environment
JANGAN pernah commit file .env ke version control!
-
Copy file template:
cp instance/.env.example instance/.env
-
Generate SECRET_KEY yang aman:
python -c "import secrets; print(secrets.token_hex(32))" -
Update file
.envdengan key yang di-generate
- β SECRET_KEY: Gunakan key random minimal 32 karakter
- β Environment Variables: Jangan hardcode sensitive data
- β .env file: Sudah di-ignore oleh .gitignore
- β Production: Gunakan key yang berbeda dari development
- β Version Control: Jangan commit file .env
- β File Permissions: Set restrictive permissions on .env file (chmod 600)
- β Regular Rotation: Rotate SECRET_KEY periodically
- β Environment Separation: Use different .env files for dev/staging/production
- β Logging: Never log sensitive environment variables
- β Backup: Don't include .env in backups or snapshots
- Type hints added for better IDE support
- Comprehensive docstrings
- Consistent error handling
- Input validation with Marshmallow
- SQLite for development (easy setup)
- SQLAlchemy for ORM
- Automatic table creation
- Foreign key relationships support
- RESTful endpoints
- JSON request/response
- Proper HTTP status codes
- Detailed error messages
- Use production-grade database (PostgreSQL, MySQL) instead of SQLite
- Set
FLASK_DEBUG=0in production - Use environment-specific configuration files
- Implement proper logging and monitoring
- Set up SSL/TLS certificates
- Configure CORS properly for your frontend domain
- Use a web server like Gunicorn or uWSGI
- Implement rate limiting
- Set up proper firewall rules
- Regular security updates
- Monitor for vulnerabilities
- Fork the repository
- Create feature branch
- Make changes
- Test thoroughly
- Submit pull request
This project is open source and available under the MIT License.
Happy Coding! π