@dexterai/x402
v1.8.2
Published
Full-stack x402 SDK - add paid API monetization to any endpoint. Express middleware, React hooks, Access Pass, dynamic pricing. Solana, Base, Polygon, Arbitrum, Optimism, Avalanche, SKALE.
Maintainers
Readme
What is x402?
x402 is a protocol for HTTP-native micropayments. When a server returns HTTP status 402 Payment Required, it includes payment details in a PAYMENT-REQUIRED header. The client signs a payment transaction and retries the request with a PAYMENT-SIGNATURE header. The server verifies and settles the payment, then returns the protected content.
This SDK handles the entire flow automatically—you just call fetch() and payments happen transparently. With Access Pass mode, buyers pay once and get unlimited access for a time window—no per-request signing needed.
Why This SDK?
Monetize any API in minutes. Add payments to your server in ~10 lines. Clients pay automatically—no checkout pages, no subscriptions, no invoices. Just HTTP.
Dynamic pricing. Charge based on usage: characters, tokens, records, pixels, API calls—whatever makes sense. Price scales with input, not fixed rates.
Token-accurate LLM pricing. Built-in tiktoken support prices AI requests by actual token count. Works with OpenAI models out of the box, or bring your own rates for Anthropic, Gemini, Mistral, or local models.
Access Pass. Pay once, get unlimited access for a time window. Buyers connect a wallet, make one payment, and receive a JWT token that works like an API key—no per-request signing, no private keys in code. The Stripe replacement for crypto-native APIs.
Full-stack. Client SDK for browsers, server SDK for backends. React hooks, Express middleware patterns, facilitator client—everything you need.
Multi-chain. Solana and Base (Ethereum L2) with the same API. Add wallets for both and the SDK picks the right one automatically.
Works out of the box. Built-in RPC proxy, pre-flight balance checks, automatic retry on 402. Uses the Dexter facilitator by default—Solana's most feature-rich x402 facilitator.
Automatic Marketplace Discovery
When someone pays for your API through the Dexter facilitator, your endpoint is automatically discovered and listed in the OpenDexter Marketplace — a searchable directory of 5,000+ paid APIs used by AI agents.
No registration step needed. The flow:
- You add
x402Middlewareto your endpoint (see Quick Start below) - An agent pays for your API → the facilitator processes the settlement
- Your endpoint is auto-discovered, AI-named, and quality-verified
- Agents find it via
x402_searchin any MCP client (ChatGPT, Claude, Cursor, etc.)
Quality-verified endpoints (score 75+) get promoted in search results. The verification bot tests your endpoint automatically — no action required on your part.
Quick Start
Install
npm install @dexterai/x402Client (Node.js)
The simplest way to make x402 payments from scripts:
import { wrapFetch } from '@dexterai/x402/client';
const x402Fetch = wrapFetch(fetch, {
walletPrivateKey: process.env.SOLANA_PRIVATE_KEY,
});
// That's it. 402 responses are handled automatically.
const response = await x402Fetch('https://api.example.com/protected');Check the payment receipt:
import { wrapFetch, getPaymentReceipt } from '@dexterai/x402/client';
const x402Fetch = wrapFetch(fetch, { walletPrivateKey: process.env.SOLANA_PRIVATE_KEY });
const response = await x402Fetch('https://api.example.com/protected');
const receipt = getPaymentReceipt(response);
if (receipt) {
console.log('Paid:', receipt.transaction, 'on', receipt.network);
}Client (Browser)
import { createX402Client } from '@dexterai/x402/client';
const client = createX402Client({
wallets: {
solana: solanaWallet,
evm: evmWallet,
},
});
// That's it. 402 responses are handled automatically.
const response = await client.fetch('https://api.example.com/protected');RPC URLs are optional—the SDK uses Dexter's RPC proxy by default. Override if needed:
const client = createX402Client({
wallets: { solana: solanaWallet },
rpcUrls: {
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp': 'https://your-rpc.com',
},
});React
Works with @solana/wallet-adapter-react and wagmi out of the box:
import { useX402Payment } from '@dexterai/x402/react';
import { useWallet } from '@solana/wallet-adapter-react'; // Solana
import { useAccount } from 'wagmi'; // EVM (Base)
function PayButton() {
// Get wallets from your existing providers
const solanaWallet = useWallet();
const evmWallet = useAccount();
const { fetch, isLoading, balances, transactionUrl } = useX402Payment({
wallets: {
solana: solanaWallet, // Pass directly - SDK handles the interface
evm: evmWallet,
},
});
return (
<div>
<p>Balance: ${balances[0]?.balance.toFixed(2)}</p>
<button
onClick={() => fetch('/api/protected')}
disabled={isLoading || !solanaWallet.connected}
>
{isLoading ? 'Paying...' : 'Pay'}
</button>
{transactionUrl && <a href={transactionUrl}>View Transaction</a>}
</div>
);
}Supported Networks
All networks supported by the Dexter facilitator. USDC on every chain.
Mainnets:
| Network | CAIP-2 | Status |
|---------|--------|--------|
| Solana | solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp | Production |
| Base | eip155:8453 | Production |
| Polygon | eip155:137 | Production |
| Arbitrum | eip155:42161 | Production |
| Optimism | eip155:10 | Production |
| Avalanche | eip155:43114 | Production |
| SKALE Base | eip155:1187947933 | Production (zero gas) |
Testnets:
| Network | CAIP-2 |
|---------|--------|
| Solana Devnet | solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1 |
| Base Sepolia | eip155:84532 |
| SKALE Sepolia | eip155:324705682 |
Accept payments on multiple chains simultaneously:
// Same address across EVM chains
app.get('/api/data', x402Middleware({
payTo: '0xYourAddress',
amount: '0.01',
network: ['eip155:8453', 'eip155:137', 'eip155:42161', 'eip155:10'],
}));
// Different addresses per chain family
app.get('/api/data', x402Middleware({
payTo: {
'solana:*': 'YourSolanaAddress...',
'eip155:*': '0xYourEvmAddress...',
},
amount: '0.01',
network: ['solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', 'eip155:8453', 'eip155:137'],
}));Package Exports
// Client - browser
import { createX402Client } from '@dexterai/x402/client';
// Client - Node.js (private key wallet)
import { wrapFetch, createKeypairWallet } from '@dexterai/x402/client';
// React hook
import { useX402Payment } from '@dexterai/x402/react';
// Server - Express middleware
import { x402Middleware } from '@dexterai/x402/server';
// Server - Access Pass (pay once, unlimited requests)
import { x402AccessPass } from '@dexterai/x402/server';
// Server - manual control
import { createX402Server } from '@dexterai/x402/server';
// Server - dynamic pricing
import { createDynamicPricing, createTokenPricing } from '@dexterai/x402/server';
// React - Access Pass hook
import { useAccessPass } from '@dexterai/x402/react';
// Chain adapters (advanced)
import { createSolanaAdapter, createEvmAdapter } from '@dexterai/x402/adapters';
// Utilities
import { toAtomicUnits, fromAtomicUnits } from '@dexterai/x402/utils';Utilities
import { toAtomicUnits, fromAtomicUnits } from '@dexterai/x402/utils';
// Convert dollars to atomic units (for API calls)
toAtomicUnits(0.05, 6); // '50000'
toAtomicUnits(1.50, 6); // '1500000'
// Convert atomic units back to dollars (for display)
fromAtomicUnits('50000', 6); // 0.05
fromAtomicUnits(1500000n, 6); // 1.5Server SDK
Express Middleware — NEW!
One-liner payment protection for any Express endpoint:
import express from 'express';
import { x402Middleware } from '@dexterai/x402/server';
const app = express();
app.get('/api/protected',
x402Middleware({
payTo: 'YourSolanaAddress...',
amount: '0.01', // $0.01 USD
}),
(req, res) => {
// This only runs after successful payment
res.json({ data: 'protected content' });
}
);Options:
payTo— Address to receive paymentsamount— Price in USD (e.g.,'0.01'for 1 cent)network— CAIP-2 network (default: Solana mainnet)description— Human-readable descriptionfacilitatorUrl— Override facilitator (default: x402.dexter.cash)verbose— Enable debug logging
Access Pass — Pay Once, Unlimited Requests
Replace API keys with time-limited access passes. Buyers make one payment and get a JWT token for unlimited requests during a time window.
Server:
import express from 'express';
import { x402AccessPass } from '@dexterai/x402/server';
const app = express();
// Protect all /api routes with access pass
app.use('/api', x402AccessPass({
payTo: 'YourSolanaAddress...',
tiers: {
'1h': '0.50', // $0.50 for 1 hour
'24h': '2.00', // $2.00 for 24 hours
},
ratePerHour: '0.50', // also accept custom durations
}));
app.get('/api/data', (req, res) => {
// Only runs with a valid access pass
res.json({ data: 'premium content' });
});Client (Node.js):
import { wrapFetch } from '@dexterai/x402/client';
const x402Fetch = wrapFetch(fetch, {
walletPrivateKey: process.env.SOLANA_PRIVATE_KEY,
accessPass: { preferTier: '1h', maxSpend: '1.00' },
});
// First call: auto-purchases a 1-hour pass ($0.50 USDC)
const res1 = await x402Fetch('https://api.example.com/api/data');
// All subsequent calls for the next hour: uses cached JWT, zero payment
const res2 = await x402Fetch('https://api.example.com/api/data');
const res3 = await x402Fetch('https://api.example.com/api/data');React:
import { useAccessPass } from '@dexterai/x402/react';
function Dashboard() {
const { tiers, pass, isPassValid, purchasePass, fetch: apFetch } = useAccessPass({
wallets: { solana: solanaWallet },
resourceUrl: 'https://api.example.com',
});
return (
<div>
{!isPassValid && tiers?.map(t => (
<button key={t.id} onClick={() => purchasePass(t.id)}>
{t.label} — ${t.price}
</button>
))}
{isPassValid && <p>Pass active! {pass?.remainingSeconds}s remaining</p>}
<button onClick={() => apFetch('/api/data')}>Fetch Data</button>
</div>
);
}How it works:
- Client requests a protected endpoint → Server returns
402withX-ACCESS-PASS-TIERSheader - Client selects a tier and pays via x402 → Server verifies, settles, issues a JWT
- Server returns
200withACCESS-PASSheader containing the JWT - Client caches the JWT and includes it as
Authorization: Bearer <token>on all subsequent requests - Server validates the JWT locally (no facilitator call) → instant response
Options:
payTo— Address to receive paymentstiers— Named duration tiers with prices (e.g.,{ '1h': '0.50' })ratePerHour— Rate for custom durations (buyer sends?duration=<seconds>)network— CAIP-2 network (default: Solana mainnet)secret— HMAC secret for JWT signing (auto-generated if not provided)facilitatorUrl— Override facilitator (default: x402.dexter.cash)
Manual Server (Advanced)
For more control over the payment flow:
import { createX402Server } from '@dexterai/x402/server';
const server = createX402Server({
payTo: 'YourAddress...',
network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
});
// In your route handler
app.post('/protected', async (req, res) => {
const paymentSig = req.headers['payment-signature'];
if (!paymentSig) {
const requirements = await server.buildRequirements({
amountAtomic: '50000', // $0.05 USDC
resourceUrl: req.originalUrl,
});
res.setHeader('PAYMENT-REQUIRED', server.encodeRequirements(requirements));
return res.status(402).json({});
}
const result = await server.settlePayment(paymentSig);
if (!result.success) {
return res.status(402).json({ error: result.errorReason });
}
res.json({ data: 'Your protected content' });
});Dynamic Pricing
Generic pricing for any use case - charge by characters, bytes, API calls, or any unit you define. No external dependencies.
Works for:
- LLM/AI endpoints (by character count)
- Image processing (by pixel count or file size)
- Data APIs (by record count)
- Any service where cost scales with input
import { createX402Server, createDynamicPricing } from '@dexterai/x402/server';
const server = createX402Server({ payTo: '...', network: '...' });
const pricing = createDynamicPricing({
unitSize: 1000, // chars per unit
ratePerUnit: 0.01, // $0.01 per unit
minUsd: 0.01, // floor
maxUsd: 10.00, // ceiling
});
app.post('/api/llm', async (req, res) => {
const { prompt } = req.body;
const paymentSig = req.headers['payment-signature'];
if (!paymentSig) {
const quote = pricing.calculate(prompt);
const requirements = await server.buildRequirements({
amountAtomic: quote.amountAtomic,
resourceUrl: req.originalUrl,
});
res.setHeader('PAYMENT-REQUIRED', server.encodeRequirements(requirements));
res.setHeader('X-Quote-Hash', quote.quoteHash);
return res.status(402).json({ usdAmount: quote.usdAmount });
}
// Validate quote hasn't changed (prevents prompt manipulation)
const quoteHash = req.headers['x-quote-hash'];
if (!pricing.validateQuote(prompt, quoteHash)) {
return res.status(400).json({ error: 'Prompt changed, re-quote required' });
}
const result = await server.settlePayment(paymentSig);
if (!result.success) return res.status(402).json({ error: result.errorReason });
const response = await runLLM(prompt);
res.json(response);
});The client SDK automatically forwards X-Quote-Hash on retry.
Token Pricing (LLM-Accurate)
Accurate token-based pricing for LLMs. Uses tiktoken for token counting. Supports OpenAI models out of the box, plus custom rates for Anthropic, Gemini, Mistral, or any model.
import { createX402Server, createTokenPricing, MODEL_PRICING } from '@dexterai/x402/server';
const server = createX402Server({ payTo: '...', network: '...' });
const pricing = createTokenPricing({
model: 'gpt-4o-mini', // Uses real OpenAI rates
// minUsd: 0.001, // Optional floor
// maxUsd: 50.0, // Optional ceiling
});
app.post('/api/chat', async (req, res) => {
const { prompt, systemPrompt } = req.body;
const paymentSig = req.headers['payment-signature'];
if (!paymentSig) {
const quote = pricing.calculate(prompt, systemPrompt);
const requirements = await server.buildRequirements({
amountAtomic: quote.amountAtomic,
resourceUrl: req.originalUrl,
description: `${quote.model}: ${quote.inputTokens.toLocaleString()} tokens`,
});
res.setHeader('PAYMENT-REQUIRED', server.encodeRequirements(requirements));
res.setHeader('X-Quote-Hash', quote.quoteHash);
return res.status(402).json({
inputTokens: quote.inputTokens,
usdAmount: quote.usdAmount,
model: quote.model,
tier: quote.tier,
});
}
// Validate quote hasn't changed
const quoteHash = req.headers['x-quote-hash'];
if (!pricing.validateQuote(prompt, quoteHash)) {
return res.status(400).json({ error: 'Prompt changed, re-quote required' });
}
const result = await server.settlePayment(paymentSig);
if (!result.success) return res.status(402).json({ error: result.errorReason });
const response = await openai.chat.completions.create({
model: pricing.config.model,
messages: [{ role: 'user', content: prompt }],
max_completion_tokens: pricing.modelInfo.maxTokens,
});
res.json({
response: response.choices[0].message.content,
transaction: result.transaction,
});
});Available Models
import { MODEL_PRICING, getAvailableModels } from '@dexterai/x402/server';
// Get all models sorted by tier and price
const models = getAvailableModels();
// → [{ model: 'gpt-5-nano', inputRate: 0.05, tier: 'fast' }, ...]
// Check pricing for a specific model
MODEL_PRICING['gpt-4o-mini'];
// → { input: 0.15, output: 0.6, maxTokens: 4096, tier: 'fast' }Supported tiers: fast, standard, reasoning, premium, custom
Custom Models (Anthropic, Gemini, etc.)
Not using OpenAI? Pass your own rates:
// Anthropic Claude
const pricing = createTokenPricing({
model: 'claude-3-sonnet',
inputRate: 3.0, // $3.00 per 1M input tokens
outputRate: 15.0, // $15.00 per 1M output tokens
maxTokens: 4096,
});
// Google Gemini
const pricing = createTokenPricing({
model: 'gemini-1.5-pro',
inputRate: 1.25,
outputRate: 5.0,
});
// Custom/local model with custom tokenizer
const pricing = createTokenPricing({
model: 'llama-3-70b',
inputRate: 0.50,
tokenizer: (text) => llamaTokenizer.encode(text).length,
});tiktoken's default encoding works well for most transformer models. Only use a custom tokenizer if your model has significantly different tokenization.
Sponsored Access (v1.7.2)
Server middleware accepts sponsoredAccess: true in its config. When enabled, it reads extensions["sponsored-access"] from the facilitator's settlement response and injects _x402_sponsored into the JSON response body so agents can see recommendations.
app.use(x402Middleware({
facilitatorUrl: "https://x402.dexter.cash",
sponsoredAccess: true, // opt-in to recommendation injection
}));Client SDK decodes the PAYMENT-RESPONSE header from x402 responses and attaches the full settlement receipt (including extensions) as response._x402 for programmatic access.
Types: SettleResponse now includes an optional extensions field.
API Reference
createX402Client(options)
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| wallets | { solana?, evm? } | Yes | Multi-chain wallets |
| wallet | SolanaWallet | No | Single Solana wallet (legacy) |
| preferredNetwork | string | No | Prefer this network when multiple options available |
| rpcUrls | Record<string, string> | No | RPC endpoints per network (defaults to Dexter proxy) |
| maxAmountAtomic | string | No | Maximum payment cap |
| verbose | boolean | No | Enable debug logging |
x402AccessPass(options)
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| payTo | string | Yes | Address to receive payments |
| tiers | Record<string, string> | One of tiers or ratePerHour | Named tiers (e.g., { '1h': '0.50' }) |
| ratePerHour | string | One of tiers or ratePerHour | USD rate for custom durations |
| network | string | No | CAIP-2 network (default: Solana mainnet) |
| secret | Buffer | No | HMAC secret for JWT (auto-generated) |
| facilitatorUrl | string | No | Facilitator URL (default: x402.dexter.cash) |
| verbose | boolean | No | Enable debug logging |
useX402Payment(options)
Returns:
| Property | Type | Description |
|----------|------|-------------|
| fetch | function | Payment-aware fetch |
| isLoading | boolean | Payment in progress |
| status | string | 'idle' | 'pending' | 'success' | 'error' |
| error | X402Error? | Error details if failed |
| transactionId | string? | Transaction signature |
| transactionUrl | string? | Block explorer link |
| balances | Balance[] | Token balances per chain |
| refreshBalances | function | Manual refresh |
| reset | function | Clear state |
| accessPass | object? | Active pass state (tier, expiresAt, remainingSeconds) |
useAccessPass(options)
| Option | Type | Required | Description |
|--------|------|----------|-------------|
| wallets | { solana?, evm? } | Yes | Multi-chain wallets |
| resourceUrl | string | Yes | The x402 resource base URL |
| preferredNetwork | string | No | Prefer this network |
| autoConnect | boolean | No | Auto-fetch tiers on mount (default: true) |
Returns:
| Property | Type | Description |
|----------|------|-------------|
| tiers | AccessPassTier[]? | Available tiers from server |
| pass | object? | Active pass (jwt, tier, expiresAt, remainingSeconds) |
| isPassValid | boolean | Whether pass is active and not expired |
| purchasePass | function | Buy a pass for a tier or custom duration |
| isPurchasing | boolean | Purchase in progress |
| fetch | function | Fetch with auto pass inclusion |
Development
npm run build # Build ESM + CJS
npm run dev # Watch mode
npm run typecheck # TypeScript checksLicense
MIT — see LICENSE
