Vishal.dev
Back
PixelForge — Pay-As-You-Go AI Image Generation — live site preview
Full-Stack

PixelForge — Pay-As-You-Go AI Image Generation

AI image generation platform with multi-model generation via OpenRouter, one-time Razorpay credit packs, OAuth via Better Auth, atomic credit deduction, automatic refunds, and a full image gallery with transaction history.

React 19BunExpress 5TypeScript 5.9PostgreSQLPrismaBetter AuthOpenRouterRazorpayCloudflare R2TanStack QueryTailwind CSS 4Zod
6+
AI image models
3
Credit packs
2
Payment paths

Domain Knowledge

What problem this project solves

AI billing is a correctness problem: a concurrent request must never spend credits twice, and a failed generation must never silently eat money. LumiForge solves both with atomic decrement and automatic refunds, while the dual-path payment flow guarantees credits are issued even if the browser disconnects mid-checkout.

Architecture

How the system is structured

A Bun + Express backend with Zod-validated routes for user, image generation, models, and credits & billing, plus a React 19 frontend with TanStack Query and React Router. Cloudflare R2 stores generated images; Better Auth handles email/password and Google OAuth. Credits, spend, and refunds are tracked per user with a full transaction history.

Data Model

Schema design and data flow

Prisma models for users, credit wallets, transactions (purchases, spend, refunds), generated images, and trial eligibility. Credit balance is stored and decremented atomically with validation in the same query.

Key Challenges

Hardest problems encountered

Atomic credit deduction under concurrent requests, mapping real model cost to fair credit pricing, the dual-path payment fulfillment (client verify + webhook fallback) without double-crediting, and server-side trial guardrails to prevent premium-model abuse on the free tier.

Scaling Strategy

How the system grows

Stateless Express API on Render with PostgreSQL; images offloaded to Cloudflare R2 for edge distribution. TanStack Query caches client state. Payment flow is idempotent across both fulfillment paths.

Security

Defense-in-depth approach

Better Auth with OAuth, Zod validation on all routes, server-side trial restrictions, atomic wallet operations, and webhook verification to prevent forged payment callbacks.

Failure Handling

Resilience and recovery

Failed generations trigger automatic refunds. The Razorpay webhook is the fallback path — if the browser never receives the response, credits are still issued and deduplicated.

Observability

Monitoring and debugging

Transaction history gives a full audit of purchases, spend, and refunds; generation success/failure is tracked per model.

Trade-offs

Engineering decisions and alternatives

Pay-as-you-go credits over subscriptions for flexibility. OpenRouter over a single vendor for multi-model support. Razorpay webhook + verify over a single path for reliability. R2 over S3 for simpler egress pricing.

Architecture Decisions

Key choices and what was rejected

Decision
Chosen
Rejected
Payments
Verify endpoint + webhook fallback
Single verification path
Credits
Atomic single-query decrement
Read-then-write (race conditions)
Models
OpenRouter multi-model
Single vendor

Senior-Level Topics

Concepts this project explores

Atomic Balance OperationsMulti-Provider Model APIsDual-Path Payment FulfillmentPer-Model Cost PricingTrial Guardrails