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-solana-sdk

v1.0.0

Published

The ultimate SDK for payment-gated APIs on Solana - Single package for all frameworks

Downloads

3

Readme

x402 Solana SDK

The ultimate SDK for payment-gated APIs using USDC on Solana - Single package for all frameworks.

Transform any API into a paid service in just 2 minutes with one simple npm install. Compatible with x402 protocol.

🚀 Quick Start

Installation

npm install x402-solana-sdk

That's it! No multiple packages, no complex setup. One package for everything.

Basic Usage

import { x402, createClient, createWallet } from 'x402-solana-sdk';

// Server-side (Express.js)
app.use('/premium', x402({
  amount: 0.01, // 0.01 USDC
  payToAddress: 'YOUR_WALLET_ADDRESS'
}));

// Client-side
const wallet = createWallet();
const client = createClient({ wallet });

const response = await client.payAndFetch('/premium', {
  amount: 0.01
});

⚡ Framework Support

Express.js

import express from 'express';
import { createX402Middleware } from 'x402-solana-sdk';

const app = express();
const x402 = createX402Middleware({
  amount: 0.01,
  merchantAddress: process.env.MERCHANT_ADDRESS
});

// Require payment
app.get('/premium', x402.requirePayment(0.01), (req, res) => {
  res.json({
    message: 'Premium content unlocked!',
    payment: req.x402 // Payment details
  });
});

// Optional payment
app.get('/freemium', x402.optionalPayment(0.005), (req, res) => {
  if (req.x402.verified) {
    res.json({ content: 'Premium content' });
  } else {
    res.json({ content: 'Free content' });
  }
});

Next.js (Coming Soon)

import { withX402 } from 'x402-solana-sdk/nextjs';

export default withX402(
  async (req, res) => {
    res.json({ message: 'Paid API response' });
  },
  {
    amount: 0.01,
    merchantAddress: process.env.MERCHANT_ADDRESS
  }
);

Fastify (Coming Soon)

import { x402Plugin } from 'x402-solana-sdk/fastify';

await fastify.register(x402Plugin, {
  amount: 0.01,
  merchantAddress: process.env.MERCHANT_ADDRESS
});

fastify.get('/premium', {
  x402: { amount: 0.01 }
}, async (request, reply) => {
  return { message: 'Premium content' };
});

Hono (Coming Soon)

import { Hono } from 'hono';
import { x402 } from 'x402-solana-sdk/hono';

const app = new Hono();

app.use('/premium/*', x402({
  amount: 0.01,
  merchantAddress: process.env.MERCHANT_ADDRESS
}));

app.get('/premium/data', (c) => {
  return c.json({ data: 'Premium data' });
});

🎯 Why x402 Solana SDK?

| Traditional Setup | x402 Solana SDK | |-------------------|-----------------| | Multiple packages | Single package | | Complex configuration | 2-line setup | | Framework-specific code | Universal API | | 30+ minutes setup | 2 minutes | | 200+ lines of code | 10-20 lines |

📦 What's Included

  • 🔒 Payment Verification - Ed25519 signatures, nonce protection, replay attack prevention
  • 🌐 Multi-Framework - Express.js, Next.js, Fastify, Hono support
  • 💎 TypeScript First - Full type safety and IntelliSense support
  • 🚀 Client Library - Browser and Node.js payment client
  • ⚡ Auto-Detection - Automatically detects your framework
  • 🛡️ Security - Built-in security best practices
  • 📱 Lightweight - Tree-shakeable, minimal bundle size

🔧 Configuration

Environment Variables

# Required
MERCHANT_ADDRESS=your_solana_wallet_address

# Optional
X402_FACILITATOR_URL=https://x402.org/facilitator
X402_NETWORK=mainnet-beta
X402_TIMEOUT=30000

Advanced Configuration

import { x402 } from 'x402-solana-sdk';

const middleware = x402({
  amount: 0.01,
  merchantAddress: process.env.MERCHANT_ADDRESS,
  facilitatorUrl: 'https://custom-facilitator.com',
  network: 'devnet',
  timeout: 60000,
  skipPaths: ['/health', '/metrics'],
  errorHandler: (error, req, res, next) => {
    console.error('Payment error:', error);
    res.status(500).json({ error: 'Payment processing failed' });
  }
});

🧪 Testing

import { createWallet, createClient } from 'x402-solana-sdk';

// Create test wallet
const wallet = createWallet();
console.log('Test wallet:', wallet.publicKey);

// Create test client
const client = createClient({
  wallet,
  facilitatorUrl: 'http://localhost:3001', // Your test facilitator
  network: 'devnet'
});

// Test payment
try {
  const response = await client.payAndFetch('http://localhost:3000/premium', {
    amount: 0.01
  });
  console.log('Success:', response);
} catch (error) {
  console.error('Payment failed:', error.message);
}

📚 API Reference

Core Functions

  • x402(options) - Universal middleware function
  • createClient(options) - Create payment client
  • createWallet() - Create new wallet
  • createPaymentGate(amount, address) - Quick setup

Types

interface PaymentOptions {
  amount: number;
  merchantAddress: string;
  facilitatorUrl?: string;
  network?: 'mainnet-beta' | 'devnet' | 'testnet';
  timeout?: number;
}

interface ClientOptions {
  wallet: WalletInterface;
  facilitatorUrl?: string;
  network?: 'mainnet-beta' | 'devnet' | 'testnet';
  timeout?: number;
}

🌟 Examples

Simple Premium API

import express from 'express';
import { x402 } from 'x402-solana-sdk';

const app = express();

// Free endpoint
app.get('/free', (req, res) => {
  res.json({ message: 'Free content' });
});

// Premium endpoint - 0.01 SOL
app.get('/premium', 
  x402({ amount: 0.01, merchantAddress: process.env.MERCHANT_ADDRESS }),
  (req, res) => {
    res.json({
      message: 'Premium content unlocked!',
      paidAmount: req.x402.amount,
      signature: req.x402.signature
    });
  }
);

app.listen(3000);

Client Usage

import { createClient, createWallet } from 'x402-solana-sdk';

const wallet = createWallet();
const client = createClient({ wallet });

// Pay and access premium content
const premiumData = await client.payAndFetch('http://localhost:3000/premium', {
  amount: 0.01
});

console.log('Premium data:', premiumData);

🔗 Links

📝 License

MIT License - see LICENSE file for details.


Ready to transform your APIs? Start with npm install x402-solana-sdk 🚀