This x402 extension demonstrate how to use the x402 protocol to monetize your APIs and pages with micropayments.
It includes:
- NextJS middleware configuration to gate access to protected routes
- A protected page that requires a payment to access
- A protected chat interface with AI (each message requires a payment)
- A file download store where users pay to download files
- Protected API routes that require payments to access
- Scripts to send requests to protected endpoints via command line
Before you begin, you need to install the following tools:
- Node (>= v20.18.3)
- Yarn (v1 or v2+)
- Git
- Install the extension with
npx create-eth@latest -e carletex/x402-extension
- Go to the destination directory and start the frontend:
yarn start
Visit your app on: http://localhost:3000.
You can test the protected endpoints using command line scripts:
Test the builder API endpoint:
yarn send402requestTest the chat API with default message:
yarn send402chatTest the chat API with a custom message:
CHAT_MESSAGE="What is blockchain technology?" yarn send402chatMore examples:
CHAT_MESSAGE="Explain smart contracts" yarn send402chat
CHAT_MESSAGE="What is your name?" yarn send402chatThese scripts will automatically handle the x402 payment flow and display the responses in the console. You'll need to have a wallet with funds (faucet) on the network you're using (default is baseSepolia).
Navigate to http://localhost:3000/chat to access the AI chat interface. Each message you send will require a micropayment ($0.01) via x402. Make sure to connect your wallet first!
Navigate to http://localhost:3000/store to browse and download files. Each download requires a micropayment ($0.05) via x402. Features:
- Instant download after payment confirmation
- Secure payment via x402 protocol
- Files are delivered as browser downloads
Currently available:
- Resume Template - Professional resume template (ZIP, 7.7 KB) - $0.05
You can configure the extension by editing the .env file.
NEXT_PUBLIC_FACILITATOR_URL=
RESOURCE_WALLET_ADDRESS=
NETWORK=
GOOGLE_GENERATIVE_AI_API_KEY=
Generated by [🏗 Scaffold-ETH 2](https://scaffoldeth.io)