# Product Inventory API Documentation ## Base URL ``` http://localhost:8000/api ``` ## Headers All requests should include: ``` Content-Type: application/json Accept: application/json ``` --- ## Endpoints ### 1. Get All Products **GET** `/products` **Description:** Retrieve a list of all products in the inventory. **Response:** ```json { "data": [ { "id": 1, "name": "Wireless Headphones", "description": "High-quality wireless headphones with noise cancellation", "sku": "WH-001", "price": "199.99", "stock_quantity": 50, "image_url": "https://example.com/images/headphones.jpg", "is_active": true, "created_at": "2025-08-04T10:30:00.000000Z", "updated_at": "2025-08-04T10:30:00.000000Z" } ], "message": "Products retrieved successfully" } ``` **JavaScript Example:** ```javascript const response = await fetch("http://localhost:8000/api/products", { method: "GET", headers: { "Content-Type": "application/json", Accept: "application/json", }, }); const data = await response.json(); console.log(data.data); // Array of products ``` --- ### 2. Get Product by SKU **GET** `/products/{sku}` **Description:** Retrieve a specific product using its SKU. **Parameters:** - `sku` (string) - The unique SKU of the product **Response:** ```json { "data": { "id": 1, "name": "Wireless Headphones", "description": "High-quality wireless headphones with noise cancellation", "sku": "WH-001", "price": "199.99", "stock_quantity": 50, "image_url": "https://example.com/images/headphones.jpg", "is_active": true, "created_at": "2025-08-04T10:30:00.000000Z", "updated_at": "2025-08-04T10:30:00.000000Z" }, "message": "Product retrieved successfully" } ``` **JavaScript Example:** ```javascript const sku = "WH-001"; const response = await fetch(`http://localhost:8000/api/products/${sku}`, { method: "GET", headers: { "Content-Type": "application/json", Accept: "application/json", }, }); const data = await response.json(); console.log(data.data); // Single product object ``` --- ### 3. Create New Product **POST** `/products` **Description:** Create a new product in the inventory. **Request Body:** ```json { "name": "Wireless Headphones", "description": "High-quality wireless headphones with noise cancellation", "sku": "WH-001", "price": 199.99, "stock_quantity": 50, "image_url": "https://example.com/images/headphones.jpg", "is_active": true } ``` **Required Fields:** - `name` (string, max: 255) - `sku` (string, max: 100, unique) - `price` (number, min: 0) - `stock_quantity` (integer, min: 0) **Optional Fields:** - `description` (string) - `image_url` (string, valid URL, max: 255) - `is_active` (boolean, default: true) **Response:** ```json { "data": { "id": 1, "name": "Wireless Headphones", "description": "High-quality wireless headphones with noise cancellation", "sku": "WH-001", "price": "199.99", "stock_quantity": 50, "image_url": "https://example.com/images/headphones.jpg", "is_active": true, "created_at": "2025-08-04T10:30:00.000000Z", "updated_at": "2025-08-04T10:30:00.000000Z" }, "message": "Product created successfully" } ``` **Status Code:** `201 Created` **JavaScript Example:** ```javascript const productData = { name: "Wireless Headphones", description: "High-quality wireless headphones with noise cancellation", sku: "WH-001", price: 199.99, stock_quantity: 50, image_url: "https://example.com/images/headphones.jpg", is_active: true, }; const response = await fetch("http://localhost:8000/api/products", { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify(productData), }); const data = await response.json(); if (response.ok) { console.log("Product created:", data.data); } else { console.error("Error:", data); } ``` --- ### 4. Update Product **PUT** `/products/{sku}` **Description:** Update an existing product by its SKU. **Parameters:** - `sku` (string) - The unique SKU of the product to update **Request Body (Partial Update):** ```json { "name": "Updated Wireless Headphones Pro", "price": 249.99, "stock_quantity": 75 } ``` **Validation Rules:** - `name` (string, max: 255) - optional for updates - `sku` (string, max: 100, unique) - optional for updates - `price` (number, min: 0) - optional for updates - `stock_quantity` (integer, min: 0) - optional for updates - `description` (string) - optional - `image_url` (string, valid URL, max: 255) - optional - `is_active` (boolean) - optional **Response:** ```json { "data": { "id": 1, "name": "Updated Wireless Headphones Pro", "description": "High-quality wireless headphones with noise cancellation", "sku": "WH-001", "price": "249.99", "stock_quantity": 75, "image_url": "https://example.com/images/headphones.jpg", "is_active": true, "created_at": "2025-08-04T10:30:00.000000Z", "updated_at": "2025-08-04T11:45:00.000000Z" }, "message": "Product updated successfully" } ``` **JavaScript Example:** ```javascript const sku = "WH-001"; const updateData = { name: "Updated Wireless Headphones Pro", price: 249.99, stock_quantity: 75, }; const response = await fetch(`http://localhost:8000/api/products/${sku}`, { method: "PUT", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify(updateData), }); const data = await response.json(); if (response.ok) { console.log("Product updated:", data.data); } else { console.error("Error:", data); } ``` --- ### 5. Delete Product **DELETE** `/products/{sku}` **Description:** Delete a product from the inventory by its SKU. **Parameters:** - `sku` (string) - The unique SKU of the product to delete **Response:** ```json { "message": "Product deleted successfully" } ``` **JavaScript Example:** ```javascript const sku = "WH-001"; const response = await fetch(`http://localhost:8000/api/products/${sku}`, { method: "DELETE", headers: { "Content-Type": "application/json", Accept: "application/json", }, }); const data = await response.json(); if (response.ok) { console.log("Product deleted:", data.message); } else { console.error("Error:", data); } ``` --- ## Error Responses ### Validation Errors (422) ```json { "message": "The given data was invalid.", "errors": { "name": ["The name field is required."], "sku": ["The sku has already been taken."], "price": ["The price must be a number."] } } ``` ### Not Found (404) ```json { "message": "No query results for model [App\\Models\\Product] WH-999" } ``` ### Server Error (500) ```json { "message": "Server Error" } ``` --- ## React Component Examples ### Product List Component ```jsx import React, { useState, useEffect } from "react"; function ProductList() { const [products, setProducts] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { fetchProducts(); }, []); const fetchProducts = async () => { try { const response = await fetch("http://localhost:8000/api/products"); const data = await response.json(); setProducts(data.data); } catch (error) { console.error("Error fetching products:", error); } finally { setLoading(false); } }; if (loading) return
SKU: {product.sku}
Price: ${product.price}
Stock: {product.stock_quantity}