How to Implement a Crypto Paywall with x402 Payment Protocol | BlockEden.xyz Guide
Overview
x402 is an open protocol for internet-native payments that leverages the HTTP 402 "Payment Required" status code. With x402, you can implement paywalls behind API requests, webpages, and more without the traditional friction of credit card processing, KYC, and high transaction fees.
In this guide, you'll utilize x402 as a seller and buyer on the Base Sepolia testnet via building a simple HTML/JS web app using Express as the backend to protect content behind a paywall and allow users to pay using cryptocurrency on the testnet.
Let's get started!
What You Will Do
- Learn about x402 and how it works
- Set up a HTML/JS web app with an Express server with x402 integration
- Test the web app in a local environment with Base Sepolia
- Integrate BlockEden.xyz RPC endpoints for blockchain interactions
What You Will Need
- Basic understanding of programming and blockchain concepts
- Node.js installed (v22+)
- An EVM-compatible wallet with ETH and USDC on Base Sepolia blockchain
- A BlockEden.xyz account for RPC access
What is x402?
x402 is a chain-agnostic protocol built around the HTTP 402 Payment Required status code that enables services to charge for access to their APIs and content directly over HTTP. This open payment standard allows clients to programmatically pay for resources without accounts, sessions, or credential management. With x402, any web service can require payment before serving a response, using crypto-native payments for speed and privacy.
Who is x402 for?
- Sellers: Service providers who want to monetize their APIs or content. x402 enables direct, programmatic payments from clients with minimal setup.
- Buyers: Developers and AI agents seeking to access paid services without accounts or manual payment flows.
Both sellers and buyers interact directly through HTTP requests, with payment handled transparently through the protocol.
Core Components of x402
Client/Server Architecture
Client Role (Buyer)
- Initiates requests to access paid resources
- Processes 402 responses and extracts payment details
- Submits payment with the X-PAYMENT header
Server Role (Seller)
- Defines payment requirements with HTTP 402 responses
- Verifies incoming payment payloads
- Provides the requested resource once payment confirms
Facilitators
Facilitators simplify the payment process by:
- Verifying payment payloads
- Settling payments on the blockchain for servers
- Removing the need for servers to implement complex blockchain interactions
Having a Facilitator is optional, but it is recommended to use one. BlockEden.xyz provides enterprise-grade x402 facilitator services at https://x402.blockeden.xyz, offering sub-200ms settlement on Sui and multi-network support including Ethereum, Base, Polygon, and Avalanche.
x402 Payment Flow
The x402 protocol leverages ERC-3009 TransferWithAuthorization standard to enable gasless transfers, a key component for frictionless web3 monetization. Let's cover the flow of using x402 along with specs to comply with the standard.
- Client requests a resource from a server
- Server responds with 402 Payment Required and payment instructions:
{
"maxAmountRequired": "0.10",
"resource": "/api/market-data",
"description": "Access requires payment",
"payTo": "0xABCDEF1234567890ABCDEF1234567890ABCDEF12",
"asset": "0xA0b86991C6218b36c1d19D4a2e9Eb0cE3606EB48",
"network": "ethereum-mainnet"
}
- Client prepares payment based on requirements
- Client retries with X-PAYMENT header containing signed payload
- Server verifies payment via facilitator
- Server settles payment on blockchain
- Server delivers resource once payment confirms
Now that we have a good understanding of what x402 is and how it works under the hood, let's move onto building a simple x402 demo. However, we will first need to fulfill some project prerequisites but if you already have ETH on Base Sepolia and an RPC endpoint, feel free to skip to the Project Set Up section.
Project Prerequisite: Retrieve USDC on Base Sepolia
You will need some USDC on Base Sepolia in order to demonstrate a simple x402 payment.
To retrieve USDC, navigate to the Circle USDC faucet and request some funds. Once you have USDC on Base Sepolia, you can move onto the next step.
Project Prerequisite: BlockEden.xyz Base Endpoint
To interact with Base Sepolia, you'll need an API endpoint to communicate with the network. BlockEden.xyz provides fast, reliable RPC endpoints for Base and other blockchain networks.
Setting Up Your BlockEden.xyz Endpoint
-
Sign up for BlockEden.xyz: Visit https://blockeden.xyz and create a free account
-
Create an API Key: Once logged in, navigate to your dashboard and create a new API key
-
Select Base Sepolia: Choose Base Sepolia as your network
-
Copy Your Endpoint: Your RPC endpoint will look like:
https://api.blockeden.xyz/base_sepolia/YOUR_API_KEY -
Configure Your Wallet: Add this endpoint to your Web3 wallet's RPC settings as the Base Sepolia blockchain
BlockEden.xyz offers:
- Fast response times with global CDN
- High reliability and uptime
- Generous free tier for development
- Support for multiple blockchain networks
- Advanced analytics and monitoring
Project Set Up
With the prerequisites out of the way, let's get into project setup. The demo we will be building in this guide will do the following:
- Allow a user to connect their wallet and sign a message that will be used to verify the payment
- The server will then authenticate the 402 status code was met
- Respond with the paywalled API request or webpage
Let's get started!