This is a Next.js project bootstrapped with create-next-app.
This project demonstrates a complete integration of the x402 Payment Protocol with Nevermined to create a powerful system for AI agent monetization and agent-to-agent interactions.
Traditional Web3 applications require users to sign blockchain transactions for every interaction, creating a poor user experience and limiting automated agent interactions. Our solution combines:
- x402 for one-time payments - Make a single cryptocurrency payment to purchase credits
- Nevermined for credit management - Use purchased credits to access multiple AI agents
- Agent-to-agent interactions - Let your AI agents pay other agents without additional signatures
This eliminates the need for constant transaction signing or using vulnerable session keys, creating a seamless experience for both end-users and AI agents.
sequenceDiagram
participant User
participant Browser
participant DynamicAuth as Dynamic.xyz Auth
participant useX402Hook as useX402 Hook
participant NeverminedAPI as Nevermined API
participant Blockchain as Base Sepolia
participant AgentA as AI Agent A
participant AgentB as AI Agent B
Note over User, AgentB: 1. Initial Payment Flow
User->>Browser: Visit application
Browser->>DynamicAuth: Connect wallet
User->>Browser: Purchase credits with x402
Browser->>useX402Hook: makePayment('/api/nevermined-credits')
useX402Hook->>Blockchain: Send USDC payment
useX402Hook->>NeverminedAPI: Allocate credits to user
NeverminedAPI->>Browser: Return credit balance
Note over User, AgentB: 2. Agent-to-Agent Interaction
User->>Browser: Request task from Agent A
Browser->>AgentA: Send task request + JWT
AgentA->>NeverminedAPI: Verify credits and deduct
AgentA->>AgentB: Request assistance with task
AgentB->>NeverminedAPI: Verify credits and deduct
AgentB->>AgentA: Return results
AgentA->>Browser: Return combined results
Browser->>User: Display final output
- Unified Authentication - Secure wallet authentication via Dynamic.xyz
- One-time Payments - Purchase credits via x402 protocol with a single transaction
- Credit System - Manage AI agent access with Nevermined credits
- Agent Economy - Enable agent-to-agent transactions without user intervention
- Testnet Integration - Built on Base Sepolia for easy testing without real funds
- Next.js - React framework for the frontend
- Dynamic.xyz - Web3 wallet authentication
- x402 - Payment protocol using HTTP 402 status code
- Nevermined - Credit-based system for AI agent economy
- Viem - Ethereum interaction library
- Base Sepolia - Testnet for transactions
- Node.js (v16+)
- pnpm
- MetaMask or other Web3 wallet
- Testnet USDC on Base Sepolia (available from Circle Faucet)
-
Clone the repository:
git clone https://github.com/yourusername/x402-nevermined.git cd x402-nevermined -
Install dependencies:
pnpm install
-
Create a
.env.localfile:# x402 Configuration NEXT_PUBLIC_PAYMENT_RECIPIENT_ADDRESS="your-address" NEXT_PUBLIC_USDC_CONTRACT_ADDRESS="0x036CbD53842c5426634e7929541eC2318f3dCF7e" NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID="your-dynamic-id" # Nevermined Configuration NEXT_PUBLIC_NEVERMINED_ENVIRONMENT=appBase NEXT_PUBLIC_NEVERMINED_RETURN_URL=http://localhost:3000/callback NEXT_PUBLIC_NEVERMINED_APP_ID=your-app-id NEXT_PUBLIC_NVM_AGENT_DID=did:nv:your-agent-id -
Start the development server:
pnpm dev
-
Open http://localhost:3000 with your browser.
Before using the demo, you need to:
- Create your own Nevermined Payment Plan and AI agent at https://base.nevermined.app
- Add your Nevermined API keys and agent DIDs to the
.env.localfile
- Better User Experience: Users make just one payment for multiple AI interactions
- Enhanced Security: No need for session keys or constant signatures
- Agent Autonomy: AI agents can operate and pay other agents without user intervention
- Scalability: Perfect for complex AI workflows involving multiple specialized agents
- Economy of Scale: Acquire credits in bulk, then use them across multiple services
MIT
-
hooks/useX402.ts: Primary hook for handling x402 payments- Connects to user's wallet via Dynamic Auth
- Manages payment flow including blockchain transactions
-
components/NeverminedProvider.tsx: Context provider for Nevermined SDK- Manages credit allocation and verification
- Handles agent authentication and authorization
-
app/api/nevermined-credits/route.ts: Credits purchase endpoint- Implements the 402 Payment Required response
- Converts x402 payments to Nevermined credits
-
app/api/premium-content/route.ts: Example protected content endpoint- Verifies Nevermined credits
- Serves premium content based on credit verification
- Agent Registration: Register AI agents with Nevermined
- Credit-Based Access: Set credit costs for agent usage
- JWT Authentication: Access agents using JWT tokens
- Agent-to-Agent Communication: Enable agents to pay other agents with credits
Set up the following environment variables in .env.local:
# x402 Configuration
NEXT_PUBLIC_PAYMENT_RECIPIENT_ADDRESS="0x..."
NEXT_PUBLIC_USDC_CONTRACT_ADDRESS="0x036CbD53842c5426634e7929541eC2318f3dCF7e"
NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID="your-dynamic-id"
# Nevermined Configuration
NEXT_PUBLIC_NEVERMINED_ENVIRONMENT=appArbitrum
NEXT_PUBLIC_NEVERMINED_RETURN_URL=http://localhost:3000/callback
NEXT_PUBLIC_NEVERMINED_APP_ID=your-app-id
- Connect your wallet using Dynamic Auth
- Ensure you have USDC on Base Sepolia testnet
- Get testnet USDC from the Base Sepolia Faucet
- Purchase Nevermined credits using x402 payment
- Access AI agents using your purchased credits
- Test agent-to-agent interactions without additional signatures
- Uses viem for blockchain interactions
- Implements the ERC-20 token standard for USDC payments
- Built on Base Sepolia testnet
- Authentication via Dynamic wallet authentication
- Credit management via Nevermined SDK
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out our Next.js deployment documentation for more details.
