A complete pay-per-use AI image generation system built with the x402 payment protocol. This project includes both the backend API and a beautiful frontend web application.
- ✅ x402 Payment Protocol: Uses HTTP 402 status code for seamless payment integration
- 💰 Micropayments: Charges $0.001 USDC per image generation
- 🤖 AI Image Generation: Powered by Replicate API with Stable Diffusion
- 🌐 Production-Ready Frontend: Beautiful Next.js web application
- ⚡ Frictionless: No accounts, API keys, or complex authentication
- 📘 TypeScript: Built with TypeScript for type safety
If you have any question or something, feel free to reach out me anytime.
Telegram: @crypmancer
.
├── src/ # Backend API (Express + TypeScript)
│ ├── server.ts
│ ├── config.ts
│ ├── services/
│ └── types/
├── frontend/ # Frontend (Next.js + TypeScript)
│ ├── app/
│ ├── components/
│ └── lib/
└── README.md
-
Install dependencies:
npm install
-
Configure environment: Create
.envfile (see setup instructions below) -
Run the server:
npm run dev
Server runs on
http://localhost:3000
-
Navigate to frontend:
cd frontend npm install -
Configure API URL: Create
frontend/.env.local:NEXT_PUBLIC_API_URL=http://localhost:3000
-
Run frontend:
npm run dev
Frontend runs on
http://localhost:3001
# Server Configuration
PORT=3000
# x402 Payment Configuration
API_PRICE=0.001
NETWORK=base-sepolia
WALLET_ADDRESS=0xYourWalletAddressHere
FACILITATOR_URL=https://facilitator.x402.org
# Replicate API Configuration (Required for Image Generation)
REPLICATE_API_KEY=your_replicate_api_key_hereNEXT_PUBLIC_API_URL=http://localhost:3000- Go to https://replicate.com
- Sign up for a free account
- Get your API token from Account Settings
- Add it to backend
.envasREPLICATE_API_KEY
- User enters prompt in the frontend
- Frontend requests image from backend API
- Backend responds with 402 (Payment Required) including payment details
- User pays via wallet or facilitator (USDC on Base network)
- User submits transaction hash to verify payment
- Backend verifies payment and generates image
- Image is returned and displayed in the gallery
- Install Vercel CLI:
npm i -g vercel - Deploy:
vercel - Add environment variables in Vercel dashboard
- Navigate to
frontenddirectory - Deploy:
vercel - Set
NEXT_PUBLIC_API_URLto your backend URL
Generates an image from a prompt. Requires payment via x402 protocol.
Request:
{
"prompt": "a beautiful sunset over mountains"
}First Response (402):
{
"payment": {
"id": "payment-id",
"amount": "0.001",
"currency": "USDC",
"network": "base-sepolia",
"recipient": "0x...",
"facilitator": "https://facilitator.x402.org"
}
}After Payment (200):
{
"success": true,
"image": {
"url": "https://...",
"prompt": "...",
"timestamp": "..."
},
"paymentId": "...",
"transactionHash": "..."
}Verifies a payment transaction.
Health check endpoint.
- Express.js
- TypeScript
- Replicate API
- x402 Protocol
- Next.js 14
- React 18
- Tailwind CSS
- TypeScript
- Database: Replace in-memory
paymentStorewith a database - Payment Verification: Implement proper on-chain transaction verification
- Rate Limiting: Add rate limiting to prevent abuse
- CORS: Configure CORS for production domains
- Error Handling: Add comprehensive error handling and logging
- Monitoring: Set up monitoring and alerts
MIT
Built with ❤️ using the x402 protocol.