API Documentation

Complete reference for the Grocery Management System REST API

Introduction

The Grocery Management System provides a RESTful API for programmatic access to all system functionality. All API endpoints require authentication except for the login and registration endpoints.

Authentication

Most endpoints require a JWT token for authentication. Include the token in the Authorization header:

Authorization: Bearer YOUR_JWT_TOKEN

Authentication

POST /register
Register new user

Create a new user account with username and password.

Request Body
{
    "username": "newuser",
    "password": "securepassword123"
}
Response (Success)
{
    "user_id": 42,
    "message": "User registered successfully"
}
Possible Errors
{
    "error": "Username and password are required"
} (Status: 400)
POST /login
User login

Authenticate and receive a JWT token valid for 2 hours.

Request Body
{
    "username": "existinguser",
    "password": "securepassword123"
}
Response (Success)
{
    "message": "Login successful",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user_id": 42
}
Possible Errors
{
    "error": "Username and password are required"
} (Status: 400)

{
    "error": "Invalid username or password"
} (Status: 401)
Note: Include this token in the Authorization header for protected routes:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

Products

GET /getProducts
Get all products

Retrieve a list of all products in the inventory.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Response (Success)
[
{
      "name": "rice",
       "price_per_unit": 40,
      "product_id": 1,
      "uom_id": 2,
      "uom_name": "kg"
},
{
      "name": "toothpaste",
      "price_per_unit": 30,
      "product_id": 2,
      "uom_id": 1,
      "uom_name": "each"
},
{
      "name": "Wheat",
      "price_per_unit": 40,
      "product_id": 3,
      "uom_id": 2,
      "uom_name": "kg"
}               
]
POST /insertProduct
Add new product

Add a new product to the inventory.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: multipart/form-data
Request Body (form-data)
data={
            "product_name": "Bananas",
            "unit": "kg",
            "price_per_unit": 35
            }
Response (Success)
{
    "product_id": 42
}
POST /deleteProduct
Remove product

Remove a product from the inventory.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: multipart/form-data
Request Body (form-data)
{
    "product_id": "42"
}
Response (Success)
{
    "product_id": 42
}

Orders

GET /getAllOrders
Get all orders

Retrieve a list of all orders with their details.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Response (Success)

[
"customer_name": "Sneha",
{
        "datetime": "Wed, 02 Apr 2025 00:00:00 GMT",
         "order_details": [
            {
                "order_id": 1,
                "price_per_unit": 40,
                "product_name": "rice",
                "quantity": 2,
                "total_price": 80
            }
            ],
        "order_id": 1,
        "total": 300
        },
{
"customer_name": "Tanya",
        "datetime": "Wed, 02 Apr 2025 11:12:17 GMT",
        "order_details": [
            {
                "order_id": 2,
                "price_per_unit": 30,
                "product_name": "toothpaste",
                "quantity": 1,
                "total_price": 30
        }
          ],
        "order_id": 2,
        "total": 30
        },
{
"customer_name": "Aditya",
        "datetime": "Wed, 02 Apr 2025 07:51:37 GMT",
        "order_details": [
            {
                  "order_id": 3,
                  "price_per_unit": 40,
                  "product_name": "Wheat",
                  "quantity": 1,
                  "total_price": 40
            }
              ],
        "order_id": 3,
        "total": 40
        }   
]
        
POST /insertOrder
Create new order

Create a new order with multiple products.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: multipart/form-data
Request Body (form-data)
{
        "product": "Wheat"
        "price": 40,
        "quantity": 1,
        "total-cost": 230.00 Rs 
}
Response (Success)
{
    "order_id": 102
}

Units of Measure

GET /getUOM
Get all units of measure

Retrieve a list of all available units of measure.

Headers
Authorization: Bearer YOUR_JWT_TOKEN
Response (Success)
[
    {
        "uom_id": 1,
        "uom_name": "each"
    },
    {
        "uom_id": 2,
        "uom_name": "kg"
    }
]