Skip to content

BuidlGuidl/x402-example

Repository files navigation

x402 (Scaffold-ETH 2 extension)

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

Requirements

Before you begin, you need to install the following tools:

Quickstart

  1. Install the extension with
npx create-eth@latest -e carletex/x402-extension
  1. Go to the destination directory and start the frontend:
yarn start

Visit your app on: http://localhost:3000.

Testing Protected Endpoints

You can test the protected endpoints using command line scripts:

Test the builder API endpoint:

yarn send402request

Test the chat API with default message:

yarn send402chat

Test the chat API with a custom message:

CHAT_MESSAGE="What is blockchain technology?" yarn send402chat

More examples:

CHAT_MESSAGE="Explain smart contracts" yarn send402chat
CHAT_MESSAGE="What is your name?" yarn send402chat

These 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).

Using the Chat Interface

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!

Using the File Store

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

Configuration

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)