> ## Documentation Index
> Fetch the complete documentation index at: https://docs.b3.fun/llms.txt
> Use this file to discover all available pages before exploring further.

# AnySpend facilitator

> Payment processor for x402 multi-token payments

<CardGroup cols={2}>
  <Card title="GitHub Repository" icon="github" href="https://github.com/b3-fun/anyspend-x402">
    View source code, examples, and contribute
  </Card>

  <Card title="npm Package" icon="npm" href="https://www.npmjs.com/package/@b3dotfun/anyspend-x402">
    Install @b3dotfun/anyspend-x402 package
  </Card>
</CardGroup>

## Overview

The AnySpend x402 Facilitator handles multi-token, cross-chain payments for the x402 protocol. It takes care of token swaps, cross-chain bridging, and settlement so developers can accept payments in any token while receiving their preferred currency.

**Production Endpoint:** `https://mainnet.anyspend.com/x402`

## Key capabilities

### Multi-token payment processing

Accept payments in any ERC-20 token that supports EIP-2612 (permit) or EIP-3009 (transferWithAuthorization):

* **Stablecoins**: USDC, USDT (on supported chains)
* **Native tokens**: ETH, SOL, BNB, MATIC
* **Custom tokens**: Any token with permit support, including project tokens like B3
* **19+ Networks**: Support across EVM chains (Base, Ethereum, Arbitrum, etc.) and SVM (Solana)

### Cross-chain routing

Automatically handles payments across different blockchain networks:

* **Same-chain payments**: Direct token transfers with minimal overhead
* **Cross-chain payments**: Automatic bridging between networks (e.g., ETH → Base)
* **Cross-VM payments**: Experimental support for Solana ↔ EVM transfers
* **Optimal routing**: Smart route selection for best execution

### Automatic token conversion

Converts between any supported tokens:

* **Buyer flexibility**: Users pay with whatever token they hold
* **Seller preference**: Merchants receive their chosen settlement currency
* **Best pricing**: Routes through optimal DEX liquidity sources
* **Slippage protection**: Automatic safeguards against price impact

### Gasless payments

Users never pay gas fees:

* Facilitator covers all on-chain gas costs
* Users only sign EIP-712 messages
* No need to hold native tokens for gas
* Smooth user experience across all chains

## Standards compliance

### x402 protocol compliant

The AnySpend Facilitator implements the complete x402 specification:

* HTTP-native payments via `402 Payment Required` status
* Standard headers (`X-PAYMENT`, `X-PAYMENT-RESPONSE`, `X-PREFERRED-TOKEN`)
* Exact payment scheme with recipient and amount verification
* Signature-based authorization (EIP-712, EIP-2612, EIP-3009)
* Cryptographic verification without trusted third parties

### Drop-in replacement

Fully compatible with any x402 implementation:

* **Client compatibility**: Works with any x402-compliant client
* **Server compatibility**: Can be used with any x402 middleware
* **No vendor lock-in**: Standard HTTP APIs, switch facilitators anytime
* **Interoperability**: Integrates with Coinbase CDP x402, PayAI, and others

## Architecture

### Payment flow

```
┌──────────────┐
│    Client    │  Signs payment authorization
│  (Any Token) │  preferredToken: USDT
└──────┬───────┘
       │
       │ X-PAYMENT header
       ▼
┌──────────────────────────┐
│   Resource Server        │
│   (Your API)             │
│                          │
│  AnySpend Middleware     │
│  • Intercepts request    │
│  • Gets quote            │
│  • Returns 402           │
└──────┬───────────────────┘
       │
       │ /verify, /settle
       ▼
┌──────────────────────────┐
│  AnySpend Facilitator    │
│  mainnet.anyspend.com    │
│                          │
│  • Validates signature   │
│  • Executes token swap   │
│  • Settles to seller     │
└──────┬───────────────────┘
       │
       │ On-chain txs
       ▼
┌──────────────────────────┐
│      Blockchain          │
│  • Receive payment       │
│  • Swap tokens (if needed) │
│  • Settle to merchant    │
└──────────────────────────┘
```

### Infrastructure components

**API Layer**

* RESTful HTTP API for payment verification and settlement
* WebSocket support for real-time payment status updates
* Rate limiting and DDoS protection
* 99.9% uptime SLA

**Swap Engine**

* Aggregates liquidity from multiple DEXs
* Optimal routing for best execution prices
* Slippage protection and MEV resistance
* Cross-chain bridging integration

**Settlement System**

* Parallel transaction processing
* Automatic nonce management
* Failed transaction recovery
* Real-time settlement confirmation

**Security**

* EIP-712 signature verification
* Replay attack prevention (nonce tracking)
* Deadline enforcement
* Smart contract audits

## API endpoints

### POST /verify

Validates payment signature without executing on-chain.

**Purpose:**

* Fast validation (under 100ms)
* Check signature authenticity
* Verify deadline hasn't expired
* No gas costs

**Request:**

```json theme={null}
{
  "x402Version": "0.2",
  "paymentHeader": "base64_encoded_payment",
  "paymentRequirements": {
    "scheme": "exact",
    "network": "base-mainnet",
    "amount": "1000000",
    "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
    "recipient": "0xMerchantAddress..."
  }
}
```

**Response:**

```json theme={null}
{
  "valid": true
}
```

***

### POST /settle

Executes on-chain settlement and returns transaction proof.

**Purpose:**

* Execute payment authorization
* Swap tokens if needed (buyer token → seller token)
* Settle to resource server
* Return transaction hash for verification

**Request:**

