Skip to content

naimkowshik/price-comparison-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›’ Price Comparison API

Next.js TypeScript Supabase Vercel Node.js

A modern, production-ready API system for price comparison websites

GitHub stars GitHub forks GitHub issues GitHub license


โœจ Features

๐ŸŽฏ Core Features

  • RESTful API with proper error handling
  • Type-safe TypeScript for better development
  • Production ready with comprehensive testing
  • Modern architecture with Next.js 14

๐Ÿ” Search Capabilities

  • Category-specific search for all product types
  • Advanced filtering with multiple parameters
  • Keyword search across names and brands
  • Price range filtering with min/max values

โšก Performance

  • Pagination support for large datasets
  • Optimized queries with efficient joins
  • Fast responses with caching strategies
  • Scalable architecture for high traffic

๐Ÿš€ Quick Start

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

๐Ÿ“š API Endpoints

๐Ÿ  Core Endpoints

๐Ÿ“ฆ 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 ID
  • shop_id (optional): Filter by shop ID
  • brand (optional): Filter by brand (partial match)
  • min_price (optional): Minimum price filter
  • max_price (optional): Maximum price filter
  • sort_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 brand
  • brand (optional): Filter by brand (partial match)
  • category (optional): Filter by category name or ID
  • min_price (optional): Minimum price filter
  • max_price (optional): Maximum price filter
  • page (optional): Page number (default: 1)
  • limit (optional): Items per page (default: 20, max: 100)
  • sort_by (optional): Sort field
  • sort_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"

๐ŸŽฏ Category-Specific Search Endpoints

๐ŸŽฎ GPU

/api/gpu/search

๐Ÿ–ฅ๏ธ CPU

/api/cpu/search

๐Ÿ’พ RAM

/api/ram/search

๐Ÿ”Œ Motherboard

/api/motherboard/search

๐Ÿ–ผ๏ธ Casing

/api/casing/search

โšก Power Supply

/api/power-supply/search

๐ŸŒก๏ธ CPU Cooler

/api/cpu-cooler/search

โŒจ๏ธ Keyboard

/api/keyboard/search

๐Ÿ–ฑ๏ธ Mouse

/api/mouse/search

๐ŸŽง Headphone

/api/headphone/search

๐Ÿ”„ Dynamic

/api/[category]/search

๐Ÿ” General

/api/search

๐ŸŽฎ GPU Products

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

๐Ÿ–ฅ๏ธ CPU Products

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

๐Ÿ’พ RAM Products

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

๐Ÿ”Œ Motherboard Products

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

๐Ÿ–ผ๏ธ Casing Products

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

โšก Power Supply Products

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

๐ŸŒก๏ธ CPU Cooler Products

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

โŒจ๏ธ Keyboard Products

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

๐Ÿ–ฑ๏ธ Mouse Products

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

๐ŸŽง Headphone Products

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

๐Ÿ”„ Dynamic Category Search

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

๐Ÿ“Š Response Format

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

๐Ÿ› ๏ธ Setup

๐Ÿ“‹ Prerequisites

  • Node.js 18+
  • npm or yarn
  • Supabase account

๐Ÿ”ง Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd api
  2. Install dependencies

    npm install
  3. Environment setup Create .env.local file with your Supabase credentials:

    # Supabase Configuration
    NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
    NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
  4. Start development server

    npm run dev
  5. Build for production

    npm run build
    npm start

๐Ÿš€ Deployment

๐ŸŒ Vercel Deployment

  1. Connect to Vercel

    • Connect your GitHub repository to Vercel
    • Vercel will automatically detect Next.js
  2. Environment Variables Add these in Vercel dashboard:

    NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
    NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
    
  3. Deploy

    • Deploy automatically on push to main branch
    • Your API will be available at https://your-app.vercel.app

๐Ÿ”ง Other Platforms

  • Netlify: Compatible with Next.js
  • Railway: Easy deployment with environment variables
  • DigitalOcean: App Platform support

๐Ÿ“ Usage Examples

๐Ÿ” Basic Product Search

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

๐ŸŽฏ Advanced Search

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

๐ŸŽฎ Category-Specific Search

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

๐Ÿ—„๏ธ Database Schema

The API expects the following Supabase tables:

๐Ÿ“ฆ products_table

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: string

๐Ÿ“‚ categories_table

id: number
name: string

๐Ÿช shops_table

id: number
name: string
shop_url: string

๐Ÿ—๏ธ Database Setup

SQL Schema Creation

-- 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;

Sample Data

๐Ÿ“‚ categories_table

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

๐Ÿช shops_table

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

๐Ÿ“ฆ products_table

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

Database Relationships

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

Key Features

  • โœ… 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_at and updated_at
  • โœ… Price precision with NUMERIC(12,2) for accurate pricing
  • โœ… Row Level Security disabled for public API access

๐Ÿ›ก๏ธ Error Handling

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

๐Ÿ“‹ Error Response Format

{
  "success": false,
  "count": 0,
  "data": [],
  "message": "Error description"
}

๐Ÿ“ˆ Performance

  • โšก 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

๐ŸŽฏ Category Mapping

๐ŸŽฎ GPU

gpu | ID: 1

๐Ÿ–ฅ๏ธ CPU

cpu | ID: 2

๐Ÿ’พ RAM

ram | ID: 3

๐Ÿ”Œ Motherboard

motherboard | ID: 4

๐Ÿ–ผ๏ธ Casing

casing | ID: 5

โšก Power Supply

power-supply | ID: 6

๐ŸŒก๏ธ CPU Cooler

cpu-cooler | ID: 7

โŒจ๏ธ Keyboard

keyboard | ID: 8

๐Ÿ–ฑ๏ธ Mouse

mouse | ID: 9

๐ŸŽง Headphone

headphone | ID: 10


๐Ÿ“ Project Structure

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


๐Ÿ“ž Support

Need help? Check the API responses for error messages and ensure your Supabase credentials are correctly configured.

๐Ÿ› Report Issues


Made with โค๏ธ using Next.js 14, TypeScript, and Supabase

โฌ†๏ธ Back to Top

About

A modern, production-ready API system for price comparison websites

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published