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 🙏

© 2026 – Pkg Stats / Ryan Hefner

x402-client

v1.0.0

Published

x402 client library for browser and Node.js - Easy payment requests

Readme

@x402/client

Easy payment requests for x402 protocol on Solana. This package provides client-side utilities for making payment-gated requests to x402-enabled servers.

Installation

npm install @x402/client

Quick Start

import { X402Client, X402Wallet } from '@x402/client';

// Create or import wallet
const wallet = X402Wallet.fromPrivateKey(privateKey);
// or generate new wallet
const wallet = X402Wallet.create();

// Create client
const client = new X402Client({
  wallet,
  facilitatorUrl: 'http://localhost:3001'
});

// Pay and fetch resource
const response = await client.payAndFetch('http://localhost:3000/premium', {
  amount: 0.01 // 0.01 SOL
});

console.log(response.data); // Premium content

Features

  • 🔐 Wallet Management - Create, import, and manage Solana wallets
  • 💳 Payment Creation - Generate payment requests with nonce protection
  • ✍️ Transaction Signing - Sign Solana transactions and authorization payloads
  • 🌐 HTTP Integration - Make payment-gated HTTP requests seamlessly
  • 🛡️ Type Safety - Full TypeScript support with comprehensive types

API Reference

X402Wallet

Wallet management for x402 payments.

// Create new wallet
const wallet = X402Wallet.create();

// Import from private key
const wallet = X402Wallet.fromPrivateKey('base58-private-key');

// Import from mnemonic
const wallet = await X402Wallet.fromMnemonic('your twelve word mnemonic phrase');

// Get public key
const publicKey = wallet.getPublicKey();

// Sign message
const signature = await wallet.signMessage(messageBytes);

// Export wallet
const json = wallet.toJSON();

X402Client

Client for making payment-gated requests.

const client = new X402Client({
  wallet: myWallet,
  facilitatorUrl: 'http://localhost:3001',
  rpcUrl: 'https://api.devnet.solana.com', // optional
  defaultExpiry: 300 // optional, 5 minutes default
});

// Make payment request
const response = await client.payAndFetch(url, {
  amount: 0.01, // SOL amount
  expiry: 600, // optional, seconds
  resourceId: 'custom-id' // optional
});

// Manual payment creation
const paymentRequest = await client.createPaymentRequest(url, options, merchantAddress);

// Verify payment
const isValid = await client.verifyPayment(paymentRequest);

// Get wallet balance
const balance = await client.getBalance();

// Request airdrop (devnet only)
const signature = await client.requestAirdrop(1); // 1 SOL

Examples

Basic Payment

import { X402Client, X402Wallet } from '@x402/client';

const wallet = X402Wallet.fromPrivateKey(process.env.PRIVATE_KEY!);
const client = new X402Client({
  wallet,
  facilitatorUrl: process.env.FACILITATOR_URL!
});

try {
  const response = await client.payAndFetch('https://api.example.com/premium-data', {
    amount: 0.01
  });
  
  console.log('Premium data:', response.data);
  console.log('Transaction:', response.transactionSignature);
} catch (error) {
  console.error('Payment failed:', error);
}

Custom Payment Options

const response = await client.payAndFetch('https://api.example.com/resource', {
  amount: 0.005,
  expiry: 600, // 10 minutes
  resourceId: 'custom-resource-id'
});

Manual Payment Flow

// Create payment request
const paymentRequest = await client.createPaymentRequest(
  'https://api.example.com/resource',
  { amount: 0.01 },
  'merchant-solana-address'
);

// Verify with facilitator
const isValid = await client.verifyPayment(paymentRequest);

if (isValid) {
  // Make HTTP request with payment
  const response = await client.makePaymentRequest(
    'https://api.example.com/resource',
    { amount: 0.01 }
  );
}

Error Handling

try {
  const response = await client.payAndFetch(url, options);
} catch (error) {
  if (error.message.includes('insufficient SOL')) {
    console.log('Need more SOL in wallet');
  } else if (error.message.includes('402')) {
    console.log('Payment required but failed');
  } else {
    console.log('Other error:', error.message);
  }
}

Types

interface PaymentOptions {
  amount: number; // SOL amount
  expiry?: number; // seconds
  resourceId?: string; // custom identifier
}

interface PaymentResponse<T> {
  data: T; // response data
  verified: boolean; // payment verification status
  transactionSignature?: string; // Solana transaction hash
}

interface X402ClientConfig {
  wallet: X402Wallet;
  facilitatorUrl: string;
  rpcUrl?: string; // default: devnet
  defaultExpiry?: number; // default: 300 seconds
}

Browser Support

This package works in both Node.js and browser environments. For browsers, ensure you have proper polyfills for Node.js modules if needed.

License

MIT