# How the x402 Payment Protocol Enables USDC Micropayments for Agent-to-Agent Compute Calls Without API Keys

> Discover how the x402 protocol simplifies USDC micropayments for agent-to-agent compute calls. Learn how it replaces API keys with on-chain wallet proofs for secure, keyless transactions.

- Repository: [AL/CloddsBot](https://github.com/alsk1992/CloddsBot)
- Tags: how-to-guide
- Published: 2026-09-14

---

**The x402 protocol uses HTTP 402 Payment Required responses and EIP-712 typed-data signatures to let autonomous agents pay for compute with USDC on Base or Solana, replacing API keys with cryptographic wallet proofs that verify ownership on-chain.**

The CloddsBot repository implements this standard to facilitate agent-to-agent commerce, allowing AI systems to purchase LLM calls, code execution, and web-scraping services without traditional authentication tokens. By binding payment authorization directly to cryptographic signatures on EVM and Solana networks, the x402 protocol eliminates the need for centralized API key management while enabling atomic micropayments.

## Wallet Setup and Client Initialization

Each agent initializes an **x402 client** by storing a private key for either Base (EVM) or Solana in the configuration object. The system supports both chains through distinct wallet creation functions: `createEvmWallet` in [`src/payments/x402/evm.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/evm.ts) handles Base chain identities using EIP-712 typed-data standards, while `createSolanaWallet` in [`src/payments/x402/solana.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/solana.ts) manages Ed25519 keypairs for Solana.

When the gateway starts in [`src/gateway/index.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/gateway/index.ts) (lines 1169-1177), it instantiates the `X402Client` if `config.x402.enabled` is true. This client configuration includes the network selection, auto-approve thresholds for micropayments, and dry-run flags for testing. The client stores the wallet object generated from `config.x402.evmPrivateKey` or `config.x402.solanaPrivateKey`, preparing the agent to cryptographically sign payment requests.

## Signing Payment Proofs for HTTP Requests

Before calling premium endpoints like `/api/compute`, the client constructs a payment payload containing the USDC amount, destination treasury address, expiration timestamp, and cryptographic nonce. For EVM chains, the `signEvmPayment` function in [`src/payments/x402/evm.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/evm.ts) creates an EIP-712 compliant signature, while Solana uses `signSolanaPayment` in [`src/payments/x402/solana.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/solana.ts) for equivalent typed-message signing.

The `createFetchWithX402` function exported from [`src/payments/x402/index.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/index.ts) wraps an Axios instance to automatically intercept requests. When the server returns an HTTP 402 status, the interceptor triggers the signing flow and injects the resulting proof into the `X-Payment` header. This header contains the signed payload that serves as the sole authentication credential—no bearer tokens or API keys are transmitted.

```typescript
import { createX402Client, createFetchWithX402 } from './payments/x402/index.js';
import axios from 'axios';

const x402Client = createX402Client({
  network: 'base',
  evmPrivateKey: process.env.EVM_PK,
  autoApproveLimit: 0.01,  // Auto-approve payments ≤ $0.01 USDC
  dryRun: false,
});

const fetch = createFetchWithX402(axios, x402Client);
const resp = await fetch.post('https://compute.cloddsbot.com/api/compute', {
  model: 'claude',
  prompt: 'Analyze Q3 revenue trends',
});

```

## Server-Side Verification Without API Keys

The gateway mounts **x402 middleware** via `createX402Server` to protect premium routes. In [`src/gateway/server.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/gateway/server.ts) (lines 383-405), the middleware intercepts incoming requests, extracts the `X-Payment` header, and verifies the signature cryptographically rather than checking a database of API keys.

For EVM payments, `verifyEvmPayment` in [`src/payments/x402/evm.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/evm.ts) recomputes the typed-data hash and executes `ecrecover` to derive the signer's address from the signature. Solana uses `verifySolanaPayment` in [`src/payments/x402/solana.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/solana.ts) for equivalent Ed25519 signature verification. If the recovered address matches the claimed payer and the on-chain USDC balance is sufficient, the request proceeds—proving ownership through cryptography rather than shared secrets.

```typescript
import express from 'express';
import { createX402Server } from './payments/x402/index.js';
import { config } from './config';

const app = express();

const x402 = createX402Server({
  payToAddress: config.x402.server.payToAddress,
  network: config.x402.server.network || 'solana',
  facilitatorUrl: config.x402.facilitatorUrl,
});

app.use(['/api/compute', '/api/launch/token'], x402.middleware);

```

## On-Chain Settlement and Usage Tracking

Upon successful verification, the server optionally executes the **USDC transfer** to the treasury wallet. In [`src/payments/x402/evm.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/evm.ts), the `processEvmPayment` function submits the transaction to Base, while `processSolanaPayment` in [`src/payments/x402/solana.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/payments/x402/solana.ts) handles Solana transfers via `@solana/web3.js`. Dry-run mode simulates this flow without moving funds, enabling safe integration testing.

After settlement, the gateway updates the agent's quota and stores the transaction hash for audit trails. Agents can query their spending statistics through the `/api/x402/stats` endpoint defined in [`src/gateway/payments-routes.ts`](https://github.com/alsk1992/CloddsBot/blob/main/src/gateway/payments-routes.ts), which returns current balance, total spent, and remaining compute credits.

## Summary

- **Cryptographic authentication** replaces API keys: The `X-Payment` header containing a wallet signature proves ownership without centralized credential storage.
- **Dual-chain support**: The protocol supports both Base (EVM) via `createEvmWallet` and Solana via `createSolanaWallet` with identical payment flows.
- **Atomic verification**: Server-side functions `verifyEvmPayment` and `verifySolanaPayment` use `ecrecover` and Ed25519 verification to validate payments on-chain before processing compute requests.
- **Flexible settlement**: Production mode transfers USDC immediately, while dry-run mode allows testing without real currency movement.
- **Usage transparency**: The `/api/x402/stats` endpoint provides real-time accounting of micropayments and remaining compute quotas.

## Frequently Asked Questions

### How does x402 verify payments without requiring API keys?

The x402 protocol uses **EIP-712 typed-data signatures** on EVM chains and Ed25519 signatures on Solana to prove wallet ownership. When an agent sends a request, the `X-Payment` header contains a signed payload that the server verifies cryptographically using `ecrecover` (EVM) or native Solana verification. This binds the request identity directly to the on-chain wallet address, eliminating the need for pre-shared API keys or bearer tokens.

### Which blockchain networks does CloddsBot support for x402 micropayments?

According to the source code in `src/payments/x402/`, CloddsBot supports **Base** (an EVM-compatible layer-2) and **Solana**. The `createX402Client` function accepts a `network` parameter set to either `'base'` or `'solana'`, routing payments through [`evm.ts`](https://github.com/alsk1992/CloddsBot/blob/main/evm.ts) for EIP-712 signing or [`solana.ts`](https://github.com/alsk1992/CloddsBot/blob/main/solana.ts) for Ed25519 operations respectively.

### What prevents an agent from spending more USDC than it holds?

The server-side verification functions (`verifyEvmPayment` and `verifySolanaPayment`) check the on-chain USDC balance of the signing wallet before accepting the payment proof. If the balance is insufficient to cover the requested amount specified in the payment payload, the middleware rejects the request with a 402 status, preventing double-spending or overdraft scenarios.

### Can developers test x402 integrations without spending real USDC?

Yes. The `X402Client` accepts a `dryRun: true` configuration flag. When enabled, the client generates valid payment proofs and the server executes all verification logic, but `processEvmPayment` and `processSolanaPayment` skip the actual on-chain transfer. This allows full integration testing of the HTTP 402 flow, signature generation, and middleware verification without moving real funds.