# 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
Loading...
; return (

Products

{products.map((product) => (

{product.name}

SKU: {product.sku}

Price: ${product.price}

Stock: {product.stock_quantity}

))}
); } export default ProductList; ``` ### Create Product Form ```jsx import React, { useState } from "react"; function CreateProduct() { const [formData, setFormData] = useState({ name: "", description: "", sku: "", price: "", stock_quantity: "", image_url: "", is_active: true, }); const handleSubmit = async (e) => { e.preventDefault(); try { const response = await fetch("http://localhost:8000/api/products", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ ...formData, price: parseFloat(formData.price), stock_quantity: parseInt(formData.stock_quantity), }), }); const data = await response.json(); if (response.ok) { alert("Product created successfully!"); // Reset form or redirect } else { console.error("Validation errors:", data.errors); } } catch (error) { console.error("Error creating product:", error); } }; const handleChange = (e) => { const { name, value, type, checked } = e.target; setFormData((prev) => ({ ...prev, [name]: type === "checkbox" ? checked : value, })); }; return (
); } export default CreateProduct; ``` --- ## Testing with curl You can test the API endpoints using curl commands: ```bash # Get all products curl -X GET http://localhost:8000/api/products # Get specific product curl -X GET http://localhost:8000/api/products/WH-001 # Create product curl -X POST http://localhost:8000/api/products \ -H "Content-Type: application/json" \ -d '{"name":"Test Product","sku":"TEST-001","price":29.99,"stock_quantity":100}' # Update product curl -X PUT http://localhost:8000/api/products/TEST-001 \ -H "Content-Type: application/json" \ -d '{"price":39.99,"stock_quantity":150}' # Delete product curl -X DELETE http://localhost:8000/api/products/TEST-001 ```