whop-x402
v1.0.0
Published
Whop x402 Payment Gateway Adapter for AI Agents
Maintainers
Readme
@whop-x402
An open-source, framework-agnostic x402-compatible adapter that allows Node.js/TypeScript-based AI agents to accept payments via Whop (subscriptions, memberships, digital goods) and automatically route native revenue into BNB Chain PancakeSwap on-chain buybacks.
Features
- x402 protocol support: Emits challenge payloads compatible with the x402 agent-to-agent protocol.
- On-chain routing: Executes native BNB swaps for utility tokens on PancakeSwap V2 (BSC).
- Price Oracle: Automatic CoinGecko USD-to-BNB price tracking.
- Webhook validation: Secure HMAC-SHA256 signature verification of Whop payments.
- Event hooks: Typed Node EventEmitter callbacks for custom triggers.
Installation
npm install @whop-x402
# or
bun add @whop-x402Quick Start (Express Webhook Example)
Here is how to set up the adapter with an Express server to monetize your AI agent:
import Express from 'express';
import WhopX402Adapter from '@whop-x402';
const app = Express();
app.use(Express.raw({ type: 'application/json' })); // raw body is required for HMAC validation
const adapter = new WhopX402Adapter(
{
apiKey: process.env.WHOP_API_KEY!,
webhookSecret: process.env.WHOP_WEBHOOK_SECRET!,
},
{
rpcUrl: process.env.BSC_RPC_URL || 'https://bsc-dataseed.binance.org/',
chainId: 56, // 56 for Mainnet, 97 for Testnet
walletPrivateKey: process.env.WALLET_PRIVATE_KEY!,
tokenAddress: process.env.BUYBACK_TOKEN_ADDRESS!, // Target agent utility token
buybackPercentage: 100, // Swap 100% of revenue to token
slippagePercentage: 0.5,
}
);
// Register Event Hooks
adapter.on('payment_received', (event) => {
console.log(`💰 Payment of ${event.amountCents} cents received from ${event.payer}`);
});
adapter.on('buyback_executed', (event) => {
console.log(`🔄 Buyback Executed: Swapped ${event.amountBNB} BNB. Tx Hash: ${event.txHash}`);
});
adapter.on('buyback_failed', (event) => {
console.error(`❌ Buyback Failed: ${event.error}`);
});
// Webhook Ingestion Route
app.post('/whop-webhook', async (req, res) => {
const signature = req.headers['x-whop-signature'] as string;
if (!signature) {
return res.status(400).send('Missing x-whop-signature header');
}
// Pass raw body string and header signature to the adapter
const rawBody = req.body.toString('utf8');
const result = await adapter.handlePaymentWebhook(rawBody, signature);
if (!result.success) {
return res.status(400).json(result);
}
return res.json(result);
});
app.listen(3000, () => console.log('Agent payment gateway active on port 3000'));Fetch.ai & Agentverse
The adapter is framework-agnostic, so it plugs into any Fetch.ai agent stack. Because the ASI:One Chat Protocol and uAgents run in Python, the recommended topology for a Node.js agent is:
- TypeScript webhook service (
examples/agentverse-webhook-agent.ts) — an Express server exposingGET /status,GET /checkout(returns the Whop checkout / x402 payment challenge),POST /whop-webhook(HMAC-verified, triggers the PancakeSwap buyback), andGET /proof(latest on-chain tx hash). Deploy it and expose it with a tunnel (cloudflared tunnel --url http://localhost:8080). - Python chat agent (
../python/examples/agentverse_chat_agent.py) — an ASI:One Chat Protocol uAgent that hands buyers the checkout link and surfaces the buyback proof.
Veridex Agent Fabric Integration
We also provide a fully-equipped, production-grade example using our core framework @veridex/agents:
- Veridex Chat Agent (
examples/veridex-chat-agent.ts) — A conversational AI agent constructed with the declarativecreateAgentfactory. It wraps the payment adapter inside a safe, typed Tool (create_checkout_link) and implements on-chain proof retrieval (get_latest_buyback_proof) out of the box!
To run the Veridex Chat Agent:
bun run examples/veridex-chat-agent.tsTo run the standalone Webhook agent:
bun run examples/agentverse-webhook-agent.tsLicense
MIT License.
