This is a RESTful API for managing a product catalog, designed to support an e-commerce platform. The API allows users to perform CRUD operations on products, organize them into categories, search for products, track inventory, and apply pricing and discounts.
- User authentication and authorization (JWT-based)
- CRUD operations for products and categories
- Role-based access control (Admin/Customer)
- Search and filtering functionality
- Product variants (e.g., size, color)
- Inventory tracking
- Error handling and validation
- Node.js
- Express.js
- MongoDB (with Mongoose ORM)
- JSON for data exchange
- Postman for API testing
- JWT for authentication
-
Clone this repository:
git clone https://github.com/ayadeleke/ProductCatalogAPI.git cd product-catalog-api -
Install dependencies: You can check package.json to see the list of dependencies to be installed.
npm install
-
Set up a
.envfile for environment variables:MONGO_URI=mongodb_connection_string PORT=3000 JWT_SECRET=jwt_secret_key
-
Start the server:
npx nodemon server.js // This will make the server.js to auto restart
The API will run on
http://127.0.0.1:3000.
-
Register a new user:
POST http://localhost:3000/auth/register
Request Body:
{ "name": "John Doe", "email": "john@example.com", "password": "securepassword", "role": "admin" } -
Login user:
POST http://localhost:3000/auth/login
Request Body:
{ "email": "john@example.com", "password": "securepassword" } -
Get all users (Admin Only):
GET http://localhost:3000/users Authorization: Bearer <jwt-token>
-
Create a product (Admin Only):
POST http://localhost:3000/products Content-Type: application/json Authorization: Bearer <jwt-token>
Request Body:
{ "name": "Nike Shoes", "description": "Some Nike shoes Descriptions", "category": "Shoes", "price": 99.99, "stock": 50, "image": "null", "variants": [ { "size": "M", "color": "Red", "quantity": 5 }, { "size": "L", "color": "Blue", "quantity": 10 } ] } -
Get all products:
GET http://localhost:3000/products
Response Body:
{
"name": "Nike Shoes",
"description": "Some Nike shoes Descriptions",
"category": "Shoes",
"price": 99.99,
"stock": 50,
"image": "null"
"variants": [
{ "size": "M", "color": "Red", "quantity": 5 },
{ "size": "L", "color": "Blue", "quantity": 10 }
]
}-
Get a specific product:
GET http://localhost:3000/products/:id
-
Update a product (Admin Only):
PUT http://localhost:3000/products/:id Content-Type: application/json Authorization: Bearer <jwt-token>
-
Delete a product (Admin Only):
DELETE http://localhost:3000/products/:id Authorization: Bearer <jwt-token>
-
Create a category (Admin Only):
POST http://localhost:3000/categories Content-Type: application/json Authorization: Bearer <jwt-token>
-
Get all categories:
GET http://localhost:3000/categories
-
Get a specific category:
GET http://localhost:3000/categories/:id
-
Update a category (Admin Only):
PUT http://localhost:3000/categories/:id Authorization: Bearer <jwt-token>
-
Delete a category (Admin Only):
DELETE http://localhost:3000/categories/:id Authorization: Bearer <jwt-token>
-
Search by name or description:
GET http://localhost:3000/products?search=nike
-
Filter by price range:
GET http://localhost:3000/products?minPrice=50&maxPrice=200
-
Filter by stock availability (admin):
GET http://localhost:3000/products?inStock=true
The API returns structured error messages. Example:
{
"error": "Product not found",
"error": "Product Exist variant and stock updated"
}- Use Postman or cURL for API testing
- e.g. cURL -X GET http://localhost:3000/categories,
- cURL -X GET http://localhost:3000/products
- npm test