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-sdk-for-sui

v0.1.0

Published

X402 payment protocol SDK for Sui blockchain

Readme

X402 SDK for Sui

X402 payment protocol SDK for Sui blockchain. This SDK enables HTTP 402 Payment Required responses with automatic on-chain payment handling on Sui.

Features

  • 🚀 Easy Integration - Simple Express middleware for payment-required routes
  • 🔐 Secure - Cryptographic verification of payments on Sui blockchain
  • 🌐 Sui Native - Full support for SUI and custom Coin types
  • 🎯 Flexible - Support for both direct and facilitator-based settlement
  • 📦 TypeScript - Full type safety and IntelliSense support

Installation

npm install x402-sdk-for-sui
# or
pnpm add x402-sdk-for-sui

Quick Start

1. Server Setup (Resource Provider)

import express from "express";
import { paymentMiddleware } from "x402-sdk-for-sui/express";

const app = express();

// Configure payment middleware
app.use(
  paymentMiddleware(
    "0x...", // Your Sui address to receive payments
    {
      "/premium": {
        methods: ["GET"],
        price: "1000000000", // 1 SUI (9 decimals)
        description: "Premium content access"
      }
    },
    { url: "http://localhost:3002" }, // Facilitator URL
    undefined, // Paywall config (optional)
    {
      suiConfig: {
        network: "sui-localnet",
        rpcUrl: "http://127.0.0.1:9000"
      }
    }
  )
);

// Protected route
app.get("/premium", (req, res) => {
  res.json({ data: "Premium content" });
});

app.listen(4021);

2. Client Setup (Payment Sender)

import {
  wrapFetchWithPayment,
  createSigner
} from "x402-sdk-for-sui/fetch";

// Create signer from private key
const signer = await createSigner("sui-localnet", privateKey);

// Wrap fetch with automatic payment
const fetchWithPayment = wrapFetchWithPayment(fetch, signer, undefined, undefined, {
  suiConfig: {
    network: "sui-localnet",
    rpcUrl: "http://127.0.0.1:9000"
  }
});

// Make request - payment happens automatically on 402 response
const response = await fetchWithPayment("http://localhost:4021/premium");
const data = await response.json();

3. Facilitator Setup (Optional)

import { createFacilitatorServer } from "x402-sdk-for-sui";

// Start facilitator for payment verification and settlement
createFacilitatorServer(3002, "http://127.0.0.1:9000");

Project Structure

x402-sdk-for-sui/
├── src/
│   ├── types/              # TypeScript type definitions
│   ├── x402/
│   │   ├── schemes/
│   │   │   └── exact/
│   │   │       └── sui/
│   │   │           ├── client.ts    # Client payment logic
│   │   │           └── server.ts    # Server verification logic
│   │   ├── middleware/              # Express middleware
│   │   └── facilitator/             # Facilitator service
│   ├── x402-fetch/                  # Fetch wrapper
│   └── index.ts                     # Main entry point
├── examples/                         # Example implementations
├── scripts/
│   └── setup-localnet.ts            # Localnet setup script
└── README.md

Running Examples

1. Start Sui Localnet

sui start

2. Setup Local Environment

cd x402-sdk-for-sui
pnpm install
pnpm setup-localnet

This will:

  • Generate keypairs for facilitator, server, and client
  • Request SUI from local faucet
  • Output environment variables for .env files

3. Configure Environment

Copy the output from setup-localnet to create three .env files:

.env (Facilitator):

SUI_PRIVATE_KEY=0x...
SUI_NETWORK=sui-localnet
SUI_RPC_URL=http://127.0.0.1:9000
PORT=3002

.env_server (Resource Server):

FACILITATOR_URL=http://localhost:3002
NETWORK=sui-localnet
ADDRESS=0x...
TOKEN_COIN_TYPE=0x2::sui::SUI
TOKEN_DECIMALS=9
TOKEN_NAME=SUI
SUI_RPC_URL=http://127.0.0.1:9000
PORT=4021

.env_client (Client):

SUI_NETWORK=sui-localnet
SUI_RPC_URL=http://127.0.0.1:9000
USER_SUI_PRIVATE_KEY=0x...

4. Run Services

In three separate terminals:

# Terminal 1: Start Facilitator
pnpm run 402_facilitator

# Terminal 2: Start Resource Server
pnpm run 402_server

# Terminal 3: Run Client
pnpm run 402_client

API Reference

Client Functions

createSigner(network, privateKey)

Create a signer from private key.

const signer = await createSigner("sui-localnet", "0x...");

wrapFetchWithPayment(fetch, signer, maxValue?, selector?, config?)

Wrap fetch function with automatic payment handling.

const fetchWithPayment = wrapFetchWithPayment(
  fetch,
  signer,
  1000000000n, // Max 1 SUI
  undefined,
  { suiConfig: { network: "sui-localnet" } }
);

Server Functions

paymentMiddleware(payTo, routes, facilitator?, paywall?, config?)

Express middleware for payment-required routes.

app.use(
  paymentMiddleware(
    "0x...",
    { "/premium": { price: "1000000000" } },
    { url: "http://localhost:3002" }
  )
);

createFacilitatorServer(port, rpcUrl?)

Create facilitator server for verification and settlement.

createFacilitatorServer(3002, "http://127.0.0.1:9000");

Custom Coin Support

To use custom Coin types instead of native SUI:

{
  suiConfig: {
    network: "sui-localnet",
    rpcUrl: "http://127.0.0.1:9000"
  },
  tokenConfig: {
    coinType: "0x2::usdc::USDC", // Custom coin type
    decimals: 6,
    name: "USDC",
    symbol: "USDC"
  }
}

Network Support

  • sui-localnet - Local development
  • sui-devnet - Sui Devnet
  • sui-testnet - Sui Testnet
  • sui-mainnet - Sui Mainnet

Architecture

┌─────────┐           ┌────────────┐           ┌──────────────┐
│ Client  │──① 402───▶│   Server   │◀────②────▶│ Facilitator  │
└─────────┘           └────────────┘  verify   └──────────────┘
     │                       │                          │
     └───────③ Payment───────┘                          │
                             │                          │
                             └────④ Settlement─────────▶│
                                                        │
                                                   ┌────▼────┐
                                                   │   Sui   │
                                                   │Blockchain│
                                                   └─────────┘
  1. Client requests resource → Server returns 402 with payment requirements
  2. Server asks Facilitator to verify payment
  3. Client creates and signs payment transaction
  4. Facilitator settles payment on Sui blockchain

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

License

MIT License - see LICENSE for details.

Links

Support

For issues and questions:

  • GitHub Issues: https://github.com/zhiming817/x402-sdk-for-sui/issues
  • Discord: Join our community