A production-grade TypeScript SDK for integrating x402 payments into your applications. Enable HTTP 402 Payment Required for your APIs with just a few lines of code.
- Middleware for Popular Frameworks: Express, Next.js, Hono, Fastify
- Wallet Adapters: EVM (Base, Ethereum) and Solana support
- Usage Metering: Track API usage and revenue
- Revenue Dashboard: Built-in analytics dashboard
- Retry-After-Payment: Automatic payment handling in clients
- Type-Safe: Full TypeScript support
- Production-Ready: Comprehensive error handling and validation
import express from 'express';
import { paymentMiddleware } from '@x402/server/express';
import { NetworkIdentifiers } from '@x402/core';
const app = express();
app.use(paymentMiddleware({
payTo: '0xYourAddress',
routes: {
'GET /api/premium': {
accepts: [{
scheme: 'exact',
price: '$0.01',
network: NetworkIdentifiers.BASE_MAINNET,
payTo: '0xYourAddress'
}],
description: 'Premium API access'
}
}
}));
app.get('/api/premium', (req, res) => {
res.json({ data: 'premium content' });
});import { createX402Fetch } from '@x402/client/fetch';
import { EVMWalletAdapter } from '@x402/wallet-adapters/evm';
const wallet = new EVMWalletAdapter({
privateKey: process.env.PRIVATE_KEY,
network: NetworkIdentifiers.BASE_MAINNET
});
const fetch = createX402Fetch({ wallet, autoPay: true });
// Automatically handles payments
const response = await fetch('https://api.example.com/premium');
const data = await response.json();@x402/core- Core types, utilities, and facilitator client@x402/server- Server middleware (Express, Next.js, Hono, Fastify)@x402/client- Client SDK (Fetch, Axios)@x402/wallet-adapters- Wallet adapters (EVM, Solana)@x402/metering- Usage metering and analytics@x402/dashboard- Revenue dashboard
# Install server packages
npm install @x402/core @x402/server
# Install client packages
npm install @x402/core @x402/client @x402/wallet-adapters
# Install metering and dashboard
npm install @x402/metering @x402/dashboard- Base Mainnet (
eip155:8453) - Base Sepolia (
eip155:84532) - Ethereum Mainnet (
eip155:1) - Ethereum Sepolia (
eip155:11155111)
- Solana Mainnet (
solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp) - Solana Devnet (
solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1)
┌─────────────────┐ 402 Payment Required ┌─────────────────┐
│ Client App │◄────────────────────────────►│ Server API │
│ (with wallet) │ Payment Payload (X-Payment) │ (x402 middleware)│
└────────┬────────┘ └────────┬────────┘
│ │
│ ┌──────────────────┐ │
└────────►│ Facilitator │◄─────────────────┘
│ (x402.org) │
│ - Verify │
│ - Settle │
└──────────────────┘
Apache-2.0