Skip to content

Z333Q/p402-mini

Repository files navigation

P402 Base Mini App

A full P402 AI client running as a Base Mini App. Users can connect their Base Account, fund with USDC via Base Pay, and access 100+ AI models through P402's smart routing.

🎯 What This Does

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           P402 MINI APP                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                         β”‚
β”‚  1. CONNECT                             β”‚
β”‚     User connects Base Account          β”‚
β”‚     β†’ Creates P402 session              β”‚
β”‚                                         β”‚
β”‚  2. FUND                                β”‚
β”‚     Add USDC via Base Pay               β”‚
β”‚     β†’ Credits P402 session              β”‚
β”‚                                         β”‚
β”‚  3. CHAT                                β”‚
β”‚     Select model (GPT-4, Claude, etc)   β”‚
β”‚     β†’ Streaming responses               β”‚
β”‚     β†’ Real-time cost display            β”‚
β”‚     β†’ Savings calculation               β”‚
β”‚                                         β”‚
β”‚  4. TRACK                               β”‚
β”‚     See total spent                     β”‚
β”‚     See total saved                     β”‚
β”‚     Per-message cost breakdown          β”‚
β”‚                                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ—οΈ Architecture

Base Mini App ←→ Mini App API ←→ P402.io API ←→ AI Providers
                     β”‚
                     β”œβ”€β”€ /api/session   β†’ P402 session management
                     β”œβ”€β”€ /api/fund      β†’ Base Pay β†’ P402 credits
                     β”œβ”€β”€ /api/chat      β†’ Streaming AI completions
                     β”œβ”€β”€ /api/providers β†’ Model listing
                     └── /api/balance   β†’ Real-time balance

πŸ“ File Structure

p402-miniapp/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ .well-known/
β”‚   β”‚   └── farcaster.json/
β”‚   β”‚       └── route.ts          # Manifest endpoint
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ session/route.ts      # Session management
β”‚   β”‚   β”œβ”€β”€ fund/route.ts         # Base Pay integration
β”‚   β”‚   β”œβ”€β”€ chat/route.ts         # AI completions (streaming)
β”‚   β”‚   β”œβ”€β”€ providers/route.ts    # Model listing
β”‚   β”‚   β”œβ”€β”€ balance/route.ts      # Balance check
β”‚   β”‚   └── webhook/route.ts      # Mini app events
β”‚   β”œβ”€β”€ globals.css
β”‚   β”œβ”€β”€ layout.tsx
β”‚   └── page.tsx
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ Chat.tsx                  # Chat UI + input
β”‚   β”œβ”€β”€ ConnectScreen.tsx         # Onboarding
β”‚   β”œβ”€β”€ FundModal.tsx             # Base Pay modal
β”‚   β”œβ”€β”€ Header.tsx                # Balance display
β”‚   └── ModelSelector.tsx         # Model picker
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ p402-client.ts            # P402 API client
β”‚   β”œβ”€β”€ store.ts                  # Zustand state
β”‚   └── types.ts                  # TypeScript types
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ icon.png                  # 200x200 app icon
β”‚   β”œβ”€β”€ splash.png                # 1200x1200 splash
β”‚   β”œβ”€β”€ og-image.png              # 1200x630 social
β”‚   └── screenshots/              # App store images
β”œβ”€β”€ .env.example
β”œβ”€β”€ next.config.js
β”œβ”€β”€ package.json
β”œβ”€β”€ tailwind.config.ts
└── tsconfig.json

πŸš€ Quick Start

1. Install Dependencies

cd p402-miniapp
npm install

2. Configure Environment

cp .env.example .env.local

Edit .env.local:

NEXT_PUBLIC_URL=https://mini.p402.io
P402_API_URL=https://p402.io

3. Run Locally

npm run dev
# Open http://localhost:3001

4. Deploy to Vercel

npm i -g vercel
vercel --prod

5. Create Assets

Required images in /public:

File Size Purpose
icon.png 200x200 App icon
splash.png 1200x1200 Loading screen
og-image.png 1200x630 Social sharing
screenshots/chat.png 1170x2532 Store screenshot
screenshots/models.png 1170x2532 Store screenshot
screenshots/savings.png 1170x2532 Store screenshot

6. Sign Manifest

  1. Go to https://base.dev/preview?tab=account
  2. Enter your deployed URL
  3. Click "Verify" and sign with Base Account
  4. Copy credentials to .env.local:
MANIFEST_HEADER=eyJ...
MANIFEST_PAYLOAD=eyJ...
MANIFEST_SIGNATURE=MHg...
  1. Redeploy:
vercel --prod

7. Test Your App

  1. Go to https://base.dev/preview
  2. Enter your app URL
  3. Click "Launch" to test

8. Publish

Create a post in the Base app with your app URL. It will be indexed automatically.

πŸ”Œ P402.io Integration Points

Session Management

// Create or get session for wallet
POST /api/v2/sessions
{
  "wallet_address": "0x...",
  "source": "base_miniapp"
}

// Get session
GET /api/v2/sessions/{session_id}

Funding

After Base Pay completes:

POST /api/v2/sessions/fund
{
  "session_id": "sess_xxx",
  "amount": "5.00",
  "tx_hash": "0x...",
  "source": "base_pay"
}

Chat Completions

POST /api/v2/chat/completions
Headers:
  x-p402-session: sess_xxx
Body:
{
  "model": "groq/llama-3.3-70b-versatile",
  "messages": [{"role": "user", "content": "Hello"}],
  "stream": true
}

Response includes cost breakdown:

{
  "cost": {
    "input_tokens": 10,
    "output_tokens": 50,
    "total_cost": 0.0001,
    "direct_cost": 0.0008,
    "savings": 0.0007,
    "savings_percent": 87
  }
}

πŸ’³ Base Pay Integration

The mini app uses @base-org/account for USDC payments:

import { pay, getPaymentStatus } from '@base-org/account';

// Trigger payment
const payment = await pay({
  amount: '5.00',
  to: P402_TREASURY,  // 0xb23f...
});

// Wait for confirmation
const { status } = await getPaymentStatus({ id: payment.id });

if (status === 'completed') {
  // Credit P402 session
  await fundSession(amount, payment.transactionHash);
}

πŸ“Š Analytics Events

The webhook receives:

  • frame_added - User installed app
  • frame_removed - User uninstalled
  • notifications_enabled - User enabled notifications
  • notifications_disabled - User disabled notifications

🎨 Design

Uses P402 neo-brutalist design system:

  • Black background (#000)
  • Lime primary (#B6FF2E)
  • No rounded corners
  • 2px borders
  • Bold uppercase labels

πŸ“ˆ Conversion Flow

Base App User
    ↓
Discovers P402 in app directory
    ↓
Opens mini app β†’ Connect screen
    ↓
Connects Base Account β†’ P402 session created
    ↓
Adds $5 USDC via Base Pay
    ↓
Starts chatting β†’ sees real-time savings
    ↓
"I saved 70%!" β†’ shares to followers
    ↓
Continues using OR upgrades to p402.io

πŸ”— Links

πŸ“ License

MIT - Part of P402.io

About

Mini App for P402.io

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors