I Built a Stateless Image Processing API — Here's How It Works
Source: Dev.to
The Problem
Image‑processing infrastructure is surprisingly complex:
- Installing native dependencies on different OS targets
- Managing temporary files and disk cleanup
- Handling format quirks (AVIF support, GIF animation, color profiles)
- GPU acceleration for AI operations like background removal
- Scaling under load without leaking memory
I wanted an API where you send an image in and get a processed image out. Nothing stored, nothing cached on disk, no state between requests.
How It Works
TheGlitch processes images entirely in memory. The pipeline looks like this:
Input (URL/base64/binary/form)
→ Decode & Validate
→ Resize & Crop
→ Apply Effects
→ Format Convert
→ Return Binary
A single GET request can do everything:
curl "https://theglitch.p.rapidapi.com/api/v1/process?\
url=https://picsum.photos/1000&\
width=800&\
format=webp&\
brightness=10&\
contrast=15&\
sharpen=20" \
-H "X-RapidAPI-Key: YOUR_KEY" \
-H "X-RapidAPI-Host: theglitch.p.rapidapi.com" \
-o result.webp
Features
- Resize & Crop – Four modes:
fit(preserve aspect ratio),fill(crop to exact size),pad(add borders),stretch. Supports resolutions up to 8000 × 8000 px. - Format Conversion – Input/output: JPEG, PNG, WebP, GIF, BMP, TIFF. Quality control per format.
- 7 Visual Effects – Brightness, contrast, saturation, blur, sharpen, grayscale, sepia. All combinable in one request.
- AI Background Removal – GPU‑powered, ~3 s per image. Returns a transparent PNG.
- 14 Social Media Presets – Instagram square, Facebook cover, YouTube thumbnail, LinkedIn banner, etc. One parameter instead of remembering dimensions.
Code Examples
JavaScript
const response = await fetch(
'https://theglitch.p.rapidapi.com/api/v1/process?url=IMAGE_URL&width=800&format=webp',
{
headers: {
'X-RapidAPI-Key': 'YOUR_KEY',
'X-RapidAPI-Host': 'theglitch.p.rapidapi.com'
}
}
);
const blob = await response.blob();
Python
import requests
response = requests.get(
'https://theglitch.p.rapidapi.com/api/v1/process',
params={'url': 'IMAGE_URL', 'width': 800, 'format': 'webp'},
headers={'X-RapidAPI-Key': 'YOUR_KEY', 'X-RapidAPI-Host': 'theglitch.p.rapidapi.com'}
)
with open('result.webp', 'wb') as f:
f.write(response.content)
Why Stateless?
Every image is processed in memory and discarded after the response is sent. This means:
- GDPR compliant by design – no user data stored, ever
- No disk I/O bottleneck – everything happens in RAM
- Predictable scaling – each request is independent
- No cleanup jobs – nothing to garbage‑collect
Architecture Decisions
- SkiaSharp over ImageMagick – Native performance, cross‑platform, no external dependencies. Trade‑off: less format support (no AVIF encoding yet), but WebP covers most use cases.
- Replicate for GPU ops – Proxy AI operations through Replicate. Background removal costs about $0.0014 per image with BiRefNet; cold starts are free for public models.
- Separate CPU and GPU rate limits – CPU operations (resize, effects, format) are cheap; GPU operations (background removal) are expensive. Different limits per plan make pricing fair.
- Single VPS deployment – Docker Compose with Caddy as reverse proxy, Cloudflare in front for CDN/DDoS/SSL. Total infrastructure cost: under $6 / month.
API Endpoints
| Endpoint | What it does |
|---|---|
/api/v1/process | Full pipeline – resize + effects + format |
/api/v1/resize | Resize only |
/api/v1/convert | Format conversion |
/api/v1/effects | Visual effects |
/api/v1/remove-bg | AI background removal (GPU) |
/api/v1/optimize | Auto‑optimize for web (WebP) |
/api/v1/preset/{name} | Social media presets |
Try It
The API is live with a free tier (500 requests / month). Check out the before/after examples on the website, or try it directly through RapidAPI:
- Website with live examples:
- API on RapidAPI:
I’d love to hear what features you’d find useful. Background removal was the most requested during beta — what would you want next?