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

@logiccrafterdz/xpay-sdk

v0.1.0

Published

A TypeScript SDK for automatic x402 payments on Base and other L2s

Downloads

6

Readme

XPay SDK

npm version CI License: MIT

A TypeScript SDK for handling X402 payment protocol on Base and other Layer 2 networks. Automatically handles payment flows when accessing paid APIs that return 402 Payment Required responses.

Installation

npm install @logiccrafterdz/xpay-sdk

Quick Start

import { createWalletClient, http } from 'viem';
import { baseSepolia } from 'viem/chains';
import { privateKeyToAccount } from 'viem/accounts';
import { createX402Client, createXPayWalletClient } from '@logiccrafterdz/xpay-sdk';

// Initialize wallet
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const viemWallet = createWalletClient({
  account,
  chain: baseSepolia,
  transport: http()
});

// Create XPay wallet wrapper
const xpayWallet = createXPayWalletClient({ 
  walletClient: viemWallet,
  chain: 'base-sepolia'
});

// Create X402 client
const client = createX402Client({
  walletClient: xpayWallet,
  chain: 'base-sepolia',
  maxRetries: 3,
  confirmationTimeout: 30000
});

// Make request to paid API - payment handled automatically
const response = await client.request({
  method: 'GET',
  url: 'https://api.example.com/premium-endpoint'
});

Supported Chains and Tokens

| Chain | Chain ID | Supported Tokens | |-------|----------|------------------| | Base Mainnet | 8453 | USDC | | Base Sepolia | 84532 | USDC |

Core Features

  • 🚀 TypeScript Support: Full type definitions and IntelliSense
  • 🔗 Multi-Chain: Support for Base mainnet and testnet
  • 💰 Automatic Payments: Handles X402 payment flow seamlessly
  • 🛡️ Error Handling: Comprehensive error types and retry logic
  • Viem Integration: Built on top of the modern viem library
  • 🧪 Well Tested: Comprehensive unit and integration test coverage

API Reference

createX402Client(config)

Creates an HTTP client that automatically handles X402 payment flows.

Parameters:

  • walletClient: XPayWalletClient instance
  • chain: Supported chain ('base' | 'base-sepolia')
  • maxRetries?: Maximum payment retry attempts (default: 1)
  • confirmationTimeout?: Transaction confirmation timeout in ms (default: 60000)

createXPayWalletClient(viemWallet)

Wraps a viem WalletClient with XPay-specific functionality.

Parameters:

  • viemWallet: A configured viem WalletClient instance

Payment Parser

import { parsePaymentInfo } from '@logiccrafterdz/xpay-sdk';

// Parse payment info from 402 response headers
const paymentInfo = parsePaymentInfo(response.headers);

Chain Configuration

import { getTokenAddress, getChainConfig } from '@logiccrafterdz/xpay-sdk';

// Get USDC token address for a specific chain
const usdcAddress = getTokenAddress('base-sepolia', 'USDC');

// Get viem chain configuration
const chainConfig = getChainConfig('base-sepolia');

Example

See examples/pay-for-api.ts for a complete working example.

To run the example:

# Set your private key (testnet only!)
export PRIVATE_KEY="0x..."

# Run the example
npm run example

Security Notes

⚠️ Important Security Guidelines:

  • Never hardcode private keys in production code
  • Use environment variables or secure key management systems
  • Only use testnet private keys for development and testing
  • Ensure your wallet has sufficient USDC balance and ETH for gas fees
  • Always validate payment amounts and recipients before confirming transactions

Development

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Run example
npm run example

Error Handling

The SDK provides specific error types for different failure scenarios:

import { InsufficientBalanceError, InsufficientAllowanceError } from '@logiccrafterdz/xpay-sdk';

try {
  const response = await client.request({ ... });
} catch (error) {
  if (error instanceof InsufficientBalanceError) {
    console.log('Not enough USDC balance');
  } else if (error instanceof InsufficientAllowanceError) {
    console.log('Need to approve USDC spending');
  }
}

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT

Support & Contact

Developer: LogicCrafterDZ Email: [email protected] Twitter: @Arana_lib Telegram: https://t.me/LogicCrafterDZ Farcaster : @esta