CodeWithShreyans/paperbuild

★ 0Forks 0TypeScriptGitHub ↗Compare

Project website ↗

README

Paperbuild - Pay-per-use AI Agent

A pay-per-use version of Paperbuild that uses Coinbase x402 payment protocol to monetize AI-powered repository modifications.

Architecture

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

Setup

1. Install Dependencies

npm install
# or
bun install

2. Environment Variables

Create 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=0xBuyerWalletAddress

3. Create and Fund CDP Buyer Wallet

Run the setup script to create a CDP wallet and fund it (testnet only):

npm run setup:wallet

This 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 .env file

Copy the wallet address and add it to your .env.local:

CDP_BUYER_WALLET_ADDRESS=0xYourGeneratedAddress

For mainnet: You'll need to manually fund the wallet with ETH (for gas) and USDC (for payments).

4. Run Development Server

npm run dev
# or
bun dev

How It Works

Flow

Frontend → Agent 1 → Agent 2

  1. User Request: Frontend calls /api/execute with repo URL, GitHub PAT, and prompt

  2. 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
  3. Agent 1 calls Agent 2 (commit): Calls /api/commit with job ID

    • x402 middleware intercepts and returns 402 Payment Required
    • Includes payment options in response
  4. 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
  5. Agent 2 verifies and commits: /api/commit receives retry with payment

    • x402 middleware verifies payment on-chain
    • Git push executes
    • Sandbox cleanup
    • Returns success
  6. Agent 1 returns to frontend: Success response with job ID

All agent and payment operations happen server-side!

Key Files

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 agent
  • app/api/modify/service.ts - Claude agent service logic
  • app/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

Payment Configuration

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

For Testing

Use base-sepolia network for testing without real funds.

For Production

  1. Set NETWORK=base in environment variables
  2. Update wallet address to production wallet
  3. Ensure sufficient liquidity for receiving payments

Demo UI

The demo page at / provides two modes:

Automatic Mode (One-Click)

  1. Enter Details: Repository URL, GitHub PAT, and modification prompt
  2. Click Submit: Single button triggers entire workflow
  3. Server handles everything: Modify → 402 → Pay → Commit
  4. See result: Success or error message

Step-by-Step Mode (Manual Demo)

  1. Step 1: Click "Start Modification" → Calls /api/modify → Shows job ID
  2. Step 2: Click "Request Commit" → Calls /api/commit → Shows raw 402 response with payment options
  3. 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.

Requirements for Users

  • GitHub Personal Access Token: Create one at GitHub Settings with repo scope
  • No crypto wallet needed: Payments happen automatically server-side
  • No gas fees for users: Server wallet pays all transaction costs

Benefits for Users

  • 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

Setup for Server Admin

  • 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

Public and Private Repositories

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.

Technical Stack

  • 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

Learn More

paperbuild

Contributors

CodeWithShreyans

Issues