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

g402-buyer-sdk

v1.1.2

Published

X402 Buyer SDK with Privy wallet integration

Downloads

19

Readme

X402 Buyer SDK

The simplest way to make X402 payments in your applications. Just provide a buyerId and start making paid API calls.

Quickstart

import { createBuyer } from 'g402-buyer-sdk';

const buyer = createBuyer({ buyerId: 'your-buyer-id' });
const res = await buyer.fetch('https://api.example.com/premium-data');
console.log(await res.text());

The SDK handles:

  • Network Detection: Automatically detects EVM vs Solana from payment requirements
  • Wallet Resolution: Resolves the correct wallet for the required network
  • Payment Signing: Signs with the appropriate method (EIP-712 or Solana transactions)
  • Retry Logic: Automatically retries with payment headers

API Reference

createBuyer({ buyerId })

Creates a buyer instance that can make paid API calls.

Parameters:

  • buyerId (string): Your buyer ID from the X402 dashboard

Returns:

  • buyer.fetch(url, init?): Make paid API calls
  • buyer.getBuyerId(): Get the buyer ID
  • buyer.getLastResolvedAddress(): Get the last resolved wallet address

setX402Config(config) (Optional)

Override default configuration for testing or custom deployments.

import { setX402Config } from 'g402-buyer-sdk';

setX402Config({
  controlPlaneBaseUrl: 'http://localhost:3000',
  facilitatorUrl: 'https://staging-facilitator.example.com',
});

Examples

Basic Usage

import { createBuyer } from 'g402-buyer-sdk';

async function fetchPremiumData() {
  const buyer = createBuyer({ buyerId: process.env.BUYER_ID! });

  const response = await buyer.fetch('https://api.example.com/premium-data');

  if (response.ok) {
    const data = await response.json();
    console.log('Premium data:', data);
  }
}

With Custom Headers

const buyer = createBuyer({ buyerId: 'your-buyer-id' });

const response = await buyer.fetch('https://api.example.com/data', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ query: 'premium search' }),
});

Error Handling

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

  if (!response.ok) {
    console.error('API error:', response.status, await response.text());
  }
} catch (error) {
  console.error('Payment failed:', error.message);
}

How It Works

  1. Initial Request: SDK makes the request to your API
  2. 402 Response: If payment is required, the API returns a 402 status
  3. Network Detection: SDK detects whether EVM or Solana payment is required
  4. Wallet Resolution: SDK resolves the appropriate wallet for the detected network
  5. Payment Authorization: SDK gets payment authorization from the facilitator
  6. Transaction Signing: SDK signs the payment using your control plane
  7. Retry with Payment: SDK retries the request with payment headers
  8. Success: Your API returns the paid content

Supported Networks

  • EVM Networks: Ethereum, Base, Polygon, etc. (EIP-712 signing)
  • Solana: Solana mainnet and devnet (transaction signing)

The new API is much simpler:

// Old (complex)
const signer = await makePrivySignerWithBuyerId({
  buyerId,
  resolveBuyerWallet,
  signWithControlPlane,
});
const facilitator = new HttpFacilitator(facilitatorUrl);
const buyer = createBuyer({ signer, facilitator });

// New (simple)
const buyer = createBuyer({ buyerId });

License

MIT