Starter template for AI agents that pay for x402-gated resources using Routex multi-chain routing.
What this does: Your agent calls an API. The API returns 402 Payment Required. Routex evaluates all configured blockchains, picks the cheapest, signs the payment, and your agent gets the resource. No manual chain selection. No overpaying.
# 1. Clone and install
git clone https://github.com/routexcc/x402-agent-starter.git
cd x402-agent-starter
pnpm install
# 2. Configure
cp .env.example .env
# Edit .env — add your agent wallet private key and receiving address
# 3. Run the test server (terminal 1)
pnpm server
# 4. Run the agent (terminal 2)
pnpm agentAgent Server Routex
│ │ │
│──── GET /weather ───────────>│ │
│<─── 402 + payment options ───│ │
│ │ │
│──── payment requirements ──────────────────────────────>│
│ │ evaluate Base, Stellar, │
│ │ Solana, Polygon fees │
│<─── signed payload (cheapest chain) ────────────────────│
│ │ │
│──── GET /weather + payment ─>│ │
│<─── 200 + weather data ──────│ │
src/
├── agent.ts # AI agent that requests x402 resources via Routex
├── server.ts # Local x402-gated test server (returns 402)
├── config.ts # Environment validation — fails fast on bad config
└── signer.ts # Non-custodial viem wallet signer for Routex
The starter uses Base Sepolia. To route across multiple chains, install additional adapters:
pnpm add @routexcc/chain-stellar @routexcc/chain-solana @routexcc/chain-polygonThen add them to the adapters map in src/agent.ts:
import { createStellarAdapter } from '@routexcc/chain-stellar';
import { createSolanaAdapter } from '@routexcc/chain-solana';
const adapters = new Map([
['base', createBaseAdapter(viemClient, { testnet: true })],
['stellar', createStellarAdapter(stellarKeypair)],
['solana', createSolanaAdapter(solanaConnection)],
]);Routex will automatically compare fees across all configured chains and pick the cheapest for each payment.
Change the strategy in src/agent.ts:
| Strategy | Picks | Use When |
|---|---|---|
'cheapest' |
Lowest fee in USD | Cost-sensitive, high volume (default) |
'fastest' |
Lowest finality time | Real-time agents, time-critical |
'balanced' |
60% cost / 40% speed | General purpose |
| Custom scorer | Your logic | Domain-specific requirements |
- Non-custodial: Routex never accesses your private key directly. It calls
signer.sign()andsigner.signTypedData()only. - Environment isolation: Private keys live in
.envonly, validated at startup, never logged. - Dedicated wallet: Use a separate wallet for your agent. Fund it with only what it needs.
- BigInt arithmetic: All token amounts use
bigint. No floating-point. - Typed errors: Failed routes throw
RouteExhaustedErrorwith per-chain rejection reasons — never silent failures.
Replace X402_RESOURCE_URL in .env with any x402-enabled endpoint:
X402_RESOURCE_URL=https://some-x402-api.com/dataThe agent will automatically handle the 402 → payment → resource flow.
- Node.js >= 20
- pnpm (or npm/yarn)
- A funded testnet wallet with Base Sepolia USDC
- Base Sepolia RPC endpoint (free public RPC included by default)
Get Base Sepolia testnet USDC:
- Get Sepolia ETH from a faucet (e.g., Alchemy Sepolia Faucet)
- Bridge or mint USDC on Base Sepolia using the USDC faucet