A simple RESTful API for a To-Do application built with Flask, featured on my blog.
Please, Read the article: Create a Flask API with Python - Blog AstronautMarkus
- Features
- Requirements
- Installation
- Configuration
- Database Setup
- Running the Application
- API Endpoints
- Usage
- License
- JWT Authentication
- CRUD operations for to-do items
- Input validation & error handling
- MySQL database integration (Flask-SQLAlchemy)
- CORS support
- Environment variable management (
python-dotenv) - Gunicorn for production deployment
- SMTP server integration for email functionality
- Python 3.12+
- MySQL database
- pip (Python package installer)
- virtualenv (recommended)
- SMTP server
-
Clone the repository
git clone https://github.com/astronautmarkus/flask-api-demo-blog.git cd flask-api-demo-blog -
Create and activate a virtual environment (recommended)
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
Create a .env file in the root directory with the following variables:
SECRET_KEY=mysecretkey
DB_HOST=localhost
DB_PORT=3306
DB_USER=user
DB_PASSWORD=password
DB_NAME=flask_api_db
JWT_SECRET_KEY=myjwtsecretkey
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_email_password
MAIL_USE_TLS=True
MAIL_USE_SSL=FalseInitialize the database:
python app/scripts/create_db.py --reset-
Development mode
python app.py
-
Production mode (Gunicorn)
gunicorn -w 4 -b 0.0.0.0:8000 wsgi:app
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /auth/register |
Register a new user | No |
| POST | /auth/login |
Login and obtain a JWT token | No |
| GET | /tasks |
Get all to-do items | No |
| POST | /tasks |
Create a new to-do item | Yes |
| GET | /tasks/<id> |
Get a specific to-do item by ID | No |
| PUT | /tasks/<id> |
Update a specific to-do item by ID | No |
| DELETE | /tasks/<id> |
Delete a specific to-do item by ID | No |
| GET | /users |
Get all users (admin only) | No |
| GET | /users/<id> |
Get a specific user by ID (admin only) | Yes |
| PUT | /users/<id> |
Update a specific user by ID (admin only) | No |
| DELETE | /users/<id> |
Delete a specific user by ID (admin only) | No |
Note: I know it's inconsistent to put Auth only on some things, instead of protecting everything, but in the Blog it's only put on some to explain how the middleware that protects them works, thank you for your understanding.
Access the API using tools like Postman or curl.
- Base URL:
- Development:
http://localhost:5000 - Production:
http://0.0.0.0:8000
- Development:
MIT License. See LICENSE for details.