A Next.js 16 application demonstrating payment-based access control using the x402 protocol on the Cardano blockchain. This demo shows how to implement a paywall system where users must connect their Cardano wallet and make a small payment to access protected resources using the X-PAYMENT header.
This project implements the x402 protocol, which extends HTTP with payment-based authentication. Users are required to:
- Connect their Cardano wallet (supports Yoroi, Lace, Eternl).
- Pay 0.01 USDM (Policy:
c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad) to access protected resources. - The backend verifies the transaction on-chain via Blockfrost before serving the content.
- Frontend: Next.js 16 (App Router), Tailwind CSS, Radix UI.
- Wallet Integration:
@cardano-foundation/cardano-connect-with-walletfor multi-wallet support. - Transaction Building: Client-side transaction construction using
lucid-cardano. - Backend: Next.js API Routes acting as both Resource Server and Facilitator.
- Verification: Server-side on-chain verification using
@blockfrost/blockfrost-js. - Deployment: Vercel-ready with WASM support configured.
- Framework: Next.js 16
- Styling: Tailwind CSS
- State Management: Jotai
- Cardano Libs:
lucid-cardano,@blockfrost/blockfrost-js - Animations: Motion
- Node.js 18+
- pnpm (recommended) or npm/yarn
- A Cardano wallet (Yoroi, Lace, or Eternl)
- A Blockfrost Project ID (Mainnet for this demo as per config)
-
Clone the repository:
git clone <repository-url> cd x402-example-cardano
-
Install dependencies:
pnpm install # or npm install -
Set up environment variables:
Create a
.env.localfile in the root directory:# Required for backend verification BLOCKFROST_PROJECT_ID=your_mainnet_project_id # Required for client-side transaction building (demo only) NEXT_PUBLIC_BLOCKFROST_PROJECT_ID=your_mainnet_project_id # URL for the facilitator (same as app URL for local) FACILITATOR_URL=http://localhost:3000/api/facilitator NETWORK=cardano-mainnet
-
Run the development server:
pnpm run dev
Open http://localhost:3000 with your browser.
This project requires specific configuration for WASM support (used by Cardano crypto libraries).
- Build Command: The
package.jsonis configured to force Webpack:next build --webpack - Config:
next.config.tsenablesasyncWebAssembly.
Deploy normally via Vercel CLI or Git integration. Ensure you add the Environment Variables in the Vercel dashboard.
- Resource Request: Client requests protected resource.
- Payment Required (402): Server responds with 402 and
PaymentRequirements(address, amount, asset). - Payment Submission: Client signs transaction, encodes it in
X-PAYMENTheader, and retries request. - Verification: Server verifies
X-PAYMENTheader, submits transaction to network (if needed), and monitors confirmation. - Access Granted: Once confirmed, Server responds with 200 OK and the resource.
- WASM Issues: If you see errors related to
@peculiar/webcryptoorlucid-cardano, ensure you are runningpnpm run devwhich uses the updated webpack config flags. - Wallet Connection: Ensure your wallet is set to the correct network (Mainnet) to match the demo config.