```json theme={null}
{
  "x402Version": "0.2",
  "paymentHeader": "base64_encoded_payment",
  "paymentRequirements": { /* ... */ }
}
```

**Response:**

```json theme={null}
{
  "txHash": "0xabc123...",
  "network": "base-mainnet",
  "metadata": {
    "receiptTxHash": "0x...",
    "swapTxHash": "0x...",
    "settlementTxHash": "0x..."
  }
}
```

***

### POST /quote

Get token conversion rate for multi-token payments.

**Purpose:**

* Calculate exact token amount for payment
* Get current exchange rate
* Estimate swap execution time

**Request:**

```json theme={null}
{
  "usdcAmount": "1000000",        // Target amount (1 USDC)
  "paymentToken": "0xfde4...",    // USDT address
  "network": "base-mainnet"
}
```

**Response:**

```json theme={null}
{
  "tokenAmount": "1000000",  // 1.0 USDT needed
  "exchangeRate": "1.0",
  "deadline": 1730000000,
  "route": {
    "srcToken": "0xfde4...",
    "dstToken": "0x8335...",
    "path": ["USDT", "USDC"]
  }
}
```

***

### GET /supported

Lists supported payment schemes and networks.

**Response:**

```json theme={null}
{
  "schemes": [{
    "scheme": "exact",
    "networks": [
      "base",
      "ethereum",
      "arbitrum",
      "optimism",
      "polygon",
      "bsc",
      "avalanche",
      "solana"
    ]
  }]
}
```

## Integration

### Server-Side (Express)

```typescript theme={null}
import { paymentMiddleware, facilitator } from '@b3dotfun/anyspend-x402';

const endpointConfig = {
  '/api/premium': {
    amount: '1.00',
    asset: 'USD'
  }
};

app.use(paymentMiddleware(
  '0xYourAddress...',
  endpointConfig,
  facilitator  // Pre-configured to mainnet.anyspend.com/x402
));
```

### Server-Side (Custom)

```typescript theme={null}
import { X402Facilitator } from '@b3dotfun/anyspend-x402';

const facilitator = new X402Facilitator({
  url: 'https://mainnet.anyspend.com/x402',
  apiKey: process.env.ANYSPEND_API_KEY  // Optional
});

// Verify payment
const { valid } = await facilitator.verify(paymentHeader, requirements);

// Settle payment
const { txHash } = await facilitator.settle(paymentHeader, requirements);
```

### Client-Side

```typescript theme={null}
import { X402Client } from 'anyspend-x402-client';

const client = new X402Client({
  walletClient,
  facilitatorUrl: 'https://mainnet.anyspend.com/x402'  // Optional, uses this by default
});

const response = await client.request('https://api.example.com/premium');
```

## Security

### Signature verification

All payments are verified using EIP-712 typed data signatures:

* **USDC**: EIP-3009 `transferWithAuthorization`
* **Other tokens**: EIP-2612 `permit`

Signatures are validated before any on-chain execution.

### Replay protection

**Random Nonce (USDC):**

* Each signature includes unique bytes32 nonce
* Nonce can only be used once per address
* No ordering dependencies

**Sequential Nonce (Permit):**

* Token contract maintains nonce counter
* Auto-increments on each permit execution
* Prevents out-of-order execution

### Deadline enforcement

All signatures include expiration timestamps:

* Default: 5 minutes from signature time
* Prevents stale payment authorizations
* Automatic rejection of expired signatures

### No private key access

The facilitator **never** has access to user private keys:

* Users sign messages client-side
* Only signatures are transmitted
* Non-custodial architecture

## Performance

### Speed

* **Verification**: Under 100ms for signature validation
* **Same-chain settlement**: 2-10 seconds average
* **Cross-chain settlement**: 2-5 minutes (depends on bridging)
* **Quote generation**: Under 200ms

### Scalability

* Handles 1000+ concurrent payments
* Horizontal scaling for high throughput
* Parallel transaction processing
* Load balancing across multiple nodes

### Reliability

* 99.9% uptime guarantee
* Automatic failover and redundancy
* Failed transaction retry logic
* 24/7 monitoring and alerts

## Network support

### EVM mainnet networks (12)

Abstract, Arbitrum, Avalanche, B3, Base, BNB Chain, Ethereum, IoTeX, Optimism, Peaq, Polygon, Sei

### EVM testnets (5)

Abstract Testnet, Avalanche Fuji, Base Sepolia, Polygon Amoy, Sei Testnet

### SVM networks (2)

Solana Mainnet, Solana Devnet

See [Network Support](/anyspend/x402-network-support) for complete details.

## Getting started

<CardGroup cols={2}>
  <Card title="Quickstart for Sellers" icon="money-bill" href="/anyspend/x402-quickstart-sellers">
    Start accepting multi-token payments in your API
  </Card>

  <Card title="Quickstart for Buyers" icon="wallet" href="/anyspend/x402-quickstart-buyers">
    Pay with any token using the AnySpend client
  </Card>

  <Card title="Network Support" icon="network-wired" href="/anyspend/x402-network-support">
    See all supported networks and tokens
  </Card>

  <Card title="Overview" icon="book" href="/anyspend/x402-overview">
    Learn how AnySpend x402 works
  </Card>
</CardGroup>

## Support

Need help with the facilitator?

* **Discord**: [discord.gg/b3dotfun](https://discord.gg/b3dotfun)
* **GitHub**: [github.com/b3-fun/anyspend-x402](https://github.com/b3-fun/anyspend-x402)
* **Email**: [support@b3.fun](mailto:support@b3.fun)
* **Docs**: [docs.b3.fun](https://docs.b3.fun)
