Embed automatic charitable giving into any payment flow. No extra steps. No separate UI. Just part of the transaction.
Attach a cause to any event — a trade, an API call, a subscription. Donations happen in the background.
Uses the x402 protocol for native HTTP stablecoin payments. USDC on Base — fast, cheap, and gas-efficient.
MIT licensed. Add your charity to the registry. Integrate with Express, Next.js, or the CLI.
Pick how you want to integrate — CLI, Express middleware, or Next.js middleware.
import express from 'express';
import { x402charity } from '@x402charity/express';
const app = express();
// Auto-donate 0.0001 USDC on every request to /api/trade
app.use('/api/trade', x402charity({
privateKey: process.env.X402_PRIVATE_KEY,
cause: 'testing-charity',
amount: '0.0001',
network: 'base-sepolia',
}));
app.post('/api/trade', (req, res) => {
// your trade logic — donation happens automatically
res.json({ ok: true });
});
app.listen(3000);
x402charity({
privateKey: '0x...', // Wallet private key (required)
cause: 'testing-charity', // Charity ID (required)
amount: '0.0001', // USDC per event (default: 0.0001)
network: 'base-sepolia', // base or base-sepolia (default)
silent: true, // Suppress errors (default: true)
shouldDonate: (req) => { // Optional filter function
return req.method === 'POST';
},
});
// middleware.ts
import { x402charity } from '@x402charity/next';
export default x402charity({
privateKey: process.env.X402_PRIVATE_KEY,
cause: 'testing-charity',
amount: '0.0001',
network: 'base-sepolia',
matcher: '/api/*', // Only donate on API routes
});
// Or match multiple paths:
// matcher: ['/api/trade', '/api/subscribe']
import { X402CharityClient, listCharities } from '@x402charity/core';
// List available charities
const charities = listCharities();
console.log(charities);
// Create a client and donate
const client = new X402CharityClient({
privateKey: process.env.X402_PRIVATE_KEY,
network: 'base-sepolia',
});
const receipt = await client.donate('testing-charity', '0.01');
console.log('Tx hash:', receipt.txHash);
The core library gives you X402CharityClient, listCharities(), findCharity(), and setCharities(). Use them to build donations into any workflow — cron jobs, webhooks, AI agents, or custom middleware.
Connect your wallet and make a test donation on Base Sepolia
Requires MetaMask or a compatible browser wallet
Live USDC donations to registered charities