@dexterai/x402
Give your agent a spending limit it can't exceed.
Install
npm i @dexterai/x402BuyerPay per call with
Pay per call with payAndFetch
Drop-in replacement for fetch. Your agent pays the seller automatically — no wallet UI, no SDK init boilerplate. The tab caps total spend so a runaway loop can't drain you.
import { payAndFetch, createKeypairWallet } from '@dexterai/x402/client';
const solana = await createKeypairWallet(process.env.AGENT_KEY!);
// The seller's endpoint — payment is handled automatically.
const res = await payAndFetch('https://seller.example/v1/infer', {
method: 'POST',
body: JSON.stringify({ messages, max_tokens: 512 }),
}, { solana });
if (res.ok) {
const data = await res.response.json();
console.log(data);
}SellerMeter calls with
Meter calls with tabOrExactMiddleware
One middleware call gates your route. Accept either a tab (flat perUnit charge per call, reserved on-chain) or an exact one-time payment. The buyer's tab caps cumulative spend; you get paid per call.
import express from 'express';
import { Connection } from '@solana/web3.js';
import { tabOrExactMiddleware } from '@dexterai/x402/tab/seller';
const app = express();
const connection = new Connection(process.env.SOLANA_RPC_URL!);
// Each call costs perUnit USDC. The buyer's tab caps cumulative spend across
// all calls and reserves what's accrued on-chain — they can't be over-billed,
// you can't be drained.
app.post('/v1/infer',
tabOrExactMiddleware({
connection,
sellerPubkey: process.env.SELLER_SOLANA_ADDRESS!,
network: 'solana:mainnet',
perUnit: '0.002', // USDC per completion call
}),
async (req, res) => {
const result = await runInference(req.body);
res.json(result);
},
);
app.listen(3000);