x402-cli
A command-line tool for automating x402 project lifecycle management
About
x402-cli is a Rust-based command-line tool designed to automate the lifecycle of an x402 project. It acts as a bridge between a developer's local code, the Aptos blockchain, and the x402 facilitator.
Features
- ๐ Project Initialization - Scaffold new x402-enabled projects with configurable frameworks
- ๐ฐ Wallet Management - Create, save, and fund wallets for testnet/mainnet
- ๐ Facilitator Server - Start/stop local development facilitator with health checks
- ๐งช Payment Testing - End-to-end payment flow testing with detailed output
- ๐ Deployment - Deploy facilitators to platforms like Vercel
- ๐ Configuration - TOML-based project and environment configuration
- ๐จ Multiple Frameworks - Support for Next.js, React, and other frameworks
- โ Blockchain Support - Built-in Aptos blockchain integration
Installation
Prerequisites
- Rust: 1.70 or higher
- Cargo: Rust package manager (comes with Rust)
- Node.js & npm: Required for project initialization (for web frameworks)
- Operating System: macOS, Linux, or Windows
Build from Source
Clone the repository and build the CLI:
# Clone the repository
# Build in release mode for optimal performance
# The binary will be available at:
# target/release/x402-cli
Install System-Wide (Optional)
To make the CLI available system-wide:
# Copy to a directory in your PATH
# Or create a symlink
Verify Installation
# Check if CLI is installed
# Show help
Getting Started
Quick Start
Here's the fastest way to get started with x402-cli:
# 1. Initialize a new x402 project
# 2. Create a test wallet
# 3. Start the local facilitator
# 4. Test the payment flow
# 5. Deploy to production
Command Reference
init - Initialize a new project
Create a new x402-enabled project with the specified framework.
Options:
-n, --name <NAME>: Project name (required)-c, --chain <CHAIN>: Blockchain network (e.g., aptos)-f, --framework <FRAMEWORK>: Framework to use (e.g., next, react, vanilla)
Example:
Output:
- Creates project directory structure (src/, config/, tests/, docs/)
- Generates configuration files (x402.toml, .env.example, .gitignore)
- Installs framework dependencies
- Generates README with x402-specific commands
wallet - Manage wallets
Create and manage wallets for x402 transactions.
Subcommands:
create: Create a new wallet
Options for create:
-n, --network <NETWORK>: Network to use (default: testnet)
Example:
# Create a wallet on testnet
# Create a wallet on mainnet
Output:
- Generates a new Ed25519 key pair
- Creates a 12-word BIP39 seed phrase
- Saves wallet to
~/.x402/wallets/<address>.json - Funds wallet from faucet (testnet only)
facilitator - Manage facilitator server
Start and stop the local development facilitator server.
Subcommands:
start: Start the facilitator serverstop: Stop all facilitator processes
Options for start:
-p, --port <PORT>: Port to listen on (default: 3001)
Examples:
# Start facilitator on default port (3001) with auto-detected wallet
# Start facilitator on custom port
# Start facilitator on mainnet
# Start facilitator with specific wallet address
# Start facilitator with private key and custom network
# Stop facilitator
Options for start:
-p, --port <PORT>: Port to listen on (default: 3001)--wallet <ADDRESS>: Use wallet with this address (optional)--private-key <KEY>: Use wallet from this private key (optional)-n, --network <NETWORK>: Network to use (default: testnet)
Output:
- Starts a TCP server on the specified port
- Health check endpoint at
http://localhost:<port>/health - Handles payment facilitation requests
- Uses specified wallet for payment transactions (defaults to first found wallet)
test - Test payment flows
Test end-to-end payment flows to verify functionality.
Subcommands:
payment: Test a payment flow
Options for payment:
-a, --api <API>: API endpoint to test (required)-a, --amount <AMOUNT>: Amount to pay in micro-APT (default: 1000)
Example:
Output:
- Step-by-step payment flow progress
- Transaction hash and timing information
- Detailed error messages if failures occur
deploy - Deploy to production
Deploy your facilitator to production platforms.
Options:
-p, --provider <PROVIDER>: Deployment platform (e.g., vercel)
Example:
Output:
- Builds the project
- Checks for provider CLI installation
- Initiates deployment process
- Provides deployment URL
Configuration
Project Configuration (config/x402.toml)
= "my-project"
= "aptos"
= "next"
= "0.1.0"
[]
= 3000
= "localhost"
[]
= "aptos"
[]
= true
= 3001
Environment Variables (.env)
NODE_ENV=development
X402_CHAIN=aptos
X402_PROJECT=my-project
Wallet Storage
Wallets are stored securely in:
- Location:
~/.x402/wallets/<address>.json - Format: JSON with address, private_key, network, and seed_phrase
- Security: Files are not encrypted - ensure system security
Supported Frameworks
- Next.js: Full-featured React framework
- React: React library support
- Vanilla: Basic HTML/JavaScript setup
Architecture
User Input (CLI)
โ Command Parsing (Clap)
โ Command Routing (main.rs)
โ Module Handler (x402/mod.rs)
โ Domain Logic (project.rs/wallet.rs/facilitator.rs/test.rs/deploy.rs)
โ External Services (blockchain, file system, HTTP)
Development
Building
# Debug build
# Release build
Running Tests
# Run all tests
# Run specific test
Adding New Features
- Create new module in
src/x402/ - Add command enum in
src/main.rs - Export from
src/lib.rs - Add handler in
src/x402/mod.rs - Test functionality
Troubleshooting
Wallet Funding Fails
If faucet funding fails with 405 Method Not Allowed:
- The Aptos faucet API may have changed
- Fund the wallet manually using the Aptos explorer
- Check the faucet service status
Facilitator Won't Start
If the facilitator fails to start:
- Check if the port is already in use:
lsof -i :3001 - Use a different port:
x402-cli facilitator start --port 3002 - Check firewall settings
Payment Flow Fails
If payment testing fails:
- Ensure the API endpoint is accessible
- Verify the facilitator is running
- Check network connectivity
- Review error messages for specific issues
License
MIT License - see LICENSE file for details
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Resources
Changelog
v1.0.0 (2025-02-01)
- Initial release
- Project scaffolding with framework support
- Wallet creation and management
- Facilitator server management
- Payment flow testing
- Vercel deployment support
- Aptos blockchain integration