x402 E2E Test Server
Overview
Setup
Prerequisites
- Node.js and pnpm installed
- Environment variables configured:
EVM_PRIVATE_KEY=0x... # Private key for facilitator account EVM_PAYEE_ADDRESS=0x... # Address to receive payments PORT=4021 # Optional: server port (defaults to 4021)
Installation
pnpm install
Running the Server
pnpm run start
Or for development with auto-reload:
pnpm run dev
Endpoints
Protected Endpoint
- GET /protected - Requires $0.001 USDC payment on Base Sepolia
- Returns success message with timestamp when payment is valid
Utility Endpoints
- GET /health - Health check (no payment required)
- POST /close - Gracefully shutdown server (for testing)
Architecture
Components
index.ts- Main server file with Express app and route configurationfacilitator.ts- Local facilitator setup for payment processing
Payment Flow
- Client requests protected endpoint
- Server returns 402 Payment Required with payment instructions
- Client creates and signs payment
- Client retries request with payment signature
- Server verifies payment via facilitator
- If valid, server processes request and settles payment
- Server returns response with settlement confirmation
Testing
This server is used by the e2e test suite to verify x402 client implementations. The local facilitator allows testing without relying on external services.
Running E2E Tests
From the project root:
pnpm run test:e2e
Production Considerations
For production use:
- Replace the local facilitator with a remote facilitator service
- Configure appropriate payment amounts and networks
- Add proper error handling and logging
- Implement rate limiting and security measures
- Use environment-specific configuration
Troubleshooting
Common Issues
-
"EVM_PRIVATE_KEY environment variable is required"
- Ensure
.envfile exists with required variables - Check that dotenv is loading correctly
- Ensure
-
"Server already running on port"
- Check for existing processes:
lsof -i :4021 - Kill existing process or use different port
- Check for existing processes:
-
Payment verification fails
- Verify private key matches the expected account
- Check network configuration matches client
- Ensure sufficient balance for gas fees