Boilerplate for building out an api on the X402 payment protocol for monetizing API endpoints.
402-api-boilerplate is an early-stage JavaScript project in the AI payments / x402 ecosystem. It currently has 4 GitHub stars and 5 forks.
Boilerplate for building out an api on the X402 payment protocol for monetizing API endpoints.
Live demo at 402api.com
Edit and add API Endpoints in server.js and public/config.js The server exposes the following API endpoints (GET or POST):
/text-to-imageprompt (string): The text prompt for image generation (defaults to "a beautiful landscape").image URL and prompt./word-counttext (string): The text for which to count words.wordCount and original text./sentiment-analysistext (string): The text to analyze.sentiment, original text, and scores (positive and negative word counts).GET /index.html page from the public directory.Install Dependencies:
npm install
# or
yarn install
Environment Variables:
Create a .env file in the root of the project with the following variables:
# Your wallet address that will receive payments
WALLET_ADDRESS=your_wallet_address_here
# OpenAI API key for DALL-E image generation
OPENAI_API_KEY=your_openai_api_key_here
# CDP API key ID
CDP_API_KEY_ID=your_cdp_api_key_id_here
# CDP API key secret
CDP_API_KEY_SECRET=your_cdp_api_key_secret_here
Start the Server:
node server.js
The server will start on http://localhost:4021.
Install Dependencies (if not already done):
npm install
# or
yarn install
Environment Variables:
Add the following to your .env file for testing buy:
# Private key for the wallet (for testing buy)
PRIVATE_KEY=0xPrivateKeyHere
Test Buy:
node testBuy.js
express: Web framework for Node.js.x402-express: Express middleware for X402 payments.@coinbase/x402: Facilitator for X402 protocol.dotenv: Loads environment variables from a .env file.openai: OpenAI Node.js library for accessing the DALL-E API.Learn more about the 402 payment protocol at x402.org.
The agent-native LLM router for autonomous agents. 55+ models (8 free), <1ms local routing, USDC payments on Base & Solana via x402.
A payments protocol for the internet. Built on HTTP.
A local-first AI agent with persistent memory, emotional intelligence, and a peer-to-peer skills economy.
The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.
Self-healing infrastructure for AI agent payments. 90.3% auto-recovery.
The AI agent with a wallet — spends USDC autonomously to get real work done. Apache-2.0, TypeScript.