X402 PROTOCOL

X402 PROTOCOL

x402 Payment Protocol

HTTP 402 micropayments for agent-to-agent commerce


Overview

x402 enables autonomous agents to pay for services via the HTTP 402 “Payment Required” status code. This creates a native payment layer for the agent economy.


Protocol Flow

┌──────────┐    1. Request     ┌──────────┐
│  Agent A │ ───────────────→ │  Agent B │
│ (Client) │                   │ (Server) │
│          │ ←─────────────── │          │
│          │  2. HTTP 402     │          │
│          │  X-Payment-Request │        │
│          │                   │          │
│          │  3. Payment      │          │
│          │ ───────────────→ │          │
│          │  (Solana Tx)     │          │
│          │                   │          │
│          │  4. Retry + Receipt│        │
│          │ ───────────────→ │          │
│          │  X-Payment-Receipt│         │
│          │                   │          │
│          │ ←─────────────── │          │
│          │  5. Response     │          │
└──────────┘    (200 OK)      └──────────┘

Payment Request Header (X-Payment-Request)

{
  "version": "1.0",
  "network": "solana-devnet",
  "payTo": "BqPoJnqNLeQZCV5d9YY3Fo2LwFw17fRZbTTkEWGJJRUU",
  "amount": "10000000",
  "asset": null,
  "paymentId": "x402_intel_1707400000",
  "memo": "GRID Premium Swarm Intelligence"
}
FieldDescription
versionProtocol version (1.0)
networksolana-mainnet or solana-devnet
payToRecipient treasury wallet address
amountAmount in lamports (e.g., “10000000” = 0.01 SOL)
assetToken mint (null = Native SOL)
paymentIdUnique identifier for transaction correlation
memoHuman-readable service description

Payment Receipt Header

{
  "paymentId": "x402_abc123_xyz",
  "signature": "5K8Yh...",
  "payer": "DeF456...",
  "timestamp": 1707400050
}

Client Usage

import { X402Client } from '@grid/x402-solana';

const client = new X402Client(connection, wallet, {
  maxPayment: 0.1 * LAMPORTS_PER_SOL,
  dailyLimit: 1 * LAMPORTS_PER_SOL,
});

// Auto-pay and retry
const result = await client.payAndRetry('/api/service');

Server Usage

import { X402Server } from '@grid/x402-solana';

const server = new X402Server(connection, treasuryPubkey);

// Express middleware
app.use('/paid-api', server.middleware(10000)); // 10k lamports

Spending Limits

Client enforces limits:

  • Per-transaction maximum
  • Daily spending cap
  • Auto-pay threshold (require confirmation above)

Token-2022 Support

x402 supports multiple settlement options:

AssetMintUse Case
USDCEPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1vPrimary stable settlement
$GRID(TBD at TGE)Fee exemptions, governance
SOLNativeFallback

USDC Integration

USDC is the recommended settlement currency for agent commerce:

  • Stable value (no volatility risk)
  • Circle ecosystem compatibility
  • Native gas abstraction support
// USDC payment request
const request = {
  asset: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
  amount: "1000000", // 1 USDC (6 decimals)
  ...
};

Technical Appendix: Receipt Validation

Servers must verify the signature against the payTo address and amount on-chain.

Server-Side Validation (NodeJS)

import { Connection, PublicKey } from '@solana/web3.js';

async function verifyX402Payment(signature: string, expectedLamports: number) {
  const connection = new Connection('https://api.devnet.solana.com');
  const tx = await connection.getParsedTransaction(signature, 'confirmed');
  
  if (!tx) throw new Error("Transaction not found");
  
  const ix = tx.transaction.message.instructions.find(i => i.program === 'system');
  const { destination, lamports } = ix.parsed.info;
  
  if (destination !== TREASURY_ACCOUNT) throw new Error("Invalid recipient");
  if (lamports < expectedLamports) throw new Error("Insufficient payment");
  
  return true;
}

Last Updated: February 11, 2026