Billing infrastructure for AI agents. Drop-in middleware that adds a paywall to any HTTP API or MCP server. Agents pay in USDC on Solana — no banks, no credit cards, no human intervention.
npm install gate402
For API Developers
Add a paywall to any Express API
npm install gate402-agent
For Agent Operators
Pay APIs automatically on HTTP 402
npm install create-gate402-mcp
For MCP Developers
Monetize any MCP tool call
✓
Gate402 is MIT licensed. The npm packages are free forever. The hosted platform at gate402.dev is the commercial offering.
How it works
Every payment goes through 5 steps. Total time: under one second.
# With demo payment — works instantly, no blockchain
$ curl http://localhost:3000/api/data \
-H "X-Payment-Payload: demo_test_001"
✓ 200 OK — { "message": "You paid 0.001 USDC!" }
05
Test with a real agent
typescript
import { Gate402Agent } from 'gate402-agent'
const agent = new Gate402Agent({
privateKey: process.env.AGENT_WALLET_KEY,
network: 'devnet'
})
// Detects 402, pays automatically, retries
const res = await agent.fetch('http://localhost:3000/api/data')
const data = await res.json()
// { "message": "You paid 0.001 USDC!" }
06
See it in the dashboard
Open gate402.dev/dashboard — every call appears in real time with endpoint, amount, and status (demo on devnet, verified on mainnet).
Core concepts
x402 Protocol
HTTP 402 Payment Required is a status code defined in 1991. The x402 protocol defines how to use it for machine-to-machine payments. Backed by Google, Microsoft, Stripe, Coinbase, and Cloudflare.
Fee split
During early access, Gate402 charges no platform fee. You receive 100% of every payment directly in your wallet.
✓
Gate402 takes 0% platform fee during early access. 100% of every payment goes directly to your wallet. This will change when we exit early access — you will be notified in advance.
Demo mode
demo mode
# Any hash starting with demo_ bypasses blockchain
✓ Works on devnet — blocked automatically on mainnet
Demo mode is controlled by the network setting in your Gate402 dashboard, not by NODE_ENV.
⚠
On devnet: demo_ hashes are accepted for development. On mainnet: demo_ hashes are rejected with DEMO_NOT_ALLOWED_ON_MAINNET. Switch your network in gate402.dev/settings — Gate402 blocks demo payments on mainnet automatically.
Installation
Requirements
→Node.js 18+
→Express 4+
→Gate402 account at gate402.dev
→Solana wallet (Phantom, Backpack, or any)
install
# npm
$ npm install gate402
# yarn
$ yarn add gate402
# pnpm
$ pnpm add gate402
Basic setup
typescript
import { gate402 } from 'gate402'
import express from 'express'
const app = express()
app.use(gate402({
apiKey: process.env.GATE402_API_KEY,
walletAddress: process.env.SOLANA_WALLET, // optional — uses dashboard setting
serverUrl: 'https://api.gate402.dev',
network: 'devnet', // 'devnet' | 'mainnet'
endpoints: {
'/api/search': 0.001, // $0.001 per call
'/api/analyze': 0.010, // $0.010 per call
'/api/generate': 0.050, // $0.050 per call
}
}))
// Your handlers don't change at all
app.get('/api/search', (req, res) => {
res.json({ results: ['...'] })
})
app.listen(3000)
PropertyTypeReqDefaultDescription
apiKeystringYes—API key from gate402.dev/settings
walletAddressstringNo—Solana wallet to receive USDC. Falls back to dashboard setting.
serverUrlstringYes—Gate402 API URL for payment verification
network'devnet' | 'mainnet'No'devnet'Solana network for on-chain verification
endpointsRecord<string, number>No—Map of URL paths to prices in USDC. Omit to use managed mode.
Going to mainnet
When you're ready to receive real USDC payments, follow these steps.
01
Configure your Solana wallet
Go to gate402.dev/settings → Receiving Wallet. Add your Solana mainnet wallet address (Phantom, Backpack, or any Solana wallet).
02
Switch to mainnet
Go to gate402.dev/settings → Network → Mainnet. Or via API:
The public Solana RPC has rate limits. For production, use a dedicated RPC like Helius (free tier available). Go to helius.dev → create account → copy your RPC URL → paste in gate402.dev/settings.
04
Test with real USDC
typescript
import { Gate402Agent } from 'gate402-agent'
const agent = new Gate402Agent({
privateKey: process.env.AGENT_WALLET_KEY,
network: 'mainnet',
limits: { maxPerCall: 0.01, maxPerDay: 1.00 }
})
const res = await agent.fetch('https://your-api.dev/api/data')
05
Monitor in dashboard
Every payment appears in gate402.dev/dashboard in real time with status "verified" (on-chain confirmed) and the tx hash on Solscan.
⚠
Always test thoroughly on devnet before switching to mainnet. Demo payments are blocked on mainnet automatically.
Endpoint pricing
Each entry in endpoints maps a URL path to a price in USDC. Prices can be as low as 0.0001 USDC ($0.0001).
All payments land directly in your Solana wallet. There is no Gate402 escrow. You can withdraw or use USDC at any time through any Solana wallet (Phantom, Backpack, CLI).
✓
During early access, Gate402 takes 0% platform fee. You receive 100% of every configured price directly in your Solana wallet. This will change when we exit early access — you will be notified in advance.
API Key Management
Your API key authenticates requests to Gate402. Never expose it in client-side code or public repositories.
402PAYMENT_REQUIREDNo payment header. Response includes price and wallet.
402PAYMENT_ALREADY_USEDThis tx hash has already been consumed (anti-replay).
402PAYMENT_INVALIDTx not found on-chain or amount does not match.
402DEMO_NOT_ALLOWED_ON_MAINNETDemo mode is disabled on mainnet. Send real USDC or switch to devnet.
401INVALID_API_KEYx-api-key header contains an invalid or unknown key.
401MISSING_API_KEYx-api-key header not present in request.
403UPGRADE_REQUIREDThis feature requires a Pro plan. Upgrade at gate402.dev/billing.
400INVALID_PATHEndpoint path must start with / and contain only valid characters.
400INVALID_PRICEpriceUsdc must be a positive number between 0.0001 and 1000.
502UPSTREAM_UNAVAILABLEOrigin API did not respond (managed mode only).
500INTERNAL_ERRORUnexpected error. Check server logs.
Troubleshooting
"Invalid API key" error
Your x-api-key header is invalid or missing. Check gate402.dev/settings for your current API key. If you rotated recently, update your environment variables.
"Demo mode not allowed on mainnet"
Your account is set to mainnet but you sent a demo_ hash. Switch to devnet for testing, or send a real Solana transaction.
"Transaction not found"
The tx hash doesn't exist on the expected network. Common causes:
–Wrong network — sent mainnet tx but account is on devnet (or vice versa)
–Transaction not yet finalized — wait 1–2 seconds and retry
–Invalid tx hash format
"Insufficient amount: received 0, expected X"
The transaction was found but the recipient received 0 USDC. Common causes:
–Sender and recipient are the same wallet — use different wallets for testing
–Wrong recipient address — check your wallet in Settings
–RPC rate limit — use Helius for production
Payment goes through but dashboard shows 0
Your API key might not match the endpoint owner. Make sure you're passing x-api-key in the middleware config and that it matches the key in gate402.dev/settings.
Webhook not firing
Check that your webhook URL is publicly accessible. Test with POST /api/users/webhook/test. Webhooks only fire for payments through api.gate402.dev, not local MCP servers.