The x402 protocol brings native crypto payments to HTTP. Build pay-per-use APIs, premium content gates, and microtransaction systems with a single TypeScript SDK.
$ npm install @wazabiai/x402
# Client usage
import { X402Client } from '@wazabiai/x402/client';
const client = new X402Client({
privateKey: process.env.PRIVATE_KEY
});
// 402 responses handled automatically ✨
const response = await client.fetch(
'https://api.example.com/premium'
);
A complete toolkit for building payment-enabled applications on the blockchain.
The client SDK intercepts 402 responses, signs EIP-712 payloads, and retries requests automatically. Zero manual intervention required.
Payments are signed with EIP-712 typed data, ensuring tamper-proof transactions verified on-chain.
Native support for BSC with USDT, USDC, BUSD, and WBNB tokens. More chains coming soon.
Drop-in middleware for Node.js servers. Protect any route with payment requirements in one line.
Full type safety with exported interfaces, Zod validation schemas, and intelligent autocomplete.
Separate entry points for client and server. Import only what you need to minimize bundle size.
A simple four-step flow that brings payments to any HTTP endpoint.
Client makes a standard HTTP request to a protected endpoint.
GET /api/premium
Server responds with payment requirements in the header.
X-Payment-Required: {...}
Client signs EIP-712 typed data with their private key.
signTypedData({...})
Server verifies signature and returns the protected resource.
200 OK { data: ... }
See the x402 protocol in action. Toggle between client and server implementations.
import { X402Client } from '@wazabiai/x402/client';
// Initialize client with your private key
const client = new X402Client({
privateKey: process.env.PRIVATE_KEY,
// Optional callbacks for payment events
onPaymentRequired: (requirement) => {
console.log('Payment needed:', requirement.amount);
},
onPaymentSigned: (payment) => {
console.log('Signed by:', payment.signer);
}
});
// Make requests - 402s are handled automatically!
const response = await client.fetch(
'https://api.example.com/premium-data'
);
console.log(response.data); // Premium content ✨
// HTTP verb shortcuts
await client.get('/api/resource');
await client.post('/api/resource', { data: 'value' });
await client.put('/api/resource', { update: true });
await client.delete('/api/resource');
import express from 'express';
import {
x402Middleware,
BSC_USDT,
parseTokenAmount
} from '@wazabiai/x402/server';
const app = express();
// Configure payment requirements
const paymentConfig = {
recipientAddress: '0xYourWalletAddress',
amount: parseTokenAmount('0.10', BSC_USDT.address).toString(),
tokenAddress: BSC_USDT.address,
description: 'Access to premium API',
};
// Protect routes with one line
app.use('/api/premium', x402Middleware(paymentConfig));
// Your protected route
app.get('/api/premium/data', (req, res) => {
// Payment verified ✓
const { x402 } = req;
console.log(`Paid by: ${x402?.signer}`);
res.json({
premium: 'content',
secret: 'data'
});
});
app.listen(3000);
import type {
PaymentRequirement,
PaymentPayload,
SignedPayment,
X402ClientConfig,
X402MiddlewareConfig,
} from '@wazabiai/x402/types';
// Payment requirement (402 response)
interface PaymentRequirement {
amount: string; // Wei amount
token: string; // Token address
network_id: string; // "eip155:56"
pay_to: string; // Recipient
expires_at?: number; // Unix timestamp
nonce?: string; // Replay protection
}
// EIP-712 payment payload
interface PaymentPayload {
amount: string;
token: string;
chainId: number;
payTo: string;
payer: string;
deadline: number;
nonce: string;
resource?: string;
}
// Signed payment for retry
interface SignedPayment {
payload: PaymentPayload;
signature: `0x${string}`;
signer: `0x${string}`;
}
Install the SDK and start accepting payments on your API immediately.
npm install @wazabiai/x402 viem
import { X402Client } from '@wazabiai/x402/client'
const client = new X402Client({ privateKey })
Accept payments in popular stablecoins and native tokens on BNB Smart Chain.
Tether USD
0x55d3...7955
USD Coin
0x8AC7...580d
Binance USD
0xe9e7...7D56
Wrapped BNB
0xbb4C...095c
Everything you need to integrate x402 into your project.
Browse the source code, report issues, and contribute to the project.
View on GitHub →Install the SDK from npm and get started in seconds.
View on NPM →Comprehensive guides, API references, and examples.
Read Docs →Explore transactions and verify contracts on BNB Smart Chain.
View Explorer →Join the future of programmable payments. Start accepting crypto in minutes.