buildx402
v3.0.0
Published
The Swiss Army knife for x402 on Aptos — scaffold, inspect, and pay
Downloads
1,698
Maintainers
Readme
buildx402
The Swiss Army knife for x402 on Aptos — scaffold, inspect, and pay from the terminal.
Quick Start
# Scaffold a new x402 app
npx buildx402 my-app
# Generate a wallet
npx buildx402 wallet create
# Inspect any x402 endpoint
npx buildx402 inspect https://your-app.vercel.app/api/fortune
# Pay and fetch any x402 endpoint
npx buildx402 pay https://your-app.vercel.app/api/fortune --key 0xYOUR_KEYWhat is buildx402?
buildx402 is a developer toolkit for the x402 payment protocol on Aptos. It handles the full lifecycle — from scaffolding a new project to inspecting and paying x402-protected endpoints directly from the command line.
Commands
| Command | Description |
|---------|-------------|
| buildx402 <app-name> | Scaffold a new x402 Next.js app |
| buildx402 wallet create | Generate a new Aptos Ed25519 wallet |
| buildx402 inspect <url> | Inspect payment requirements of any x402 endpoint |
| buildx402 pay <url> | Pay and fetch any x402-protected endpoint |
Commands in Detail
Scaffold a New App
npx buildx402 my-payment-apiCreates a new x402 application with:
- Template download from GitHub
- Project setup and cleanup
- Interactive dependency installation
- Next steps instructions
Generate a Wallet
npx buildx402 wallet createGenerates a new Aptos Ed25519 keypair and displays:
PAYMENT_RECIPIENT_ADDRESS=0x1234567890abcdef...
NEXT_PUBLIC_APTOS_PRIVATE_KEY=0xabcdef1234567890...Copy these to .env.local in your project.
Inspect an Endpoint
npx buildx402 inspect https://your-app.vercel.app/api/fortuneProbes any URL and parses the 402 response to show payment requirements:
x402 Endpoint Detected!
URL: https://your-app.vercel.app/api/fortune
x402 Version: 2
Description: Your Fortune Awaits
Content Type: application/json
Payment Options (1):
Scheme: exact
Price: 0.01 USDC
Raw Amount: 10000 atomic units
Network: Aptos Testnet
Pay To: 0xaaea...e6f4
Asset: 0x6909...2832
Timeout: 300s
Sponsored: trueWorks with any x402 implementation — parses payment data from headers (base64, JSON) and response body. If the endpoint isn't x402-protected, it tells you that too.
Pay an Endpoint
npx buildx402 pay https://your-app.vercel.app/api/fortune --key 0xYOUR_PRIVATE_KEYHandles the full x402 payment flow from the terminal:
- Loads your wallet from
--key, env variable, or.env.local - Hits the endpoint and detects the 402 response
- Builds and signs the Aptos transaction
- Sends payment through the facilitator
- Retries the request with payment proof
- Displays the response and transaction receipt
Wallet loaded: 0xaaea4890...49e6f4
x402 client ready
Status: 200 OK
Payment Receipt:
Transaction: 0x4bd6...e9a6
Explorer: https://explorer.aptoslabs.com/txn/0x4bd6...e9a6?network=testnet
Response:
{
"fortune": "Your keys, your fortune."
}Key resolution order:
--keyflag:buildx402 pay <url> --key 0xYOUR_KEY- Environment variable:
NEXT_PUBLIC_APTOS_PRIVATE_KEY .env.localfile in current directory
Options:
-k, --key <private-key>— Aptos private key (hex)-m, --method <method>— HTTP method: GET, POST, PUT (default: GET)-d, --data <body>— Request body for POST/PUT requests
Step-by-Step Guide
1. Create Your App
npx buildx402 my-appPress Y when asked to install dependencies.
2. Generate Wallet
npx buildx402 wallet create3. Configure Environment
Create .env.local in your project root:
PAYMENT_RECIPIENT_ADDRESS=0x...
NEXT_PUBLIC_APTOS_PRIVATE_KEY=0x...4. Fund Your Wallet (Testnet)
- APT (gas fees): Aptos Faucet
- USDC (payments): Circle Faucet
5. Start Development
cd my-app
npm run dev6. Test with the CLI
# In another terminal:
npx buildx402 inspect http://localhost:3000/api/fortune
npx buildx402 pay http://localhost:3000/api/fortune --key 0xYOUR_KEYCustomization
Change API Pricing
Edit middleware.ts:
const paymentRoutes = {
"/api/fortune": {
accepts: [{
price: "0.05", // Change from 0.01 to 0.05 USDC
// ...
}],
},
};Add New Payment-Gated Routes
- Create the endpoint in
app/api/my-route/route.ts - Add to
middleware.tspayment routes - Update the matcher config
Security Best Practices
- Never commit
.env.localto version control - Use testnet ONLY for development
- For production, use hardware wallets or secure key management
- Never share private keys
- Monitor wallet balances
Tech Stack
| Layer | Technology |
|-------|-----------|
| CLI | Commander.js, Chalk, Ora |
| Scaffolding | Degit |
| Blockchain | Aptos SDK (Ed25519) |
| Payment Client | x402 V2 (@rvk_rishikesh/fetch, @rvk_rishikesh/aptos) |
| Template | Next.js 16, TypeScript 5, Tailwind CSS 4 |
Resources
Contributing
Contributions welcome! Check out the template repo for issues and planned features.
License
MIT
Built for the Aptos x x402 Hackathon
