Skip to main content
Try it live! Experience X402 on Solana at x402-demo.anyspend.com or explore the source code on GitHub.

GitHub Repository

View source code and examples

npm Package

@b3dotfun/anyspend-x402

Live Demo

Full working Solana demo app

Overview

This guide shows you how to use AnySpend X402 on Solana for both server (accepting payments) and client (making payments) applications. AnySpend X402 on Solana enables gasless SPL token payments using transaction signatures, making it perfect for paywalled APIs, AI agents, and premium services. Key Features:
  • Gasless transactions - Facilitator sponsors all transaction fees
  • SPL token support - Pay with USDC and other SPL tokens on Solana
  • Browser wallet integration - Works with Phantom, Solflare, Ledger, and more
  • AI agent friendly - Simple fetch wrapper for autonomous payments

Prerequisites

  • Node.js 18+ installed
  • Solana wallet with some USDC (for testing)
  • Basic knowledge of Solana or Express.js (depending on your use case)

Server Setup

Accept Solana USDC payments with just a few lines of code.

Installation

Basic server example

What this does:
  1. Client requests the endpoint
  2. Middleware returns 402 Payment Required with Solana payment details
  3. Client signs a Solana transaction with USDC transfer
  4. Facilitator verifies and submits the transaction (gasless)
  5. Your route handler executes after payment confirmation

Client setup

There are two ways to make payments on Solana depending on your use case.

Option 1: using fetch (AI agents and backend)

Perfect for Node.js scripts, AI agents, and server-side applications.

Installation

Example script

Key Points:
  • Use base58-encoded private key (from Solana keypair)
  • Automatically handles 402 responses and payment flow
  • Returns transaction hash in x-payment-response header

Option 2: using browser wallets (React/Next.js)

For web applications using Phantom, Solflare, Ledger, or other Solana wallets.

Installation

React component example

App wrapper setup

Wrap your app with wallet providers:
Key Points:
  • Works with Phantom, Solflare, Ledger, Trust Wallet, and more
  • Prompts user to approve transaction signature
  • Fully gasless - users only sign, facilitator pays fees
  • Bridges Solana Wallet Adapter v1 with X402 v2 signatures

Configuration

Supported networks

  • solana - Solana mainnet-beta
  • solana-devnet - Solana devnet (for testing)

Token configuration

The most common SPL token on Solana is USDC:
For other SPL tokens, specify the mint address and decimals.

Facilitator URL

Production: https://mainnet.anyspend.com/x402 The facilitator handles:
  • Transaction verification
  • On-chain submission
  • Gas fee sponsorship
  • Payment settlement

Testing

Get test USDC

For Solana devnet testing:
  1. Get devnet SOL from Solana Faucet
  2. Use devnet USDC mint: 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU
  3. Set network to solana-devnet in your config

Test your integration


Common token addresses


Error handling


What’s next

X402 Overview

Learn how AnySpend X402 works

Network Support

See all supported chains and tokens

Signature Methods

Deep dive into Solana transaction signatures

Buyers Guide

EVM client setup guide

Troubleshooting

Check that:
  • Your wallet has enough USDC for the payment
  • You’re on the correct network (mainnet vs devnet)
  • The facilitator URL is correct
Make sure:
  • You’ve wrapped your app with WalletProvider
  • The wallet extension is installed (Phantom, Solflare, etc.)
  • You’re using the correct RPC endpoint
Verify:
  • The server endpoint is configured correctly
  • The payment amount matches server expectations
  • Check server logs for errors

Getting help