@payai/x402-hono-starter
v1.0.2
Published
Create an x402 Hono server in less than 2 minutes!
Readme
x402 Hono Starter
A Hono application with x402 payment protocol integration, providing a modern web framework alternative to Next.js with the same payment functionality.
Features
- Hono Framework: Fast, lightweight web framework for Bun/Node.js
- x402 Payment Integration: Built-in payment middleware for micro-payments
- TypeScript Support: Full TypeScript support with proper type definitions
- Tailwind CSS: Utility-first CSS framework for styling
- API Routes: RESTful API endpoints with payment protection
- Static File Serving: Built-in static file serving for assets
Getting Started
Requirements
- Node.js: 18 or newer
Create a new app using the template
npm (npx)
npm exec @payai/x402-hono-starter -- my-x402-apppnpm
pnpm dlx @payai/x402-hono-starter my-x402-appbun
bunx @payai/x402-hono-starter my-x402-appThen inside your new app:
npm run devManual Installation
- Clone or download this starter template
- Install dependencies:
bun install- Copy the environment file:
cp env.local .env.local- Update your environment variables in
.env.local:
# Replace with your actual wallet address
RESOURCE_WALLET_ADDRESS=YourAddress
# Facilitator URL
FACILITATOR_URL=https://facilitator.payai.network- Start the development server:
bun run dev- Open http://localhost:3000 in your browser
Project Structure
src/
├── index.ts # Main application entry point
├── routes/
│ ├── home.tsx # Home page route
│ ├── premium.tsx # Premium content page
│ └── api/
│ ├── weather/ # Weather API endpoint
│ └── premium/
│ └── content/ # Premium content API
├── styles/
│ └── globals.css # Global styles with Tailwind
└── ...API Endpoints
Free Endpoints
GET /- Home pageGET /premium- Premium content page
Protected Endpoints (Require Payment)
GET /api/weather- Weather data (requires $0.001 USDC)GET /api/premium/content- Premium content (requires custom token payment)
Payment Configuration
The payment middleware is configured in src/index.ts:
const paymentConfig = {
'/api/weather': {
price: '$0.001',
network: 'base-sepolia',
},
'/api/premium/*': {
price: {
amount: '100000',
asset: {
address: '0xabc',
decimals: 18,
eip712: {
name: 'WETH',
version: '1',
},
},
},
network: 'base-sepolia',
},
};Available Scripts
bun run dev- Start development server with hot reloadbun run build- Build the application for productionbun run start- Start the production serverbun run lint- Run ESLintbun run format- Format code with Prettierbun run format:check- Check code formatting
Development
The application uses:
- Hono for the web framework
- x402-hono for payment middleware
- TypeScript for type safety
- Tailwind CSS for styling
- ESLint and Prettier for code quality
Deployment
The application can be deployed to any platform that supports Bun or Node.js:
- Build the application:
bun run build - Start the production server:
bun run start
License
Apache-2.0
