Protocol
x402
Type
Example
Stars
181
License
MIT
Last push
Mar 2, 2026

x402 Starter Kit x402 example

by dabit3• 181•
x402ExampleTypeScript

Starter kit for building paid APIs with x402 v2

About x402 Starter Kit

x402 Starter Kit is an open-source x402 example by dabit3, written mainly in TypeScript. Starter kit for building paid APIs with x402 v2. It has 181 GitHub stars and uses the MIT license, and its code was last pushed on Mar 2, 2026.

New to x402? Our x402 Protocol guide explains how it works and lists other projects built on it.

At a glance

Protocol
x402
Type
Example
Language
TypeScript
License
MIT
GitHub stars
181
Last push
Mar 2, 2026

Listed from public GitHub data, checked 2026-11-08. Not scanned by AgenticMart.

Questions about x402 Starter Kit

What is x402 Starter Kit?

Starter kit for building paid APIs with x402 v2 It is listed as a x402 example, maintained in the dabit3/x402-starter-kit repository on GitHub.

How do I install x402 Starter Kit?

The README in dabit3/x402-starter-kit has the setup steps. Clone the repository with git clone https://github.com/dabit3/x402-starter-kit.git and follow its instructions.

Is x402 Starter Kit an official x402 project?

No. It is a community project built on x402, not published by the organisation that runs the protocol.

What license does x402 Starter Kit use?

It is published under the MIT license. Check the license file in the repository before using it commercially.

Is x402 Starter Kit still maintained?

Its code was last pushed on Mar 2, 2026, and it had 181 GitHub stars when we checked on 2026-11-08. Look at recent issues and releases in the repository for a fuller picture.

README

x402 Starter Kit

x402 Starter Kit

A starter kit for building paid APIs using the x402 v2 payment protocol with support for both EVM and Solana networks.

Overview

