Overview
This quickstart guide will get you running the simplest demo - a basic HTTP paywall using the x402 protocol. You’ll see how an AI agent or API client can autonomously make a payment to access a protected endpoint.Time to complete: ~5 minutes
What you’ll build: A payment-protected API endpoint
What you’ll build: A payment-protected API endpoint
What You’ll Learn
By the end of this guide, you’ll have:- A running Express server with x402 payment protection
- Understanding of the 402 Payment Required flow
- Ability to test payments with curl
Prerequisites
Before starting, make sure you have:Node.js 18+
Download from nodejs.org
Git
Install from git-scm.com
Step 1: Clone the Repository
Step 2: Configure the Server
Step 3: Install and Run
Step 4: Test the Payment Flow
Now let’s test the payment-protected endpoint. Open a new terminal window:What Just Happened?
The x402 Payment Protocol
The x402 Payment Protocol
The x402 protocol extends HTTP with payment requirements. When a client tries to access a protected resource:
- Server returns
402 Payment Requiredwith payment details - Client signs a payment authorization (EIP-712 signature)
- Client retries request with
X-PAYMENTheader - Server verifies signature and settles payment on-chain
- Server returns the protected resource
Why use HTTP 402?
Why use HTTP 402?
- Native to HTTP: The 402 status code was reserved since HTTP/1.1 (1997) for payments
- Automatic handling: Clients can automatically detect and handle payment requirements
- No custom protocols: Works with standard HTTP tools and libraries
- AI-friendly: Agents can understand and respond to 402 without special training
How does the server work?
How does the server work?
The ping server uses The middleware intercepts requests and returns 402 until payment is verified.
x402-express middleware:Next Steps
Now that you’ve seen the basic payment flow, you can:Generate Payment Headers
Learn how to create valid payment signatures and make successful requests
Explore More Demos
Try the weather API demo with dynamic pricing
Build an Agent
Create an autonomous agent that makes payments
Deploy to Production
Learn how to deploy to production with mainnet
Common Issues
Port 3000 already in use
Port 3000 already in use
If port 3000 is already in use, you can change it by editing
src/server.ts:npm install fails
npm install fails
Make sure you’re using Node.js 18 or higher:
Can't clone repository
Can't clone repository
If you’re behind a corporate firewall, try using HTTPS:
Learn More
x402 Protocol
Deep dive into the payment protocol
Smart Wallets
Learn about Crossmint smart wallets
API Reference
Explore the x402-express API