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

@x402relay/sdk

v0.1.0

Published

TypeScript SDK for the x402 payment protocol — automatic HTTP 402 handling with EIP-3009 transferWithAuthorization

Readme

@x402relay/sdk

TypeScript SDK for the x402 payment protocol — automatic HTTP 402 handling with EIP-3009 transferWithAuthorization.

Features

  • Fetch-compatible HTTP client — drop-in replacement with .get() / .post() convenience methods
  • Automatic 402 handling — detects HTTP 402 responses, signs EIP-712 payment authorizations, and retries
  • Zero external dependencies — uses native fetch API (Node.js 18+)
  • x402 protocol v2 — full support for the x402 v2 payment flow
  • EIP-3009 helpers — build and sign transferWithAuthorization payloads
  • Event hooksonPaymentRequired, onPaymentComplete, onError
  • Configurable — retry count, timeout, custom payment selection strategy

Installation

npm install @x402relay/sdk

Quick Start

import { x402Client } from "@x402relay/sdk";

// Implement the WalletProvider interface
const walletProvider = {
  async getAddress() {
    return "0xYourWalletAddress";
  },
  async signTypedData(data) {
    // Use your preferred signing library (ethers, viem, etc.)
    return await signer.signTypedData(data.domain, data.types, data.message);
  },
};

// Create the client
const client = x402Client({ walletProvider });

// Make requests — 402 payments are handled automatically
const response = await client.get("https://api.example.com/translate?text=hello&target=ja");
const data = await response.json();

API

x402Client(config)

Creates an HTTP client with automatic x402 payment handling.

interface X402ClientConfig {
  walletProvider: WalletProvider;
  maxRetries?: number;        // default: 1
  timeout?: number;           // default: 30000 (ms)
  paymentSelector?: (accepts: PaymentRequirements[]) => PaymentRequirements;
  onPaymentRequired?: (response: PaymentRequiredResponse, url: string) => void;
  onPaymentComplete?: (payload: PaymentPayload, url: string) => void;
  onError?: (error: X402Error, url: string) => void;
}

Client Methods

// Fetch-compatible (any HTTP method)
client.fetch(url: string | URL, init?: RequestInit): Promise<Response>

// Convenience methods
client.get(url: string, init?: RequestInit): Promise<Response>
client.post(url: string, body?: object | BodyInit, init?: RequestInit): Promise<Response>

WalletProvider Interface

interface WalletProvider {
  getAddress(): Promise<string>;
  signTypedData(data: EIP712TypedData): Promise<string>;
}

EIP-3009 Helpers

import {
  buildEIP712Domain,
  buildAuthorization,
  buildTypedData,
  signTransferAuthorization,
} from "@x402relay/sdk";

// Build EIP-712 domain from payment requirements
const domain = buildEIP712Domain(requirements);

// Build authorization with timestamps and nonce
const authorization = buildAuthorization(fromAddress, requirements);

// Build complete typed data for signing
const typedData = buildTypedData(domain, authorization);

// Or do it all at once
const { authorization, signature } = await signTransferAuthorization(
  walletProvider,
  requirements,
);

Utilities

import {
  generateNonce,
  encodePaymentHeader,
  decodePaymentHeader,
} from "@x402relay/sdk";

const nonce = generateNonce();                    // 0x-prefixed 32-byte hex
const encoded = encodePaymentHeader(payload);     // base64 for X-PAYMENT header
const decoded = decodePaymentHeader(encoded);     // parse back

Payment Flow

  1. Client makes an HTTP request
  2. Server responds with 402 Payment Required containing payment requirements
  3. SDK automatically:
    • Parses the 402 response body (x402 v2 format)
    • Selects a payment option from accepts[]
    • Builds an EIP-3009 transferWithAuthorization payload
    • Signs it with EIP-712 via the wallet provider
    • Retries the request with the X-PAYMENT header

Error Handling

All SDK errors are instances of X402Error with a code property:

| Code | Description | |------|-------------| | PAYMENT_FAILED | Payment was not accepted by the server | | SIGNING_FAILED | Wallet provider failed to sign | | INVALID_402_RESPONSE | 402 body is not valid x402 v2 | | NO_ACCEPTABLE_PAYMENT | No payment option matched selector | | TIMEOUT | Request timed out | | ABORTED | Request was aborted by the caller | | MAX_RETRIES_EXCEEDED | All retry attempts failed |

import { X402Error } from "@x402relay/sdk";

try {
  await client.get("https://api.example.com/paid-endpoint");
} catch (err) {
  if (err instanceof X402Error) {
    console.error(`x402 error [${err.code}]: ${err.message}`);
  }
}

License

MIT