Coders Packet

E-commerce backend API with Express and MongoDB tested with Postman

By Abhishek Malviya

Restful API interface for an e-commerce website with JWT Authentication build with Express and MongoDB and tested with Postman

E-commerce Rest API

Features

- > Restful interface for seamless API calls 
->  Meaningful error messages for bugs 
- > Simple file structure for extending and managing API 
- > Validation and Permission with JWT 
 

Tech

-> Node.js
-> Express
-> MongoDB

Installation

Prerequisite NodeJS, MongoDB, NPM or YARN

First setup .env file

PORT = 
JWT_SECRET = 
CUSTOMER = 1
SELLER = 4
ADMIN = 2048
DB_NAME = 

 

 

Install the dependencies and devDependencies and start the server.

cd e-commerce-rest-api
npm i
npm run start

or

cd e-commerce-rest-api
yarn add
yarrn start

 

For development environments

npm run dev

or

yarn dev

 

API document

Permission level specifies the control that a user has on API as a seller can only post products on the API

 

User methods

METHOD ROUTE
POST /users
GET /users/:userId
PATCH /users/:userId
DELETE /users/:userId

Post method "/users"

Sample request

{
    "firstName": "name",
    "lastName": "Surname",
    "email":"[email protected]",
    "password": "password_password"
    "permissionLevel": 1                   // optional
}

Sample response

{
    "id": "60e425d3d90156527f3431a1"
}

GET method "/users/:userId"

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"
}

Authentication methods

METHOD ROUTE
POST /auth
POST /auth/refresh

Post method "/auth"

Sample request

{
    "email":"[email protected]",
    "password": "password_password"
}

Sample response

{
    "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MGUxNWMxZTExYTFiNjFmOGE4ODdjNGEiLCJlbWFpbCI6InF3ZUBxd2UuY29tIiwicGVybWlzc2lvbkxldmVsIjoxLCJwcm92aWRlciI6ImVtYWlsIiwibmFtZSI6IkpvbWEgSmFrZSIsInJlZnJlc2hLZXkiOiJETFNjNmSzlWOFczYXFya0Naa3pRPT0iLCJpYXQiOjE2MjU1ODkxNjl9.P7DxN1F1t34GV0IMNfM1nHWygFIfLe1jCU9KjdMnq4k",
    "refreshToken": "Y1dlc0krVDMxdGlCVk1rQ09nZVFRbmVVdEJhR2pNTGl3pGUUszNFJBNm5maE9jVk5JTUp3cE9sNXl0QzQvQXFyNmFZbVFlQnp1RFJka0QwVGNDREE9PQ=="
}

GET method "/auth/refresh"

Need JWT token as a bearer

 

This API is created around selling books, so this following method is called books, but it can be used for all kinds of products.

Book methods

METHOD ROUTE
GET /books
POST /books
GET /books/:bookId
PATCH /books/:bookId
DELETE /books/:bookId

GET method "/books"

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"
    }
]

POST method "/books"

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"
}

GET method "/books/:bookId

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"
}

Order methods

METHOD ROUTE
POST /orders
POST /orders/:orderId

POST orders

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"
    ]
}

Download project

Reviews Report

Submitted by Abhishek Malviya (AbhishekM)

Download packets of source code on Coders Packet