RESTful HTTP API using Python Flask that allows users to manage their ecommerce platform.
Ability to create, read, update, and delete products, categories and subcategories. A category can have multiple subcategories and a subcategory can belong to multiple categories. Products can belong to multiple categories and subcategories.
Fetching a product fetches the details of categories and subcategories it belongs to. Provides the ability to search for products by name, category and subcategories.
Paginates result using cursor based pagination when products are fetched by categories, subcategories or themselves.
Deployed as a vercel function with Postgres: ecommerce-rest-api-five.vercel.app
Documented with Swagger UI.
This project is written in Python 3.12.1
pip install -r requirements.txtrequirements.txt contains an adapter for PostgreSQL by default.
Copy .env.example and rename to .env. Provide your database URL to the SQLALCHEMY_DATABASE_URI environment variable.
Create database tables:
flask db upgrade head(Optional) Populate database with fake data :
pip install -r requirements-dev.txt
python populate_db.pySet JWT_SECRET_KEY environment variable. Run this in a python shell to generate sample keys:
import secrets
secrets.token_urlsafe(32) # 'fP-3vOuhEr7Nl9DdJiX5XyjOedquOrifDps2KS34Wu0'Start the server: (Runs on 127.0.0.1:5000)
flask --app app run [--debug]Test the API using Swagger UI (/ route), Postman, cURL or your preferred HTTP client.
- [GET]
/products?name=<name: string>- Get product with name:name - [GET]
/subcategories/<subcategory_id: int>/products- Get first page of products within subcategorysubcategory_id. - [GET]
/subcategories/<subcategory_id: int>/products?cursor=<cursor: str>- Get products paginated using cursor within subcategorysubcategory_id. Next and previous pagecursorsprovided in responses. - [GET]
/categories/<category_id: int>/products- Get first page of products within categorycategory_id. - [GET]
/categories/<category_id: int>/products?cursor=<cursor: str>- Get products paginated using cursor within categorycategory_id. Next and previous pagecursorsprovided in responses.
Protected endpoints require the following header:
Authorization: Bearer <access_token>
Refresh protected endpoints requires the following header:
Authorization: Bearer <refresh_token>
-
[POST]
/auth/register- Register a new user.{ "email": "user@example.com", "password": "your_password" } -
[POST]
/auth/login- Login a user and get access and refresh tokens.{ "email": "user@example.com", "password": "your_password" } -
[POST]
/auth/refresh(Refresh protected) - Get new access token using a refresh token.
-
[GET]
/categories- Get all categories -
[GET]
/categories/(int: category_id)- Get category with category_id -
[GET]
/categories/(int: category_id)/subcategories- Get subcategories within a category_id. -
[DELETE]
/categories/(int: category_id)(Protected) - Delete category with category_id -
[POST]
/categories(Protected) - Create a new category{ "name": "name", "subcategories": [<subcategory ids>] //optional } -
[PUT]
/categories/(int: category_id)(Protected) - Update category with category_id{ "name": "name", "subcategories": [<subcategory ids>] //optional }
-
[GET]
/subcategories- Get all subcategories -
[GET]
/subcategories/(int: subcategory_id)- Get subcategory with subcategory_id -
[GET]
/subcategories/(int: subcategory_id)/categories- Get categories related to subcategory_id -
[DELETE]
/subcategories/(int: subcategory_id)(Protected) - Delete subcategory with subcategory_id -
[POST]
/subcategories(Protected) - Create a new subcategory{ "name": "name", "categories": [(category ids)], //optional "products": [<product ids>] // optional } -
[PUT]
/subcategories/(int: subcategory_id)(Protected) - Update subcategory with subcategory_id{ "name": "name", "categories": [<category ids>], //optional "products": [<product ids>] // optional }
-
[GET]
/products- Get first page of products -
[GET]
/products?cursor=<cursor: str>- Get products paginated using cursor. Next and previous pagecursorsprovided in responses. -
[GET]
/products/(int: product_id)- Get product with product_id -
[GET]
/products/(int: product_id)/subcategories- Get subcategories related to product_id -
[DELETE]
/products/(int: product_id)(Protected) - Delete product with product_id -
[POST]
/products(Protected) - Create a new product{ "name": "name", "description": "description", "subcategories": [<subcategory ids>] //optional } -
[PUT]
/products/(int: product_id)(Protected) - Update product with product_id{ "name": "name", "description": "description", "subcategories": [<subcategory ids>] //optional }