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 when products are fetched by categories or subcategories.
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.txt
requirements.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.py
Set 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]
/product/<name: string>
- Get product with name:name
- [GET]
/subcategory/<subcategory_id: int>/products?page=<page_no>
- Get product with within subcategorysubcategory
. Returnspage_no
of the paginated results. - [GET]
/category/<category_id: int>/products
- Get product with within categorycategory
. Returns first page of the paginated results. - [GET]
/category/<category_id: int>/products?page=<page_no>
- Get product with within categorycategory
. Returnspage_no
of the paginated results.
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]
/category/(int: category_id)
- Get category with category_id -
[GET]
/category/(int: category_id)/subcategories
- Get subcategories within a category_id. -
[DELETE]
/category/(int: category_id)
(Protected) - Delete category with category_id -
[POST]
/category/create
(Protected) - Create a new category{ "name": "name", "subcategories": [<subcategory ids>] //optional }
-
[PUT]
/category/(int: category_id)/update
(Protected) - Update category with category_id{ "name": "name", "subcategories": [<subcategory ids>] //optional }
-
[GET]
/subcategories
- Get all subcategories -
[GET]
/subcategory/(int: subcategory_id)
- Get subcategory with subcategory_id -
[GET]
/subcategory/(int: subcategory_id)/categories
- Get categories related to subcategory_id -
[DELETE]
/subcategory/(int: subcategory_id)
(Protected) - Delete subcategory with subcategory_id -
[POST]
/subcategory/create
(Protected) - Create a new subcategory{ "name": "name", "categories": [(category ids)], //optional "products": [<product ids>] // optional }
-
[PUT]
/subcategory/(int: subcategory_id)/update
(Protected) - Update subcategory with subcategory_id{ "name": "name", "categories": [<category ids>], //optional "products": [<product ids>] // optional }
-
[GET]
/products
- Get all products -
[GET]
/product/(int: product_id)
- Get product with product_id -
[GET]
/product/(int: product_id)/subcategories
- Get subcategories related to product_id -
[DELETE]
/product/(int: product_id)
(Protected) - Delete product with product_id -
[POST]
/product/create
(Protected) - Create a new product{ "name": "name", "description": "description", "subcategories": [<subcategory ids>] //optional }
-
[PUT]
/product/(int: product_id)/update
(Protected) - Update product with product_id{ "name": "name", "description": "description", "subcategories": [<subcategory ids>] //optional }