Why Base?
Base is an L2 on Ethereum that's cheap ($0.001-0.01/tx) and fast (1 second blocks). Perfect for micro-payment use cases like x402.
What We'll Build
A simple payment gateway that accepts USDC and triggers an API response.
Prerequisites
- Node.js
- viem (ethereum library)
- A wallet with some ETH on Base for gas
The Contract
// Simple payment verification
contract PaymentGateway {
IERC20 public usdc;
address public payTo;
uint256 public price;
function pay() external {
usdc.transferFrom(msg.sender, payTo, price);
emit PaymentReceived(msg.sender, price);
}
}
Deployment
# Using viem + Base
npx wagmi init
# Deploy to Base Mainnet
npx hardhat run scripts/deploy --network base
The API Integration
On the backend, verify the payment and process the request:
app.post('/api/service', async (req, res) => {
const { txHash } = req.body;
const verified = await verifyPayment(txHash);
if (!verified) return res.status(402).json({ error: 'payment required' });
const result = await processRequest(req.body);
res.json(result);
});
Cost
Deploying to Base: ~$0.50 in gas
Per transaction: ~$0.001 in gas
Per API call: $0.01 USDC + $0.001 gas = $0.011 total
Resources
Built on Base with β€οΈ
Top comments (0)