Vue.js App Example with X402 Payment Support¶
This example demonstrates how to create a Vue.js 3 application with X402 payment requirements for API endpoints, including a separate Express API server with payment protection and client-side payment handling.
Features¶
- Vue 3 with Composition API - Modern Vue using
<script setup>syntax - Express API Server - Separate backend with
@openlibx402/expressmiddleware - On-Chain Payment Verification - Uses
SolanaPaymentProcessorto verify transactions on Solana devnet - Client-side Payment Handling - Vue components with automatic payment flow
- Free and Premium Endpoints - Mix of public and paid content
- TypeScript - Full type safety
- Vite - Lightning-fast dev server and HMR
- Tailwind CSS - Modern styling
- X402 Protocol - Blockchain-based micropayments
- Solana Wallet Integration - Support for Phantom, Solflare, and more
Architecture¶
This example uses a client-server architecture:
- Frontend (Port 5173): Vue.js app built with Vite
- Backend (Port 3001): Express.js API server with X402 middleware
- Proxy: Vite proxies /api/* requests to the Express server
Getting Started¶
Installation¶
Configuration¶
Create a .env file:
Running the App¶
For separate development:
API Endpoints¶
Free Endpoints¶
- GET /api - API information (no payment)
- GET /api/free-data - Free data access (no payment)
Paid Endpoints¶
- GET /api/premium-data - Premium data (0.10 USDC)
- GET /api/expensive-data - AI inference (1.00 USDC)
- GET /api/tiered-data/:tier - Tiered content (0.05 USDC)
- POST /api/process-data - Data processing (0.25 USDC)
Key Features¶
Backend (Express Server)¶
API routes are protected using the paymentRequired middleware which:
- Validates payment authorization headers
- Verifies amounts and addresses
- Performs on-chain verification in production mode
- Returns 402 Payment Required if payment is invalid or missing
Frontend (Vue.js)¶
Vue composables and components handle: - useWallet: Solana wallet connection and management - useX402Client: X402 payment client lifecycle - EndpointCard: Component for free endpoints - X402EndpointCard: Component for paid endpoints with payment flow - Automatic 402 response handling with payment retry logic
Payment Flow¶
- User initiates request to protected endpoint
- Server responds with 402 Payment Required
- Client displays payment request details
- User simulates or signs real payment
- Client retries request with payment authorization
- Server verifies payment and returns content
On-Chain Verification¶
- Development mode: Mock payments (no blockchain verification)
- Production mode: Full on-chain verification using Solana devnet RPC
- Enabled via
NODE_ENV=productionenvironment variable