Restful API interface for an e-commerce website with JWT Authentication build with Express and MongoDB and tested with Postman
Prerequisite NodeJS, MongoDB, NPM or YARN
PORT = JWT_SECRET = CUSTOMER = 1 SELLER = 4 ADMIN = 2048 DB_NAME =
cd e-commerce-rest-api npm i npm run start or cd e-commerce-rest-api yarn add yarrn start
npm run dev or yarn dev
METHOD | ROUTE |
---|---|
POST | /users |
GET | /users/:userId |
PATCH | /users/:userId |
DELETE | /users/:userId |
Sample request
{
"firstName": "name",
"lastName": "Surname",
"email":"[email protected]",
"password": "password_password"
"permissionLevel": 1 // optional
}
Sample response
{
"id": "60e425d3d90156527f3431a1"
}
Need JWT token as a bearer
localhost:3000/users/60e15c1e11a1b61f8a887c4a
Sample response
{
"firstName": "name",
"lastName": "surname",
"email": "[email protected]",
"permissionLevel": 1,
"createdAt": "2021-07-04T06:58:38.338Z",
"updatedAt": "2021-07-04T06:58:38.338Z",
"id": "60e15c1e11a1b61f8a887c4a"
}
METHOD | ROUTE |
---|---|
POST | /auth |
POST | /auth/refresh |
Sample request
{
"email":"[email protected]",
"password": "password_password"
}
Sample response
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MGUxNWMxZTExYTFiNjFmOGE4ODdjNGEiLCJlbWFpbCI6InF3ZUBxd2UuY29tIiwicGVybWlzc2lvbkxldmVsIjoxLCJwcm92aWRlciI6ImVtYWlsIiwibmFtZSI6IkpvbWEgSmFrZSIsInJlZnJlc2hLZXkiOiJETFNjNmSzlWOFczYXFya0Naa3pRPT0iLCJpYXQiOjE2MjU1ODkxNjl9.P7DxN1F1t34GV0IMNfM1nHWygFIfLe1jCU9KjdMnq4k",
"refreshToken": "Y1dlc0krVDMxdGlCVk1rQ09nZVFRbmVVdEJhR2pNTGl3pGUUszNFJBNm5maE9jVk5JTUp3cE9sNXl0QzQvQXFyNmFZbVFlQnp1RFJka0QwVGNDREE9PQ=="
}
Need JWT token as a bearer
METHOD | ROUTE |
---|---|
GET | /books |
POST | /books |
GET | /books/:bookId |
PATCH | /books/:bookId |
DELETE | /books/:bookId |
Sample request
URL => localhost:3000/books?limit=20&page=1 limit and page are optional.
Sample response
[
{
"tags": [
"partnerships",
"Multi-tiered",
"initiatives",
"Vision-oriented"
],
"name": "Fantastic Concrete Chair",
"image": "http://placeimg.com/640/480/nature",
"description": "Kenya Baby Falls Berkshire",
"price": 600,
"sellerId": "60e1efdf97eb4dd170399bc1",
"id": "60e206d4e21ae1ee9cead1bd"
},
{
"tags": [
"Generic",
"engineer",
"Illinois",
"calculating"
],
"name": "Practical Cotton Ball",
"image": "http://placeimg.com/640/480/nature",
"description": "Avon hacking",
"price": 466,
"sellerId": "60e1efdf97eb4dd170399bc1",
"id": "60e206d5e21ae1ee9cead1bf"
}
]
User of this method require permission level of 4 and Bearer token
Sample request
{
"name":"Fantastic Concrete Chair",
"image":"http://placeimg.com/640/480/nature",
"description":"Kenya Baby Falls Berkshire",
"price":"466",
"tags":[ "partnerships", "Multi-tiered", "initiatives", "Vision-oriented" ]
}
Sample response
{
"id": "60e48955d90156527f3431a6"
}
Return information about a particular book localhost:3000/book/60e48955d90156527f3431a6
Sample Response
{
"tags": [
"partnerships",
"Multi-tiered",
"initiatives",
"Vision-oriented"
],
"name": "Fantastic Concrete Chair",
"image": "http://placeimg.com/640/480/nature",
"description": "Kenya Baby Falls Berkshire",
"price": 600,
"sellerId": "60e1efdf97eb4dd170399bc1",
"id": "60e206d4e21ae1ee9cead1bd"
}
METHOD | ROUTE |
---|---|
POST | /orders |
POST | /orders/:orderId |
Sample order
{
"books": [
{
"qty": "1 ",
"bookId": "60e206dce21ae1ee9cead1cb"
},
{
"qty": "2",
"bookId": "60e2a432067b7e1ba5942bbc"
},
{
"qty": "3",
"bookId": "60e353cd76216bd3b8fb0649"
}
],
"phone":"0123456789",
"address":"stree one ",
"paymentType":"COD"
}
Sample response
{
"orderId": [
"60e491cbd90156527f3431ab",
"60e491cbd90156527f3431ae"
]
}
Submitted by Abhishek Malviya (AbhishekM)
Download packets of source code on Coders Packet
Comments