A RESTful API built with Express.js and Supabase for managing products with image upload capabilities. This API provides endpoints for creating, reading, updating, and deleting products, with image storage handled by Supabase Storage.
- CRUD operations for products
- Image upload and storage using Supabase Storage
- TypeScript support
- Environment variable configuration
- Error handling
- Image URL generation and management
- Node.js v18.x
- Supabase account and project
- npm or yarn
Create a .env file in the root directory with the following variables:
PORT=3000
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key├── src/
│ ├── config/
│ │ └── supabase.ts
│ ├── controllers/
│ │ └── productController.ts
│ ├── routes/
│ │ └── productRoutes.ts
│ ├── types/
│ │ └── product.ts
│ ├── utils/
│ │ └── storageHelper.ts
│ └── index.ts
├── .env
├── .gitignore
├── package.json
├── tsconfig.json
└── README.md
- Clone the repository:
git clone <repository-url>
cd express-supabase-api- Install dependencies:
npm install- Build the project:
npm run build- Start the server:
npm startFor development with hot-reload:
npm run dev| Method | Endpoint | Description |
|---|---|---|
| GET | /api/products |
Get all products |
| GET | /api/products/:id |
Get a specific product |
| POST | /api/products |
Create a new product |
| PUT | /api/products/:id |
Update a product |
| DELETE | /api/products/:id |
Delete a product |
curl -X POST -F "name=Product Name" \
-F "price=99.99" \
-F "description=Product Description" \
-F "image=@/path/to/image.jpg" \
http://localhost:3000/api/productscurl -X PUT -F "name=Updated Name" \
-F "price=149.99" \
-F "image=@/path/to/new-image.jpg" \
http://localhost:3000/api/products/1- Create a new Supabase project
- Create a storage bucket named
disc-product-images - Create a products table with the following schema:
create table products (
id bigint generated by default as identity primary key,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
name text not null,
price decimal(10,2) not null,
description text,
image_url text
);- Set up storage bucket policies:
CREATE POLICY "Public Access"
ON storage.objects FOR SELECT
TO public
USING (bucket_id = 'disc-product-images');
CREATE POLICY "Auth Upload"
ON storage.objects FOR INSERT
TO authenticated
WITH CHECK (bucket_id = 'disc-product-images');The API includes comprehensive error handling for:
- Invalid requests
- File upload failures
- Database operations
- Missing resources
- Server errors
npm run build: Compiles TypeScript to JavaScriptnpm start: Starts the production servernpm run dev: Starts the development server with hot-reloadnpm run reset: Resets the database (if configured)npm run setup-storage: Sets up Supabase storage bucket
The project uses TypeScript interfaces for type safety:
interface Product {
id: number;
created_at: string;
name: string;
price: number;
description?: string;
image_url?: string;
}This API can be deployed to platforms like Render, Heroku, or any other Node.js hosting service. Make sure to:
- Set all required environment variables
- Configure the build command:
npm install && npm run build - Set the start command:
npm start - Use Node.js 18.x runtime
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request