A pay-per-use version of Paperbuild that uses Coinbase x402 payment protocol to monetize AI-powered repository modifications.
The system consists of two agents (both server-side):
-
Agent 2 (Payee/Seller): Provides the modification service, protected by x402 payment middleware
/api/modify- Runs Claude agent in sandbox/api/commit- Commits changes (402 if unpaid, commits if paid)
-
Agent 1 (Payer/Buyer): Consumes Agent 2's service, handles payment automatically
/api/execute- Orchestrates: modify → commit → pay → commit/api/process-payment- Sends USDC via CDP wallet
Frontend: Simple form that calls /api/execute - all agent and payment logic is server-side
npm install
# or
bun installCreate a .env.local file:
# Anthropic API Key for Claude agent
ANTHROPIC_API_KEY=your_anthropic_api_key
# Coinbase CDP Server Wallet credentials
CDP_API_KEY_ID=your_cdp_api_key_id
CDP_API_KEY_SECRET=your_cdp_api_key_secret
CDP_WALLET_SECRET=your_cdp_wallet_secret
# Coinbase x402 Configuration (for receiving payments)
RESOURCE_WALLET_ADDRESS=0xYourWalletAddress
NETWORK=base-sepolia # Use 'base' for mainnet
# CDP Buyer Wallet (created in step 3)
# CDP_BUYER_WALLET_ADDRESS=0xBuyerWalletAddressRun the setup script to create a CDP wallet and fund it (testnet only):
npm run setup:walletThis will:
- Create a new CDP Server Wallet account
- Request testnet ETH and USDC from faucets (base-sepolia only)
- Output the wallet address to add to your
.envfile
Copy the wallet address and add it to your .env.local:
CDP_BUYER_WALLET_ADDRESS=0xYourGeneratedAddressFor mainnet: You'll need to manually fund the wallet with ETH (for gas) and USDC (for payments).
npm run dev
# or
bun devFrontend → Agent 1 → Agent 2
-
User Request: Frontend calls
/api/executewith repo URL, GitHub PAT, and prompt -
Agent 1 calls Agent 2 (modify): Calls
/api/modify- Agent 2 runs Claude agent in Vercel Sandbox
- Changes are staged and committed but not pushed
- Returns sandbox ID as job ID
-
Agent 1 calls Agent 2 (commit): Calls
/api/commitwith job ID- x402 middleware intercepts and returns 402 Payment Required
- Includes payment options in response
-
Agent 1 processes payment: x402-fetch automatically handles 402
- Detects 402 response and payment requirements
- Uses CDP Server Wallet to sign and send USDC transfer
- Transaction sent to Agent 2's wallet address
- Automatically retries commit request with payment proof
-
Agent 2 verifies and commits:
/api/commitreceives retry with payment- x402 middleware verifies payment on-chain
- Git push executes
- Sandbox cleanup
- Returns success
-
Agent 1 returns to frontend: Success response with job ID
All agent and payment operations happen server-side!
Agent 1 (Payer/Buyer) - Server-side:
app/api/execute/route.ts- Full workflow orchestrator (auto mode)app/api/commit-with-payment/route.ts- Step 3 wrapper using wrapFetchWithPayment (manual mode)
Agent 2 (Payee/Seller) - Server-side:
middleware.ts- x402 payment middleware configuration (deleted, now in proxy.ts)app/api/modify/route.ts- Creates sandbox and runs Claude agentapp/api/modify/service.ts- Claude agent service logicapp/api/commit/route.ts- Commits changes after payment verification
Supporting:
app/lib/cdp-signer.ts- CDP to viem account adapter (uses toAccount)scripts/setup-cdp-wallet.ts- One-time CDP wallet setup
Frontend:
app/components/modify-form.tsx- Dual-mode form (auto + manual step-by-step)app/page.tsx- Home page
The /api/commit endpoint is protected by x402 middleware:
- Price: $0.01 per commit
- Network: Base Sepolia (testnet) or Base (mainnet)
- Payment Method: Onchain stablecoin payment
Use base-sepolia network for testing without real funds.
- Set
NETWORK=basein environment variables - Update wallet address to production wallet
- Ensure sufficient liquidity for receiving payments
The demo page at / provides two modes:
- Enter Details: Repository URL, GitHub PAT, and modification prompt
- Click Submit: Single button triggers entire workflow
- Server handles everything: Modify → 402 → Pay → Commit
- See result: Success or error message
- Step 1: Click "Start Modification" → Calls
/api/modify→ Shows job ID - Step 2: Click "Request Commit" → Calls
/api/commit→ Shows raw 402 response with payment options - Step 3: Click "Pay & Commit" → Uses
wrapFetchWithPayment→ Automatically pays and commits → Shows success with payment details
Each step displays its JSON response and buttons appear sequentially as steps complete.
- GitHub Personal Access Token: Create one at GitHub Settings with
reposcope - No crypto wallet needed: Payments happen automatically server-side
- No gas fees for users: Server wallet pays all transaction costs
- Works with private repos: Use your GitHub PAT to access private repositories
- No manual transactions: Payments happen automatically server-side
- Simple UX: Just enter repo details and prompt, then click and wait
- Secure: PAT is only used temporarily and not persisted
- One-time wallet creation via
npm run setup:wallet - Wallet funded once on testnet via faucets
- Wallet address stored in environment variables
- Reused for all transactions
This version works with both public and private repositories using GitHub Personal Access Tokens (PAT). Users provide their GitHub PAT in the form, which is used for:
- Cloning the repository (public or private)
- Pushing changes back to the repository
The PAT is only stored temporarily during the modification process and is not persisted.
- Frontend: Next.js 16 with App Router, React 19, Tailwind CSS
- Server Wallet: @coinbase/cdp-sdk for automated wallet management
- Payment Protocol:
- Seller: x402-next middleware for payment protection
- Buyer: x402-fetch for automatic payment handling
- AI Agent: Claude Code via @anthropic-ai/claude-code
- Sandbox: Vercel Sandbox for isolated git operations
- Blockchain: Base (mainnet) or Base Sepolia (testnet)
- Token: USDC (native on Base)
- Transaction Signing: viem + CDP SDK integration