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

polkadot-x402

v0.1.1

Published

X402 payment protocol implementation for Polkadot Hub TestNet (EVM-compatible). Provides facilitator functions, client utilities, and axios wrapper for HTTP 402 Payment Required responses.

Readme

polkadot-x402

X402 payment protocol implementation for Polkadot Hub TestNet (EVM-compatible). This SDK provides facilitator functions, client utilities, and an axios wrapper for handling HTTP 402 Payment Required responses with automatic payment processing.

Installation

npm install polkadot-x402

Features

  • Polkadot Hub TestNet Support - EVM-compatible chain with native PAS token
  • Automatic 402 Payment Handling - Axios interceptor for seamless payment processing
  • Payment Verification - EIP-712 signature validation
  • Payment Settlement - Native token transfers from buyer to seller
  • Full TypeScript Support - Complete type definitions included
  • Server-Side Signing - Secure payment authorization creation

Quick Start

Using the Axios Wrapper (Recommended)

import { createX402Axios, createSignerFromPrivateKey } from 'polkadot-x402';

// Create a signer from private key
const signer = createSignerFromPrivateKey('your-private-key', 'polkadot-hub-testnet');

// Create axios instance with x402 support
const axiosInstance = createX402Axios({
  baseURL: 'https://api.example.com',
  x402: {
    signer,
    network: 'polkadot-hub-testnet',
    onPaymentRequired: (requirements) => {
      console.log('Payment required:', requirements);
    },
    onPaymentSettled: (txHash) => {
      console.log('Payment settled:', txHash);
    },
  },
});

// Make requests - 402 responses are handled automatically
const response = await axiosInstance.get('/protected-resource');

Manual Payment Creation

import { createSignerFromPrivateKey, createPaymentHeader } from 'polkadot-x402';

const signer = createSignerFromPrivateKey('your-private-key', 'polkadot-hub-testnet');

const paymentRequirements = {
  x402Version: 1,
  scheme: 'exact',
  network: 'polkadot-hub-testnet',
  maxAmountRequired: '1000000000000000000', // 1 PAS (18 decimals)
  payTo: '0x9A78BC1B83242189C9d456067F56FFEEfba4376c',
  resource: '/api/protected-resource',
  asset: 'native',
};

const paymentResult = await createPaymentHeader(signer, {
  from: signer.address,
  to: paymentRequirements.payTo!,
  amount: paymentRequirements.maxAmountRequired!,
  requirements: paymentRequirements,
});

// Use paymentResult.paymentHeader in X-402-Payment header
const response = await fetch('https://api.example.com/protected-resource', {
  headers: {
    'X-402-Payment': paymentResult.paymentHeader,
  },
});

Facilitator Functions

Verify Payment

import { verifyX402Payment } from 'polkadot-x402';

const result = await verifyX402Payment(payload, requirements);

if (result.valid) {
  console.log('Payment verified:', result.details);
} else {
  console.error('Payment invalid:', result.error);
}

Settle Payment

import { settleX402Payment } from 'polkadot-x402';

const result = await settleX402Payment(
  payload,
  requirements,
  facilitatorPrivateKey
);

if (result.success) {
  console.log('Transaction hash:', result.transactionHash);
} else {
  console.error('Settlement failed:', result.error);
}

Network Configuration

Polkadot Hub TestNet

  • Network Name: Polkadot Hub TestNet
  • Currency Symbol: PAS
  • Chain ID: 420420422
  • RPC URL: https://testnet-passet-hub-eth-rpc.polkadot.io
  • Block Explorer: https://blockscout-passet-hub.parity-testnet.parity.io/
import { getNetworkConfig } from 'polkadot-x402';

const config = getNetworkConfig('polkadot-hub-testnet');
console.log(config.chainId); // 420420422

Payment Flow

  1. Client Request: Client makes request to protected resource
  2. 402 Response: Server responds with HTTP 402 and payment requirements
  3. Payment Authorization: Client creates EIP-712 signed payment authorization
  4. Retry with Payment: Client retries request with X-402-Payment header
  5. Verification: Server verifies payment signature and requirements
  6. Settlement: Server settles payment by transferring tokens on-chain
  7. Success: Protected resource is returned with transaction hash

API Reference

Types

import type {
  PaymentRequirements,
  VerificationResult,
  SettlementResult,
  PolkadotSigner,
} from 'polkadot-x402';

Network Configuration

import { getNetworkConfig, getSupportedNetworks } from 'polkadot-x402';

// Get network configuration
const config = getNetworkConfig('polkadot-hub-testnet');

// Get all supported networks
const networks = getSupportedNetworks(); // ['polkadot-hub-testnet']

Signer Creation

import { createSignerFromPrivateKey } from 'polkadot-x402';

// Create signer from private key (hex format)
const signer = createSignerFromPrivateKey('0x...', 'polkadot-hub-testnet');

Payment Header Creation

import { createPaymentHeader } from 'polkadot-x402';

const result = await createPaymentHeader(signer, {
  from: signer.address,
  to: 'recipient-address',
  amount: '1000000000000000000',
  requirements: paymentRequirements,
});

Supported Networks

Currently supports:

  • Polkadot Hub TestNet (polkadot-hub-testnet) - EVM-compatible testnet

Native Token Support

This SDK supports native PAS token transfers on Polkadot Hub TestNet. Set asset: 'native' in payment requirements to use native tokens instead of ERC-20 tokens.

TypeScript Support

Full TypeScript definitions are included. All types are exported from the main package:

import type {
  PaymentRequirements,
  VerificationResult,
  SettlementResult,
  PolkadotSigner,
  NetworkConfig,
  NetworkId,
} from 'polkadot-x402';

Differences from Coinbase x402 SDK

This SDK is specifically designed for Polkadot Hub TestNet and provides:

  1. Custom Network Support: Full support for Polkadot Hub TestNet without SDK limitations
  2. Native Token Transfers: Direct native token settlement without ERC-20 contracts
  3. EVM-Compatible: Built specifically for EVM-compatible Polkadot chains
  4. Custom EIP-712 Signing: Complete control over payment authorization format

License

MIT

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Support

For questions or issues, please open an issue on GitHub.