@kaspacom/x402-kaspa
v0.1.0
Published
x402 Kaspa client SDK — automatic HTTP 402 payments on Kaspa L1
Readme
@kaspacom/x402-kaspa
Client SDK for the x402 Kaspa payment protocol. Automatically handles HTTP 402 payments on Kaspa L1.
Install
npm install @kaspacom/x402-kaspaQuick Start
import { X402Client } from "@kaspacom/x402-kaspa";
const client = new X402Client({
privateKeyHex: "<your-64-char-hex-key>",
rpcUrl: "ws://tn12-node.kaspa.com:17210",
network: "kaspa:testnet-12",
});
// Fetch a paid endpoint — payment happens automatically
const response = await client.fetch("https://api.example.com/weather");
const data = await response.json();How It Works
- Client calls a paid API endpoint
- Server returns
402 Payment Requiredwith payment details - SDK automatically deploys a payment channel (first time only)
- SDK signs a settle TX and retries the request with payment proof
- Facilitator (KaspaCom) co-signs and broadcasts on-chain
- Server returns the requested data
Configuration
const client = new X402Client({
privateKeyHex: "...", // Required: your Kaspa private key
rpcUrl: "ws://...", // Required: Kaspa wRPC node
network: "kaspa:testnet-12", // Required: CAIP-2 network
defaultFunding: 1_000_000_000n, // Optional: channel funding (default 10 KAS)
defaultTimeout: 86400, // Optional: channel timeout seconds (default 24h)
});Payment Channels
The SDK manages payment channels automatically:
- Opens a channel on first payment (deploys a 2-of-2 covenant)
- Reuses the channel for subsequent payments (incrementing nonce)
- Each payment is a settle TX that pays the merchant directly
