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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nuwa-ai/facilitator

v0.1.0

Published

X402 Facilitator - A reference implementation for x402 payment facilitation

Readme

x402-exec Facilitator Example

This is an example implementation of an x402 facilitator service with SettlementRouter support for the x402-exec settlement framework. It demonstrates how to build a facilitator that supports both standard x402 payments and extended settlement flows with Hook-based business logic.

Features

🔄 Dual-Mode Settlement Support

  • Standard Mode: Direct ERC-3009 token transfers
  • SettlementRouter Mode: Extended settlement with Hook execution
    • Atomic payment verification + business logic
    • Built-in facilitator fee mechanism
    • Support for revenue splitting, NFT minting, reward distribution, etc.

🎯 Auto-Detection

The facilitator automatically detects the settlement mode based on the presence of extra.settlementRouter in PaymentRequirements. No manual configuration needed!

🌐 Multi-Network Support

  • EVM Networks: Base Sepolia (testnet), Base (mainnet)
  • Solana: Devnet support (standard mode only)

Quick Start

Prerequisites

Installation

From the project root:

cd examples/facilitator
pnpm install

Configuration

  1. Copy the example environment file:
cp .env.example .env
  1. Edit .env and add your private key:
# Required: Your facilitator wallet private key
EVM_PRIVATE_KEY=0xYourPrivateKeyHere

# Optional: Solana support
# SVM_PRIVATE_KEY=your_solana_private_key_base58
# SVM_RPC_URL=https://api.devnet.solana.com

# SettlementRouter addresses are pre-configured
SETTLEMENT_ROUTER_BASE_SEPOLIA=0x32431d4511e061f1133520461b07ec42aff157d6

# Server port (default: 3000)
PORT=3000

Running the Facilitator

pnpm dev

The server will start on http://localhost:3000

API Endpoints

GET /supported

Returns the payment kinds that the facilitator supports.

Response Example:

{
  "kinds": [
    {
      "x402Version": 1,
      "scheme": "exact",
      "network": "base-sepolia"
    }
  ]
}

POST /verify

Verifies an x402 payment payload without executing it.

Request Body:

{
  "paymentPayload": PaymentPayload,
  "paymentRequirements": PaymentRequirements
}

Response:

{
  "isValid": boolean,
  "invalidReason"?: string
}

POST /settle

Settles an x402 payment. Automatically detects and routes between standard and SettlementRouter modes.

Request Body:

{
  "paymentPayload": PaymentPayload,
  "paymentRequirements": PaymentRequirements
}

Response:

{
  "success": boolean,
  "transaction": string,    // Transaction hash
  "network": string,
  "payer": string,
  "errorReason"?: string
}

SettlementRouter Integration

What is SettlementRouter?

SettlementRouter is an extended settlement framework that enables:

  • Atomic Operations: Payment verification + business logic in one transaction
  • Hook Execution: Custom on-chain logic executed after payment
  • Facilitator Fees: Built-in fee mechanism for permissionless facilitators
  • Idempotency: Guaranteed once-only settlement

How It Works

The facilitator detects SettlementRouter mode by checking for extra.settlementRouter in the PaymentRequirements:

{
  "scheme": "exact",
  "network": "base-sepolia",
  "asset": "0x...",
  "maxAmountRequired": "1000000",
  "payTo": "0x...",
  "extra": {
    "settlementRouter": "0x32431d4511e061f1133520461b07ec42aff157d6",
    "salt": "0x1234...",
    "payTo": "0xabc...",
    "facilitatorFee": "10000",
    "hook": "0xdef...",
    "hookData": "0x..."
  }
}

When detected, the facilitator calls SettlementRouter.settleAndExecute() instead of the standard transferWithAuthorization().

Settlement Extra Parameters

| Field | Type | Description | |-------|------|-------------| | settlementRouter | address | SettlementRouter contract address | | salt | bytes32 | Unique identifier for idempotency (32 bytes hex) | | payTo | address | Final recipient address (for transparency) | | facilitatorFee | uint256 | Facilitator fee amount in token's smallest unit | | hook | address | Hook contract address (address(0) = no hook) | | hookData | bytes | Encoded hook parameters |

Example Flow

  1. Client receives 402 response with SettlementRouter parameters
  2. Client signs EIP-3009 authorization with commitment as nonce
  3. Client sends payment to facilitator
  4. Facilitator detects SettlementRouter mode (auto)
  5. Facilitator calls SettlementRouter.settleAndExecute()
  6. SettlementRouter verifies commitment and executes Hook
  7. Hook performs business logic (e.g., mint NFT, split revenue)

Supported Hooks

The facilitator works with any Hook that implements the ISettlementHook interface:

  • RevenueSplitHook: Multi-party payment distribution
  • NFTMintHook: Atomic NFT minting with payment
  • RewardHook: Loyalty points distribution
  • Custom Hooks: Any business logic you can imagine!

See contracts/examples/ for Hook implementations.

Testing

Test with curl

Standard payment:

curl -X POST http://localhost:3000/settle \
  -H "Content-Type: application/json" \
  -d '{
    "paymentPayload": {...},
    "paymentRequirements": {...}
  }'

SettlementRouter payment:

curl -X POST http://localhost:3000/settle \
  -H "Content-Type: application/json" \
  -d '{
    "paymentPayload": {...},
    "paymentRequirements": {
      "extra": {
        "settlementRouter": "0x32431d4511e061f1133520461b07ec42aff157d6",
        ...
      }
    }
  }'

Integration Testing

Use the settlement-showcase application for end-to-end testing with real Hook examples.

Architecture

┌─────────────────────────────────────────────────┐
│              Facilitator Server                 │
├─────────────────────────────────────────────────┤
│                                                 │
│  POST /settle                                   │
│       ↓                                         │
│  isSettlementMode()?                            │
│       ↓                ↓                        │
│     Yes              No                         │
│       ↓                ↓                        │
│  settleWithRouter  settle (x402 standard)       │
│       ↓                                         │
│  SettlementRouter.settleAndExecute()            │
│       ↓                                         │
│  Hook.execute()                                 │
│       ↓                                         │
│  Business Logic                                 │
│                                                 │
└─────────────────────────────────────────────────┘

Error Handling

The facilitator handles various error scenarios:

| Error | Cause | Response | |-------|-------|----------| | invalid_payment_requirements | Missing or invalid extra parameters | 400 with error details | | invalid_network | Unsupported network | 400 with error details | | invalid_transaction_state | Transaction reverted | Settlement response with error | | unexpected_settle_error | Unexpected error during settlement | Settlement response with error |

Production Deployment

For production use, consider:

  1. Use Production Facilitators:

    • Testnet: https://x402.org/facilitator
    • Production: https://api.cdp.coinbase.com/platform/v2/x402
  2. Security Considerations:

    • Secure private key storage (e.g., AWS KMS, HashiCorp Vault)
    • Rate limiting to prevent abuse
    • Request validation and sanitization
    • HTTPS/TLS for all connections
  3. Monitoring:

    • Track settlement success rates
    • Monitor gas usage
    • Alert on failed settlements
    • Log all transactions for reconciliation

Further Reading

Documentation

Integration Guides

If you're extending an existing facilitator in another language:

  • See the Facilitator Developer Guide for step-by-step integration instructions
  • This TypeScript implementation serves as a reference for any language

License

Apache-2.0 - see LICENSE for details