|
|
|
# 1. Clone the repository
git clone https://github.com/naimkowshik/price-comparison-api.git
cd price-comparison-api
# 2. Install dependencies
npm install
# 3. Create environment file
echo "NEXT_PUBLIC_SUPABASE_URL=your_supabase_url" > .env.local
echo "NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_key" >> .env.local
# 4. Start development server
npm run dev
# 5. Test the API
curl "http://localhost:3000/api/gpu/search?keyword=GT"๐ฆ GET /api/products - Get all products with filters
Query Parameters:
page(optional): Page number (default: 1)limit(optional): Items per page (default: 20, max: 100)category_id(optional): Filter by category IDshop_id(optional): Filter by shop IDbrand(optional): Filter by brand (partial match)min_price(optional): Minimum price filtermax_price(optional): Maximum price filtersort_by(optional): Sort field (created_at, updated_at, current_price, name, brand)sort_order(optional): Sort direction (asc, desc)
Example:
curl "http://localhost:3000/api/products?page=1&limit=10&category_id=1&min_price=1000&sort_by=current_price&sort_order=asc"๐ GET /api/products/[id] - Get single product details
Example:
curl "http://localhost:3000/api/products/3204"๐ GET /api/categories - Get all categories
Query Parameters:
sort_by(optional): Sort field (id, name)sort_order(optional): Sort direction (asc, desc)
Example:
curl "http://localhost:3000/api/categories?sort_by=name&sort_order=asc"๐ช GET /api/shops - Get all shops
Query Parameters:
sort_by(optional): Sort field (id, name)sort_order(optional): Sort direction (asc, desc)
Example:
curl "http://localhost:3000/api/shops?sort_by=name&sort_order=asc"๐ GET /api/search - Advanced search with multiple filters
Query Parameters:
keyword(optional): Search in product name and brandbrand(optional): Filter by brand (partial match)category(optional): Filter by category name or IDmin_price(optional): Minimum price filtermax_price(optional): Maximum price filterpage(optional): Page number (default: 1)limit(optional): Items per page (default: 20, max: 100)sort_by(optional): Sort fieldsort_order(optional): Sort direction
Example:
curl "http://localhost:3000/api/search?keyword=3070&category=GPU&min_price=20000&max_price=90000&page=1&limit=10"|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Simple GPU search
curl "http://localhost:3000/api/gpu/search?keyword=3070"
# Advanced GPU search with filters
curl "http://localhost:3000/api/gpu/search?keyword=rtx&brand=NVIDIA&min_price=20000&max_price=90000&sort_by=current_price&sort_order=asc"# Simple CPU search
curl "http://localhost:3000/api/cpu/search?keyword=5700x"
# Advanced CPU search with filters
curl "http://localhost:3000/api/cpu/search?keyword=amd&brand=AMD&min_price=15000&max_price=50000"# Simple RAM search
curl "http://localhost:3000/api/ram/search?keyword=16gb"
# Advanced RAM search with filters
curl "http://localhost:3000/api/ram/search?keyword=corsair&brand=Corsair&min_price=5000&max_price=20000"# Simple Motherboard search
curl "http://localhost:3000/api/motherboard/search?keyword=B550"
# Advanced Motherboard search with filters
curl "http://localhost:3000/api/motherboard/search?keyword=msi&brand=MSI&min_price=10000&max_price=30000"# Simple Casing search
curl "http://localhost:3000/api/casing/search?keyword=mid tower"
# Advanced Casing search with filters
curl "http://localhost:3000/api/casing/search?keyword=cooler master&brand=Cooler Master&min_price=3000&max_price=15000"# Simple Power Supply search
curl "http://localhost:3000/api/power-supply/search?keyword=750w"
# Advanced Power Supply search with filters
curl "http://localhost:3000/api/power-supply/search?keyword=evga&brand=EVGA&min_price=5000&max_price=25000"# Simple CPU Cooler search
curl "http://localhost:3000/api/cpu-cooler/search?keyword=ld240"
# Advanced CPU Cooler search with filters
curl "http://localhost:3000/api/cpu-cooler/search?keyword=corsair&brand=Corsair&min_price=5000&max_price=20000"# Simple Keyboard search
curl "http://localhost:3000/api/keyboard/search?keyword=mechanical"
# Advanced Keyboard search with filters
curl "http://localhost:3000/api/keyboard/search?keyword=razer&brand=Razer&min_price=2000&max_price=15000"# Simple Mouse search
curl "http://localhost:3000/api/mouse/search?keyword=gaming"
# Advanced Mouse search with filters
curl "http://localhost:3000/api/mouse/search?keyword=logitech&brand=Logitech&min_price=1000&max_price=10000"# Simple Headphone search
curl "http://localhost:3000/api/headphone/search?keyword=wireless"
# Advanced Headphone search with filters
curl "http://localhost:3000/api/headphone/search?keyword=sony&brand=Sony&min_price=2000&max_price=25000"# Works with any category
GET /api/[category]/search?keyword=...
# Examples:
curl "http://localhost:3000/api/gpu/search?keyword=3070"
curl "http://localhost:3000/api/cpu/search?keyword=5700x"
curl "http://localhost:3000/api/ram/search?keyword=16gb"All endpoints return a consistent JSON response:
{
"success": true,
"count": 2,
"data": [
{
"id": 3204,
"name": "Afox GeForce GT 240 1GB GDDR3 Graphics Card #AF240-1024D3L2-V2",
"brand": "Afox",
"category_id": 1,
"current_price": "3900.00",
"old_price": null,
"image_url": "https://www.ryans.com/storage/products/small/afox-geforce-gt-240-1gb-gddr3-graphics-card-11736685726.webp",
"shop_id": 1,
"product_url": "https://www.ryans.com/afox-geforce-gt-240-1gb-graphics-card",
"created_at": "2025-09-29T20:57:13.861431+00:00",
"updated_at": "2025-09-29T20:57:13.861431+00:00",
"category": {
"name": "GPU"
},
"shop": {
"name": "Ryans Computers",
"shop_url": "https://ryans.com"
}
}
],
"message": "GPU search results"
}- Node.js 18+
- npm or yarn
- Supabase account
-
Clone the repository
git clone <your-repo-url> cd api
-
Install dependencies
npm install
-
Environment setup Create
.env.localfile with your Supabase credentials:# Supabase Configuration NEXT_PUBLIC_SUPABASE_URL=your_supabase_url NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key -
Start development server
npm run dev
-
Build for production
npm run build npm start
-
Connect to Vercel
- Connect your GitHub repository to Vercel
- Vercel will automatically detect Next.js
-
Environment Variables Add these in Vercel dashboard:
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key -
Deploy
- Deploy automatically on push to main branch
- Your API will be available at
https://your-app.vercel.app
- Netlify: Compatible with Next.js
- Railway: Easy deployment with environment variables
- DigitalOcean: App Platform support
# Get all products with pagination
curl "http://localhost:3000/api/products?page=1&limit=5"
# Get single product
curl "http://localhost:3000/api/products/3204"
# Get all categories
curl "http://localhost:3000/api/categories"
# Get all shops
curl "http://localhost:3000/api/shops"# Search for GPU products under 10000 BDT
curl "http://localhost:3000/api/search?category=GPU&max_price=10000"
# Search by keyword "3050" in GPU category
curl "http://localhost:3000/api/search?keyword=3070&category=GPU&min_price=20000&max_price=90000"
# Search by brand "MSI"
curl "http://localhost:3000/api/search?brand=MSI"# GPU search
curl "http://localhost:3000/api/gpu/search?keyword=3070"
curl "http://localhost:3000/api/gpu/search?keyword=rtx&brand=NVIDIA&min_price=20000"
# CPU search
curl "http://localhost:3000/api/cpu/search?keyword=5700x"
curl "http://localhost:3000/api/cpu/search?keyword=amd&brand=AMD"
# RAM search
curl "http://localhost:3000/api/ram/search?keyword=16gb"
curl "http://localhost:3000/api/ram/search?keyword=corsair&brand=Corsair"
# Motherboard search
curl "http://localhost:3000/api/motherboard/search?keyword=B550"
curl "http://localhost:3000/api/motherboard/search?keyword=msi&brand=MSI"
# Casing search
curl "http://localhost:3000/api/casing/search?keyword=mid tower"
curl "http://localhost:3000/api/casing/search?keyword=cooler master&brand=Cooler Master"
# Power Supply search
curl "http://localhost:3000/api/power-supply/search?keyword=750w"
curl "http://localhost:3000/api/power-supply/search?keyword=evga&brand=EVGA"
# CPU Cooler search
curl "http://localhost:3000/api/cpu-cooler/search?keyword=ld240"
curl "http://localhost:3000/api/cpu-cooler/search?keyword=corsair&brand=Corsair"
# Keyboard search
curl "http://localhost:3000/api/keyboard/search?keyword=mechanical"
curl "http://localhost:3000/api/keyboard/search?keyword=razer&brand=Razer"
# Mouse search
curl "http://localhost:3000/api/mouse/search?keyword=gaming"
curl "http://localhost:3000/api/mouse/search?keyword=logitech&brand=Logitech"
# Headphone search
curl "http://localhost:3000/api/headphone/search?keyword=wireless"
curl "http://localhost:3000/api/headphone/search?keyword=sony&brand=Sony"The API expects the following Supabase tables:
id: number
name: string
brand: string
category_id: number
current_price: string
old_price: string | null
image_url: string
shop_id: number
product_url: string
created_at: string
updated_at: stringid: number
name: stringid: number
name: string
shop_url: string-- Create categories table
CREATE TABLE categories_table (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name TEXT NOT NULL UNIQUE
);
-- Create shops table
CREATE TABLE shops_table (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name TEXT NOT NULL UNIQUE,
shop_url TEXT NOT NULL
);
-- Create products table
CREATE TABLE products_table (
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
name TEXT NOT NULL,
brand TEXT NOT NULL,
category_id BIGINT REFERENCES categories_table(id) ON DELETE SET NULL,
current_price NUMERIC(12,2) NOT NULL,
old_price NUMERIC(12,2),
image_url TEXT,
shop_id BIGINT REFERENCES shops_table(id) ON DELETE CASCADE,
product_url TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Disable Row Level Security for public access
ALTER TABLE categories_table DISABLE ROW LEVEL SECURITY;
ALTER TABLE shops_table DISABLE ROW LEVEL SECURITY;
ALTER TABLE products_table DISABLE ROW LEVEL SECURITY;[
{"id": 1, "name": "GPU"},
{"id": 2, "name": "CPU"},
{"id": 3, "name": "RAM"},
{"id": 4, "name": "MOTHERBOARD"},
{"id": 5, "name": "Casing"},
{"id": 6, "name": "Power Supply"},
{"id": 7, "name": "CPU Cooler"},
{"id": 8, "name": "Keyboard"},
{"id": 9, "name": "Mouse"},
{"id": 10, "name": "Headphone"}
][
{"id": 1, "name": "Ryans Computers", "shop_url": "https://ryans.com"},
{"id": 2, "name": "Startech", "shop_url": "https://startech.com.bd"},
{"id": 3, "name": "Potakait", "shop_url": "https://potakait.com"}
][
{
"id": 3204,
"name": "Afox GeForce GT 240 1GB GDDR3 Graphics Card #AF240-1024D3L2-V2",
"brand": "Afox",
"category_id": 1,
"current_price": "3900.00",
"old_price": null,
"image_url": "https://www.ryans.com/storage/products/small/afox-geforce-gt-240-1gb-gddr3-graphics-card-11736685726.webp",
"shop_id": 1,
"product_url": "https://www.ryans.com/afox-geforce-gt-240-1gb-graphics-card",
"created_at": "2025-09-29T20:57:13.861431+00:00",
"updated_at": "2025-09-29T20:57:13.861431+00:00"
},
{
"id": 3205,
"name": "OCPC GeForce GT 730 4GB GDDR3 Graphics Card #OCVNGT730G4",
"brand": "OCPC",
"category_id": 1,
"current_price": "6500.00",
"old_price": null,
"image_url": "https://www.ryans.com/storage/products/small/ocpc-geforce-gt-730-4gb-gddr3-graphics-card-11733921036.webp",
"shop_id": 1,
"product_url": "https://www.ryans.com/ocpc-geforce-gt-730-4gb-gddr3-graphics-card",
"created_at": "2025-09-29T20:57:13.861431+00:00",
"updated_at": "2025-09-29T20:57:13.861431+00:00"
}
]| Table | Relationship | Description |
|---|---|---|
| products_table | โ categories_table | category_id references categories_table.id |
| products_table | โ shops_table | shop_id references shops_table.id |
| categories_table | โ products_table | One category can have many products |
| shops_table | โ products_table | One shop can have many products |
- โ
Auto-generated IDs with
GENERATED BY DEFAULT AS IDENTITY - โ Foreign key constraints for data integrity
- โ Cascade delete for shops (products deleted when shop is deleted)
- โ Set NULL for categories (products remain when category is deleted)
- โ
Timestamps with automatic
created_atandupdated_at - โ
Price precision with
NUMERIC(12,2)for accurate pricing - โ Row Level Security disabled for public API access
All endpoints include comprehensive error handling:
- โ Input validation - Parameter validation
- โ Database error handling - Graceful error responses
- โ HTTP status codes - Proper status codes
- โ Structured responses - Consistent error format
{
"success": false,
"count": 0,
"data": [],
"message": "Error description"
}- โก Pagination - Handle large datasets efficiently
- ๐ Optimized queries - Efficient database queries with proper indexing
- ๐ก๏ธ Type-safe operations - TypeScript for better performance
- ๐ Production optimized - Ready for high-traffic scenarios
|
|
|
|
|
|
|
|
|
|
|
|
api/
โโโ ๐ app/
โ โโโ ๐ api/
โ โโโ ๐ [category]/search/route.ts # Dynamic category search
โ โโโ ๐ gpu/search/route.ts # GPU search
โ โโโ ๐ cpu/search/route.ts # CPU search
โ โโโ ๐ ram/search/route.ts # RAM search
โ โโโ ๐ motherboard/search/route.ts # Motherboard search
โ โโโ ๐ casing/search/route.ts # Casing search
โ โโโ ๐ power-supply/search/route.ts # Power Supply search
โ โโโ ๐ cpu-cooler/search/route.ts # CPU Cooler search
โ โโโ ๐ keyboard/search/route.ts # Keyboard search
โ โโโ ๐ mouse/search/route.ts # Mouse search
โ โโโ ๐ headphone/search/route.ts # Headphone search
โ โโโ ๐ products/route.ts # All products
โ โโโ ๐ products/[id]/route.ts # Single product
โ โโโ ๐ categories/route.ts # All categories
โ โโโ ๐ shops/route.ts # All shops
โ โโโ ๐ search/route.ts # General search
โโโ ๐ lib/
โ โโโ ๐ supabase.ts # Supabase client
โ โโโ ๐ categories.ts # Category mapping
โโโ ๐ package.json
โโโ ๐ next.config.js
โโโ ๐ tsconfig.json
โโโ ๐ .gitignore
โโโ ๐ README.md
Need help? Check the API responses for error messages and ensure your Supabase credentials are correctly configured.
Made with โค๏ธ using Next.js 14, TypeScript, and Supabase