A RESTful API built with Flask and SQLAlchemy for managing users, products, and orders with a MySQL database.
- User Management: Create, read, update, and delete users
- Product Catalog: Manage products with pricing
- Order Management: Create orders, add/remove products, track order history
- Data Validation: Input validation using Marshmallow schemas
- CORS Enabled: Cross-origin requests supported
- Environment Variables: Secure credential management with
.env
- Python 3.7+
- MySQL Server
- Virtual environment (recommended)
cd my-ecommerce-apipython3 -m venv venvMac/Linux:
source venv/bin/activateWindows:
venv\Scripts\activatepip install -r requirements.txtcp .env.example .envEdit .env and add your MySQL credentials:
DATABASE_URL=mysql+mysqlconnector://root:YOUR_PASSWORD@localhost:3306/ecommerce_api
FLASK_ENV=development
In MySQL Workbench or command line:
CREATE DATABASE IF NOT EXISTS ecommerce_api;python app.pyYou should see:
* Running on http://127.0.0.1:5000
The application will automatically create all tables on startup.
id (Integer, Primary Key, Auto-increment)
name (String, 100 chars, Required)
address (String, 255 chars, Optional)
email (String, 100 chars, Unique, Required)
id (Integer, Primary Key, Auto-increment)
product_name (String, 100 chars, Required)
price (Float, Required)
id (Integer, Primary Key, Auto-increment)
order_date (DateTime, Default: Current UTC Time, Required)
user_id (Integer, Foreign Key → User, Required)
order_id (Integer, Primary Key, Foreign Key → Order)
product_id (Integer, Primary Key, Foreign Key → Product)
unique_order_product (Unique Constraint: prevents duplicate products in same order)
Retrieve all users.
Request:
GET http://localhost:5000/users
Response (200 OK):
[
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"address": "123 Main St"
},
{
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com",
"address": "456 Oak Ave"
}
]