init Command
The init command creates a configuration file and test wallet for your project.
npx x402test init [options]What It Does
Section titled “What It Does”- Creates Configuration File: Generates
x402test.config.js - Creates Test Wallet: Generates and funds a test wallet
- Saves Wallet: Stores wallet in
.x402test-wallets.json
Options
Section titled “Options”—force, -f
Section titled “—force, -f”Overwrite existing configuration file.
npx x402test init --forceUse Case: Recreate configuration after accidental modification
Output
Section titled “Output”✔ Initializing x402test configuration...✔ Creating new x402test config...✔ Creating test wallet...✔ Config file created at x402test.config.js✔ Recipient wallet: FcxKSp7YxqYXdq...✔ USDC balance: 1000 USDC✔ Ready to start your server! Run 'x402test start' to start the server.Generated Files
Section titled “Generated Files”x402test.config.js
Section titled “x402test.config.js”// x402test configurationexport default { port: 4402, network: "solana-devnet", rpcUrl: "http://localhost:8899",
recipient: "FcxKSp7YxqYXdq...",
routes: { "/api/premium": { price: "0.10", description: "Premium content access", response: { data: "This is premium content!", timestamp: Date.now(), }, },
"/api/data": { price: "0.01", description: "Data API access", response: (req) => ({ method: req.method, path: req.path, data: { message: "Your data here" }, }), }, },};.x402test-wallets.json
Section titled “.x402test-wallets.json”{ "wallets": [{ "publicKey": "FcxKSp7YxqYXdq...", "secretKey": [...], "tokenAccounts": { "USDC": "EPjFWdd5AufqSSqeM2..." } }], "mints": { "USDC": "EPjFWdd5AufqSSqeM2..." }}Important: Add to .gitignore!
.x402test-wallets.jsonExamples
Section titled “Examples”Basic Initialization
Section titled “Basic Initialization”npx x402test initForce Overwrite
Section titled “Force Overwrite”npx x402test init --forceUse when you want to:
- Reset configuration to defaults
- Fix corrupted config file
- Start fresh after experiments
Customizing Generated Config
Section titled “Customizing Generated Config”After running init, edit x402test.config.js:
Change Port
Section titled “Change Port”export default { port: 8080, // Change from 4402 // ... rest of config};Add Routes
Section titled “Add Routes”export default { // ... existing config routes: { "/api/premium": { price: "0.10", description: "Premium content", response: { data: "Premium content" }, }, "/api/basic": { price: "0.01", description: "Basic content", response: { data: "Basic content" }, }, // Add your routes here "/api/custom": { price: "0.05", description: "Custom endpoint", response: { custom: true }, }, },};Use Environment Variables
Section titled “Use Environment Variables”export default { port: parseInt(process.env.PORT || "4402"), rpcUrl: process.env.RPC_URL || "http://localhost:8899", recipient: process.env.RECIPIENT_WALLET || "default-wallet", // ... rest of config};Error Handling
Section titled “Error Handling”Config Already Exists
Section titled “Config Already Exists”✘ Config file already exists. Use --force to overwrite.Solution:
npx x402test init --forceWallet Creation Failed
Section titled “Wallet Creation Failed”✘ Failed to create wallet: Connection failedSolutions:
- Check Solana validator is running
- Verify RPC URL is correct
- Check network connectivity
Permission Denied
Section titled “Permission Denied”✘ EACCES: permission deniedSolutions:
- Check file permissions
- Run in different directory
- Use
sudo(not recommended)
Best Practices
Section titled “Best Practices”- Version Control: Commit
x402test.config.js, not.x402test-wallets.json - Security: Never share or commit wallet files
- Team Setup: Each developer runs
initindependently - CI/CD: Run
initin CI pipeline for testing
.gitignore Setup
Section titled “.gitignore Setup”Add these to .gitignore:
.x402test-wallets.json.x402test-signatures.jsonNext Steps
Section titled “Next Steps”After running init:
- Review Config: Check
x402test.config.jssettings - Start Server: Run
npx x402test start - Make Request: Test with x402 client
- Customize: Add your own routes and responses
Related Commands
Section titled “Related Commands”- start Command - Start the server
- routes Command - View configured routes
Troubleshooting
Section titled “Troubleshooting”Fresh Start
Section titled “Fresh Start”If something goes wrong:
rm x402test.config.jsrm .x402test-wallets.jsonrm .x402test-signatures.json
npx x402test initWallet Balance Issues
Section titled “Wallet Balance Issues”rm .x402test-wallets.jsonnpx x402test initThe new wallet will have 1000 USDC again.