跳转至主要内容
返回示例应用

x402

这是一个演示Quicknode x402 示例应用,x402 用于按请求付费的区块链 API 访问。连接您的钱包,通过 SIWE 进行身份验证,并使用 EVM 支付签名调用跨多条链的 RPC/REST 方法。

前端框架/库:
React
语言:
TypeScript
构建工具/开发服务器:
Next.js
示例应用预览

概述

This sample app demonstrates Quicknode's x402 protocol for pay-per-request blockchain API access. The x402 protocol enables seamless micropayments for API calls — each request is paid for with an EVM signature, removing the need for traditional API keys or subscription plans.

Connect your wallet, authenticate with Sign-In with Ethereum (SIWE), and request testnet USDC from the faucet. When you execute blockchain methods, if you don't have enough credits, the app will automatically purchase test credits using your testnet USDC. Execute RPC/REST methods across multiple networks including Ethereum, Base Sepolia, Arbitrum, Polygon, and Aptos.

建筑

Wallet ──► SIWE Auth ──► JWT Token


x402 Fetch Wrapper (JWT + EVM payment signature)


Quicknode x402 API ──► Blockchain RPC / REST

Each API request is wrapped by @x402/fetch, which automatically signs a payment authorization using your connected wallet. The x402 backend verifies the signature, deducts credits, and proxies the request to the target blockchain network.

功能


  • Wallet Connection via ConnectKit (MetaMask, Coinbase Smart Wallet, etc.)
  • SIWE Authentication to receive a session JWT
  • Testnet USDC Faucet on Base Sepolia — credits are automatically purchased with testnet USDC when needed
  • Multi-Chain RPC/REST Execution across Ethereum, Base Sepolia, Arbitrum, Polygon, and Aptos
  • Payment Metadata Inspection returned with each response
  • JSON-RPC, REST, gRPC-Web, and WebSocket method support

Supported Methods

As all JSON-RPC, REST, gRPC-Web, and WebSocket methods are supported, you can call any method available on the target network. For demonstration, the app includes buttons for popular methods:

方法议定书网络
eth_blockNumberJSON-RPCBase Sepolia, Arbitrum, Polygon
eth_gasPriceJSON-RPCBase
eth_chainIdJSON-RPCEthereum
Ledger InfoRESTAptos
Block by HeightRESTAptos

先决条件


  • Node.js v18+
  • pnpm v8+
  • A browser wallet (e.g., MetaMask) with Base Sepolia testnet configured

项目结构

src/
├── app/ # Next.js App Router (pages & layouts)
├── components/
│ ├── providers.tsx # Wagmi + React Query + ConnectKit providers
│ └── wallet-connection-shell.tsx
├── hooks/
│ ├── use-x402-auth.ts # SIWE authentication state
│ ├── use-credits.ts # Credit balance fetching
│ └── use-x402-fetch.ts # Authenticated fetch wrapper
└── lib/
├── constants.ts # x402 URLs, chain IDs, contract addresses
├── methods.ts # Available RPC/REST method definitions
├── types.ts # TypeScript types
└── x402.ts # Core x402 logic (auth, credits, execution)

环境变量

变量默认描述
NEXT_PUBLIC_X402_BASE_URLhttps://x402.quicknode.com/x402 API base URL

Create a .env.local file to override, but the default points to the public x402 endpoint which you can use for testing without any setup.

NEXT_PUBLIC_X402_BASE_URL=https://x402.quicknode.com/

入门指南

1. 克隆该代码库

git clone https://github.com/quiknode-labs/qn-guide-examples.git
cd qn-guide-examples/sample-dapps/x402-quicknode

2. 安装依赖项

pnpm 安装

3. Start the development server

pnpm dev

Open http://localhost:3000 in your browser. Connect your wallet, authenticate with SIWE, request testnet USDC from the faucet, and start making pay-per-request API calls — credits are automatically purchased with your testnet USDC when needed.


预览

预览

投稿与反馈
我们非常期待收到您的反馈,并欢迎大家为本示例应用贡献力量!
如需报告问题或提供反馈,请在 qn-guide-示例 存储库。
如需贡献,请按照以下步骤操作:
  1. 分叉该仓库
  2. 创建一个功能分支:
    git checkout -b feature/amazing-feature
  3. 提交您的更改:
    git commit -m "添加超棒的功能"
  4. 推送您的分支:
    git push origin feature/amazing-feature
  5. 提交一个拉取请求。