A lightweight Express.js API server with TypeScript for background removal using Sharp image processing.
- 🚀 Fast & Lightweight: Express.js with TypeScript
- 🖼️ Image Processing: Sharp-based background removal
- 🌐 CORS Support: Configurable cross-origin requests
- 📁 File Upload: Multer-based multipart handling
- 🔧 Environment Config: Flexible configuration via environment variables
- 🧪 Testing: Jest test suite included
- 🐳 Replit Ready: Optimized for Replit deployment
-
Clone and Install
git clone <your-repo-url> cd express-removebg-api npm install
-
Environment Setup
cp env.example .env # Edit .env with your configuration -
Development Server
npm run dev
-
Build for Production
npm run build npm start
- Import this repository to Replit
- Replit will automatically install dependencies
- Click "Run" - the server will start automatically
GET /api/healthResponse:
{
"status": "healthy",
"message": "RemoveBG API is running 🚀",
"timestamp": "2024-01-01T00:00:00.000Z",
"version": "1.0.0"
}POST /api/removebg
Content-Type: multipart/form-dataParameters:
file: Image file (JPEG, PNG, WebP, GIF)
cURL Example:
curl -X POST \
-F "file=@/path/to/your/image.jpg" \
http://localhost:3000/api/removebg \
--output removed-bg.pngResponse:
- Success: PNG image with transparent background
- Error: JSON error message
Environment variables (see env.example):
| Variable | Default | Description |
|---|---|---|
PORT |
3000 |
Server port |
NODE_ENV |
development |
Environment mode |
ALLOWED_ORIGINS |
* |
CORS allowed origins (comma-separated) |
MAX_FILE_SIZE |
10485760 |
Max file size in bytes (10MB) |
ALLOWED_FILE_TYPES |
image/jpeg,image/png,image/webp,image/gif |
Allowed MIME types |
BACKGROUND_REMOVAL_TOLERANCE |
20 |
Color tolerance for background removal |
express-removebg-api/
├── src/
│ ├── controllers/ # Request handlers
│ │ └── removebgController.ts
│ ├── routes/ # API routes
│ │ └── api.ts
│ ├── utils/ # Utilities
│ │ └── sharpUtils.ts
│ ├── middleware/ # Custom middleware
│ │ └── cors.ts
│ ├── app.ts # Express app setup
│ └── server.ts # Server entry point
├── tests/ # Test files
├── dist/ # Compiled JavaScript (generated)
├── package.json
├── tsconfig.json
└── README.md
npm run dev- Start development server with hot reloadnpm run build- Compile TypeScript to JavaScriptnpm start- Start production servernpm test- Run test suitenpm run test:watch- Run tests in watch mode
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Test the API manually
curl http://localhost:3000/api/healthThe API uses a color-based background removal algorithm:
- Key Color Detection: Samples the top-left pixel as the background color
- Color Tolerance: Removes pixels within a configurable color distance
- Transparency: Converts matching pixels to transparent
- Output: Returns PNG with alpha channel
Adjust BACKGROUND_REMOVAL_TOLERANCE environment variable:
0-10: Very precise (fewer pixels removed)20-30: Balanced (recommended)40-50: Aggressive (more pixels removed)
- Import repository
- Environment variables are set automatically
- Click "Run" to deploy
- Heroku: Add
Procfilewithweb: npm start - Railway: Works out of the box
- DigitalOcean App Platform: Use Node.js buildpack
- Vercel: Add
vercel.jsonconfiguration
- Memory Usage: ~30-50MB base + image processing
- Cold Start: ~500ms
- Processing Time: 1-3 seconds per image (depends on size)
- Concurrent Requests: Handles multiple requests efficiently
-
"File too large" error
- Increase
MAX_FILE_SIZEenvironment variable - Check platform upload limits
- Increase
-
"Not allowed by CORS" error
- Update
ALLOWED_ORIGINSenvironment variable - Use
*for development, specific domains for production
- Update
-
Sharp installation issues
- Ensure platform supports native Sharp binaries
- Some serverless platforms may require Sharp layers
The server provides detailed logging:
- Request logging with timestamps
- Background removal progress
- Error details in development mode
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - see LICENSE file for details.