Skip to main content

GitHub Repository

View source code and examples

npm Package

@b3dotfun/anyspend-x402

Overview

This guide shows you how to add AnySpend x402 payment support to your Express API, enabling you to accept payments in any token while receiving the token of your choice. With just a few lines of code, your API can monetize endpoints and handle automatic payment verification and settlement. Key Benefits:
  • Buyers can pay with any token they hold (ETH, DAI, USDC, etc.)
  • Sellers can receive any token they prefer (USDC, B3, DAI, custom tokens, etc.)
  • AnySpend automatically handles all token conversions
  • No blockchain infrastructure or wallet management required

Prerequisites

Before you begin, make sure you have:
  • Node.js 18+ installed
  • An Express.js API (or willingness to create one)
  • A wallet address to receive USDC payments
  • Basic knowledge of Express middleware

Installation

Install the AnySpend x402 package:

Quick start

1. Basic integration

The simplest way to add payments to your Express API:
What this does:
  1. Clients request your API endpoint
  2. Middleware returns 402 Payment Required with payment details
  3. Client pays with their preferred token (ETH, DAI, etc.)
  4. AnySpend facilitator converts to USDC and settles to your wallet
  5. Your route handler executes and returns data

2. Receive custom tokens (e.g., B3)

You can configure your API to receive any token, not just USDC:
What’s different:
  • Buyers can still pay with any token (ETH, USDC, DAI, etc.)
  • AnySpend converts their payment to B3 tokens
  • You receive exactly 100 B3 tokens in your wallet
  • Both buyers and sellers keep their token preferences

Configuration

Facilitator URL

The AnySpend facilitator is hosted at:
The @b3dotfun/anyspend-x402 package includes a pre-configured facilitator instance:

Endpoint configuration

You have three methods to configure payment requirements: Let AnySpend handle token conversion automatically:
Benefits:
  • Clients can pay with any supported token
  • AnySpend converts to USDC automatically
  • You always receive USDC

Method 2: specific token requirements

Require specific tokens on specific networks. You can receive any token, not just USDC! Example 1: receive USDC (stablecoin)
Example 2: receive B3 tokens (custom token)
Example 3: receive DAI (stablecoin)
Example 4: receive native ETH
Example 5: receive custom ERC-20 token For any ERC-20 token with permit support:
Key Points:
  • Buyers can still pay with any token they hold
  • AnySpend automatically converts their token to the one you want to receive
  • You receive exactly the token you specified in your configuration
  • Both buyer and seller flexibility are maintained

Finding token details

To configure a custom token, you need to know its EIP-712 parameters. Here’s how to find them: Method 1: check token contract Use a blockchain explorer like Basescan:
Method 2: common tokens Method 3: use viem

Method 3: flexible (let client choose)

Allow clients to specify their preferred payment token:
Clients can then specify their token preference:

Advanced configuration

Dynamic pricing

Calculate prices based on request parameters:

Custom facilitator configuration

If you need custom facilitator settings:

Multiple recipients

Route payments to different wallets based on endpoint:

Protected routes

Protect specific routes

Only protect endpoints that require payment:

Route-specific middleware

Apply different payment configs to different route groups:

Testing

Local development

Test your API locally with testnet USDC:
Get testnet USDC from the Coinbase Faucet.

Test client request

Test with curl:
Or use the x402 client SDK (see Quickstart for Buyers).

Monitoring payments

Access payment metadata

The middleware attaches payment information to the request:

Webhook notifications

Get notified when payments are settled:

Examples

AI agent API

Data API with rate limiting

Best practices

Store sensitive data in environment variables:
Log all payment events for debugging and analytics:
Provide clear error messages when payments fail:
Always test with testnet before deploying to production:

What’s next

Overview

Understand how AnySpend x402 works

Network Support

See supported chains, tokens, and addresses

Troubleshooting

Check that:
  • Your facilitator URL is correct: https://mainnet.anyspend.com/x402
  • Your recipient address is valid
  • The network configuration matches the client’s network
Make sure your endpoint config allows flexible payments:
Verify:
  • Your recipient address is correct
  • You’re checking the correct network
  • Payment was settled (check req.x402Payment.txHash)

Getting help