This starter kit demonstrates how to build paid APIs using x402 v2. It:

  1. Receives API requests
  2. Requires payment (in this example of $0.10 USDC) before processing
  3. Verifies and settles payments through a facilitator (default: https://x402.org/facilitator for testnets, or your own local facilitator for mainnets)
  4. Processes requests (using OpenAI/EigenAI as configurable examples)
  5. Returns responses after payment is confirmed

Architecture

The API consists of four main components:

  • ExampleService: Example service logic that processes requests using OpenAI or EigenAI (replace with your own service implementation)
  • MerchantExecutor: Handles payment requirements, verification, and settlement (supports both EVM and Solana)
  • Server: Express HTTP server that orchestrates payment validation and request processing
  • Facilitator (optional): Facilitator server for mainnet support or custom networks

Prerequisites

  • Node.js 18 or higher
  • A wallet address to receive USDC payments
  • An OpenAI or EigenAI API key (for the example implementation - replace with your own API)
  • For testing: A wallet with USDC and gas tokens on your chosen network

Setup

1. Install Dependencies

npm install

2. Configure Environment Variables

Copy the example environment file:

cp env.example .env

Edit .env and fill in your values:

# =============================================================================
# Server Configuration
# =============================================================================
PORT=3000

# Your wallet address to receive payments (no private key needed!)
PAY_TO_ADDRESS=0xYourWalletAddress

# Network to use for payments
# Legacy names: base, base-sepolia, polygon, polygon-amoy, avalanche, avalanche-fuji,
#               iotex, sei, sei-testnet, peaq, solana, solana-devnet
# CAIP-2 format: eip155:8453, eip155:84532, solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
NETWORK=base-sepolia

# =============================================================================
# AI Provider Configuration
# =============================================================================
AI_PROVIDER=openai
OPENAI_API_KEY=sk-your-openai-api-key

# Alternative: EigenAI
# AI_PROVIDER=eigenai
# EIGENAI_API_KEY=your-eigenai-api-key

# =============================================================================
# Settlement Mode
# =============================================================================
# Options: "facilitator" (default) or "direct" (EVM only)
SETTLEMENT_MODE=facilitator

# For custom/local facilitator (required for mainnets):
# FACILITATOR_URL=http://localhost:4022

# For direct settlement (EVM only, no facilitator needed):
# SETTLEMENT_MODE=direct
# PRIVATE_KEY=0xYourMerchantPrivateKey

# =============================================================================
# Test Client Configuration
# =============================================================================
# EVM test client (for npm run test)
# EVM_CLIENT_PRIVATE_KEY=0xYourTestWalletPrivateKey

# Solana test client (for npm run test:solana)
# SOLANA_CLIENT_PRIVATE_KEY=YourBase58SolanaKeypair

# =============================================================================
# Local Facilitator Configuration (npm run start:facilitator)
# =============================================================================
# Required for mainnet support - the facilitator settles payments on-chain

# FACILITATOR_PORT=4022
# EVM_PRIVATE_KEY=0xFacilitatorEvmKey
# SVM_PRIVATE_KEY=FacilitatorSolanaKeypair
# FACILITATOR_EVM_NETWORK=base-sepolia
# FACILITATOR_SVM_NETWORK=solana-devnet

Quickstart

For Testnets (Simple)

Just run the server - it uses the default facilitator automatically:

npm run dev

For Mainnets (Requires Custom Facilitator)

The default facilitator at https://x402.org/facilitator only supports testnets. For mainnet, you need a facilitator that supports your network - this can be a local facilitator you run yourself, or an external hosted facilitator service.

Option 1: Run Your Own Facilitator

Terminal 1 - Start the Facilitator:

npm run start:facilitator

Terminal 2 - Start the Server:

FACILITATOR_URL=http://localhost:4022 npm run start

Option 2: Use an External Facilitator

If you have access to a hosted facilitator that supports mainnet:

FACILITATOR_URL=https://your-mainnet-facilitator.example.com npm run start

Note: Some facilitators do not require API keys to get started. For example, PayAI, x402rs, Heurist, Corbits, and other public facilitators can be used without additional authentication.

Some Available Facilitators (No API key required):

Option 3: Direct Settlement (EVM Only)

Skip the facilitator entirely and settle directly on-chain (requires merchant private key):

SETTLEMENT_MODE=direct PRIVATE_KEY=0xYourMerchantKey npm run start

Running Tests

# Test EVM payments
npm run test

# Test Solana payments
npm run test:solana

Available Scripts

ScriptDescription
npm run devBuild and start the server
npm run startStart the server (production)
npm run start:facilitatorStart the local facilitator
npm run buildBuild TypeScript
npm run testRun EVM test client
npm run test:solanaRun Solana test client
npm run setup:solanaSetup Solana wallets (create ATAs)
npm run cleanRemove build artifacts

Environment Variables Reference

Server (Merchant)

VariableRequiredDescription
PORTNoServer port (default: 3000)
PAY_TO_ADDRESSYesWallet address to receive payments
NETWORKNoNetwork for payments (default: base-sepolia)
SETTLEMENT_MODENofacilitator (default) or direct
FACILITATOR_URLNoCustom facilitator URL
PRIVATE_KEYFor direct modeMerchant key for direct settlement

AI Provider

VariableRequiredDescription
AI_PROVIDERNoopenai (default) or eigenai
OPENAI_API_KEYFor OpenAIOpenAI API key
EIGENAI_API_KEYFor EigenAIEigenAI API key
AI_MODELNoModel to use
AI_TEMPERATURENoTemperature setting
AI_MAX_TOKENSNoMax tokens

Test Clients

VariableDescription
EVM_CLIENT_PRIVATE_KEYEVM wallet for test payments
SOLANA_CLIENT_PRIVATE_KEYSolana wallet for test payments
AGENT_URLServer URL (default: http://localhost:3000)

Local Facilitator

VariableRequiredDescription
FACILITATOR_PORTNoFacilitator port (default: 4022)
EVM_PRIVATE_KEYYesEVM key for settling payments
SVM_PRIVATE_KEYYesSolana key for settling payments
FACILITATOR_EVM_NETWORKNoEVM network (default: base-sepolia)
FACILITATOR_SVM_NETWORKNoSolana network (default: solana-devnet)

Network Support

Testnets (Default Facilitator)

NetworkConfig ValueSupported
Base Sepoliabase-sepolia✅
Polygon Amoypolygon-amoy✅
Avalanche Fujiavalanche-fuji✅
Solana Devnetsolana-devnet✅

Mainnets (Requires Custom Facilitator or Direct Settlement)

NetworkConfig ValueCustom FacilitatorDirect Settlement
Basebase✅✅
Polygonpolygon✅✅
Avalancheavalanche✅✅
Solanasolana✅❌

Usage

Health Check

curl http://localhost:3000/health

Response:

{
  "status": "healthy",
  "service": "x402-payment-api",
  "version": "2.0.0",
  "x402Version": 2,
  "payment": {
    "address": "0xYourAddress...",
    "network": "base-sepolia",
    "price": "$0.10"
  }
}

Making a Request

curl -X POST http://localhost:3000/process \
  -H "Content-Type: application/json" \
  -d '{
    "message": {
      "parts": [{ "kind": "text", "text": "What is 2+2?" }]
    }
  }'

This returns payment requirements. To complete the flow, use the test clients or implement x402 payment signing.

How It Works

Payment Flow

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   CLIENT        │     │     SERVER      │     │   FACILITATOR   │
│                 │     │                 │     │                 │
│ 1. Request      │────▶│ 2. Return       │     │                 │
│                 │     │    payment      │     │                 │
│                 │◀────│    requirements │     │                 │
│                 │     │                 │     │                 │
│ 3. Sign payment │     │                 │     │                 │
│                 │     │                 │     │                 │
│ 4. Submit       │────▶│ 5. Verify       │────▶│ 6. Check sig    │
│    payment      │     │                 │◀────│                 │
│                 │     │                 │     │                 │
│                 │     │ 7. Process      │     │                 │
│                 │     │    request      │     │                 │
│                 │     │                 │     │                 │
│                 │     │ 8. Settle       │────▶│ 9. Submit tx    │
│                 │◀────│                 │◀────│    on-chain     │
│ 10. Response    │     │                 │     │                 │
└─────────────────┘     └─────────────────┘     └─────────────────┘

Key Roles

RoleWhat They NeedPurpose
Client (Payer)Private key + USDCSigns payment authorization
Server (Payee)Just an addressReceives USDC payments
FacilitatorPrivate key + gasSettles transactions on-chain

Project Structure

x402-starter/
├── src/
│   ├── server.ts              # Express server and endpoints
│   ├── ExampleService.ts      # Example AI service (replace with your own)
│   ├── MerchantExecutor.ts    # Payment verification & settlement
│   ├── facilitator.ts         # Local facilitator server
│   ├── testClient.ts          # EVM test client
│   ├── testClientSolana.ts    # Solana test client
│   ├── setupSolanaWallets.ts   # Solana wallet setup tool
│   └── x402Types.ts           # Shared types
├── env.example                # Example environment configuration
├── package.json
├── tsconfig.json
└── README.md

Troubleshooting

"PAY_TO_ADDRESS is required"

Set PAY_TO_ADDRESS in your .env file to your wallet address.

"OPENAI_API_KEY is required"

Set OPENAI_API_KEY in your .env file, or use AI_PROVIDER=eigenai with EIGENAI_API_KEY.

Payment verification fails

  • Check you're using the correct network
  • Verify the client wallet has USDC
  • For facilitator mode, ensure the facilitator is running and reachable
  • For direct mode, ensure PRIVATE_KEY has gas tokens

"Default facilitator only supports TESTNETS"

For mainnets, you need to:

  1. Run your own facilitator: npm run start:facilitator, or
  2. Use an external facilitator that supports mainnet, or
  3. Use direct settlement (EVM only): SETTLEMENT_MODE=direct

Solana payments not working

  • Ensure server is configured with NETWORK=solana-devnet or NETWORK=solana
  • For mainnet, use a facilitator with SVM_PRIVATE_KEY configured
  • Client needs SOLANA_CLIENT_PRIVATE_KEY with USDC + SOL for fees

Solana Wallet Setup

Solana requires Associated Token Accounts (ATAs) for each wallet to hold USDC. Unlike EVM, you can't just send tokens to any address - the receiving account must exist first.

Run the setup tool to check and create missing ATAs:

npm run setup:solana

This will:

  1. Check SOL balance for all configured wallets
  2. Check if USDC ATAs exist
  3. Automatically create missing ATAs (if a funded wallet is available)
  4. Show helpful instructions for any issues

Required wallets for Solana payments:

WalletNeeds ATA?Needs USDC?Needs SOL?
Client✅ Yes✅ Yes (to pay)✅ Minimal
Merchant✅ Yes❌ No✅ Minimal
Facilitator✅ Yes❌ No✅ Yes (for tx fees)

Getting devnet tokens:

Security Considerations

  • Never commit your .env file
  • Keep private keys secure
  • Use testnet for development
  • The merchant (server) only needs an address - no private key required to receive payments
  • Facilitator keys should be separate from merchant keys

Next Steps

  • Replace the example OpenAI service with your own API logic
  • Deploy the facilitator for mainnet payments
  • Add support for different payment tiers
  • Implement caching and rate limiting
  • Add monitoring and analytics

License

MIT

Resources

More x402 projects

All 20 →