npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@x402-solana/client

v0.2.0

Published

Client SDK for automatic x402 payment handling on Solana

Readme

@x402-solana/client

Client SDK for automatic x402 payment handling on Solana.

npm version License: MIT x402

Overview

@x402-solana/client provides a drop-in replacement for native fetch() that automatically handles official x402 micropayments on Solana. When your code requests a paid API endpoint, this client:

🏆 x402 v1 Compliant: Automatically sends x402-compliant payment headers and handles 402 responses. See X402_COMPLIANCE.md for details.

  1. Detects 402 Payment Required responses
  2. Creates USDC payment on Solana automatically
  3. Waits for confirmation (400-800ms)
  4. Retries request with payment proof
  5. Returns data seamlessly

Perfect for AI agents, CLI tools, and automated systems that need to pay for API access.

Installation

npm install @x402-solana/client @solana/web3.js

Quick Start

import { X402Client } from '@x402-solana/client';

const client = new X402Client({
  solanaRpcUrl: 'https://api.devnet.solana.com',
  walletPrivateKey: process.env.WALLET_PRIVATE_KEY, // Base58 encoded
  network: 'devnet',
});

// Use exactly like fetch() - payments happen automatically!
const response = await client.fetch('https://api.example.com/premium-data');
const data = await response.json();

console.log('Got data:', data);
// Payment was handled automatically behind the scenes!

That's it! The client handles all payment complexity for you.

Features

  • Drop-in Fetch Replacement - Same API as native fetch()
  • Automatic Payments - Detects 402, pays, retries automatically
  • USDC on Solana - Fast, cheap micropayments
  • Error Handling - Clear error messages for payment issues
  • Retry Logic - Exponential backoff for transient failures
  • TypeScript - Fully typed with IntelliSense support
  • Debug Mode - Optional logging for troubleshooting
  • MCP Compatible - Works in Model Context Protocol servers

Usage Examples

Basic GET Request

const response = await client.fetch('https://api.example.com/data');
const json = await response.json();

POST with Body

const response = await client.fetch('https://api.example.com/analyze', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: 'bitcoin price' }),
});
const result = await response.json();

Error Handling

try {
  const response = await client.fetch('https://api.example.com/data');

  if (!response.ok) {
    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
  }

  const data = await response.json();
} catch (error) {
  if (error.code === 'INSUFFICIENT_BALANCE') {
    console.error('Not enough USDC in wallet!');
  } else if (error.code === 'PAYMENT_TIMEOUT') {
    console.error('Payment took too long to confirm');
  }
}

MCP Integration

This client works perfectly in Model Context Protocol (MCP) servers!

import { X402Client } from '@x402-solana/client';

const x402Client = new X402Client({
  solanaRpcUrl: process.env.SOLANA_RPC_URL!,
  walletPrivateKey: process.env.MCP_WALLET_PRIVATE_KEY!,
  network: 'mainnet-beta',
  debug: true, // Safe in v0.1.1+ (logs to stderr, not stdout)
});

// Use in MCP tool handlers
const response = await x402Client.fetch('https://api.example.com/premium');

Note: v0.1.1+ uses console.error() for debug logs (stderr), making it MCP-compatible.

Changelog

v0.1.1 (2025-01-02)

  • Fixed: Debug logs now use console.error() instead of console.log()
    • Makes client compatible with MCP servers
    • Debug mode can now be safely enabled in MCP environments

v0.1.0 (2025-01-02)

  • Initial release

Testing

Test Coverage: 31/31 tests passing (100%)

The test suite validates:

  • ✅ Client initialization (bs58 & Uint8Array wallet formats)
  • ✅ HTTP fetch operations (200 OK, 404 errors)
  • ✅ Balance queries (USDC and SOL)
  • ✅ USDC mint validation (devnet/mainnet)
  • ✅ Network error handling & retry logic
npm test

Production Validation: Real payment flow examples using Solana devnet are available in examples/ directory.

Related Packages

Documentation

License

MIT © 2025