How I Built an AI Product Photography Pipeline with 30+ Models (Next.js + Express + Replicate/FAL)
Source: Dev.to
Overview
PixelMotion is a SaaS that transforms a single product photo into enhanced images and AI‑generated videos. The platform abstracts away the complexities of 30+ AI models, handling everything from background removal to video generation.
Stack
| Layer | Technologies |
|---|---|
| Frontend | Next.js 15 (App Router) • TailwindCSS |
| Backend | Express.js • TypeScript • PostgreSQL (Sequelize ORM) |
| AI Providers | Replicate • FAL AI • OpenAI |
| Storage | Google Cloud Storage |
| Payments | Stripe (usage‑based credits) |
Architecture Flow
User Upload → Website Scraper → AI Analysis → Model Selection → Enhancement/Generation → Storage → Delivery
Unified LLM Service
A single service normalizes the differing APIs of each provider.
// Simplified model orchestration
const result = await llmService.generate({
provider: model.provider, // 'replicate' | 'fal' | 'openai'
model: model.id,
input: normalizedParams,
webhook: callbackUrl,
});
Each provider adapter implements its own retry logic, timeout handling, and error mapping, so adding a new model is just a configuration change.
Fallback Chains
AI models can fail due to rate limits, cold starts, or updates. A fallback chain ensures continuity.
const ENHANCEMENT_CHAIN = [
{ model: 'flux-pro-v2', provider: 'replicate' },
{ model: 'flux-pro', provider: 'fal' },
{ model: 'stable-diffusion-xl', provider: 'replicate' },
];
If the primary model fails or times out, the system automatically tries the next one, keeping the user experience seamless.
Credit System
Different models have varying costs. Instead of flat subscriptions, a credit system assigns a credit cost per generation.
| Model | Credits | Approx. Cost |
|---|---|---|
| Flux 2 Pro (photo) | 2 | ~ $0.05 |
| Kling 1.6 (video) | 5 | ~ $0.13 |
| Sora 2 (video) | 20 | ~ $0.50 |
| Veo 3.1 (video) | 25 | ~ $0.65 |
Users can select models based on budget and quality requirements.
Store Integration & Prompt Generation
When a user connects their e‑commerce store, the platform:
- Scrapes product data.
- Uses GPT‑4o to analyze:
- Product category & type
- Target audience
- Brand aesthetic
- Optimal AI models for the product
The analysis feeds into prompt generation, allowing, for example, a luxury watch to receive a different treatment than a kitchen gadget.
Job Queue & Frontend Polling
AI generation takes 30–120 seconds. A job‑queue pattern manages the workflow:
- User submits request → job created (
pendingstatus). - Backend dispatches to the selected AI provider.
- Frontend polls every 3 seconds via a custom hook.
- Webhook or poll marks the job as
completed.
// Frontend polling hook (simplified)
const useGenerationStatus = (jobId: string) => {
const [status, setStatus] = useState('pending');
useEffect(() => {
const interval = setInterval(async () => {
const res = await api.get(`/jobs/${jobId}`);
setStatus(res.data.status);
if (res.data.status === 'completed') clearInterval(interval);
}, 3000);
return () => clearInterval(interval);
}, [jobId]);
return status;
};
Lessons Learned
- Validate model output – Blank images, corrupted files, or completely wrong results must be filtered before serving.
- Cost management – Implement per‑user rate limits and daily cost alerts; otherwise expenses can explode quickly.
- Prompt engineering outweighs model selection – A well‑crafted prompt on a cheaper model often beats a poor prompt on an expensive one.
- Webhooks are unreliable – Always have a polling fallback for both Replicate and FAL.
- Users care about results, not the model – Auto‑selecting the best model based on product type improves satisfaction more than exposing the model list to users.
Current Work
- Multi‑photo video generation (combining several angles).
- UGC‑style video generation with AI avatars.
- Direct publishing to TikTok/YouTube from the platform.
Call to Action
If you’re building with AI APIs and want to discuss multi‑provider orchestration, fallback patterns, or credit systems, feel free to drop a comment. I’m happy to dive deeper into any of these topics.
Live Demo
Check out the product at pixelmotion.io.
Built with: Next.js 15, Express.js, PostgreSQL, Replicate, FAL AI, and OpenAI.