n-payment

n-payment@0.29.1 on npm — collapses x402 + ERC-8004 + BTC lending + OWS signing into one call. Single peer dependency: viem. Dual CJS/ESM output. MIT-licensed.

Three principles

  1. Agents never touch a private key. OWS vault holds keys and enforces per-tx policy.
  2. BTC is the treasury; USDC is borrowed just-in-time. Lock PegBTC → borrow → pay → repay → unlock.
  3. Five lines, not five hundred. fetchWithPayment() unifies the lifecycle.

Consumer — fetchWithPayment()

// agent.ts
import { createPaymentClient } from 'n-payment';
 
const client = createPaymentClient({
  chains: ['goat-mainnet', 'base-mainnet'],
  ows: { wallet: 'my-agent' },
  policy: {
    maxPerTransaction: 100_000n,    // $0.10 cap per call
    maxPerDay: 10_000_000n,         // $10.00 daily cap
    treasury: 'BTC',                // PegBTC-collateralized
    rateLimit: { maxRequests: 100, windowMs: 60_000 },
  },
});
 
const res = await client.fetchWithPayment('https://hypermove.duckdns.org/api/paid-endpoint');
console.log(await res.json());      // 200 OK, paid in ~1.2s

Under the hood:

  1. GET the target URL → server returns HTTP 402 + WWW-Authenticate: x402-USDC.
  2. Vault checks policy (spend cap, chain allowlist, token allowlist).
  3. If treasury: 'BTC', the lending adapter locks PegBTC + borrows the exact USDC.
  4. EIP-3009 authorize signature is produced inside the OWS vault.
  5. The client retries with X-Payment header → server returns 200.

Provider — createPaywall()

// server.ts
import { createPaywall } from 'n-payment/middleware';
import { Hono } from 'hono';
 
const app = new Hono();
app.use('*', createPaywall({
  payTo: '0xYourAddress',
  chain: 'goat-mainnet',
  asset: 'USDC',
  routes: {
    '/api/paid-endpoint': { priceMicroUsdc: 10_000n },  // $0.01/call
  },
}));
app.get('/api/paid-endpoint', (c) => c.json({ ok: true }));
export default app;

One middleware call. Works with Express, Hono, Fastify, or raw fetch.

Paid MCP Server

// paid-mcp-server.ts
import { createPaidMcpServer, paidTool } from 'n-payment/agent';
 
const server = createPaidMcpServer({
  name: 'MyAgentService',
  payTo: '0xYourAddress',
  chain: 'goat-mainnet',
  tools: [
    paidTool({
      name: 'forecast',
      description: 'Get weather forecast',
      price: 10_000n,
      handler: async ({ city }) => ({ city, temp: 22 }),
    }),
  ],
});
await server.listen(3000);

14-protocol matrix

ProtocolCoverageChains
x402HTTP 402 handshakeGOAT, Base, Arb, Optimism
EIP-3009Authorized USDC transferBase, Arb, Polygon
MPPMulti-Party splitGOAT, Base
Stellar ChannelsSub-cent micropaymentsStellar
Wormhole NTTNative Token TransfersBase, Solana, Sui
Circle GatewayUSDC mint/burnBase, Solana
RLUSDRipple USD stablecoinXRPL, Base
XRPL x402XRPL-native MPT paywallXRPL
ERC-8004Agent identity + reputationBase, Celo
Aave V3 GHOIdle USDC treasury yieldBase, Arb
OWS VaultVault-bound signingall 27 chains
PegBTC LendingJIT USDC from BTCGOAT
Flare FXRPBridged XRP gas + assetFlare
Initia VIPValidator-incentive settleInitia

Links

Was this helpful?