Go reference implementation of the x402 Payment Protocol for HTTP 402 machine-to-machine micropayments
x402-go-demo is an early-stage Go project in the AI payments / x402 ecosystem. It currently has 0 GitHub stars and 0 forks.
A reference implementation of the x402 Payment Protocol in Go, demonstrating how HTTP 402 "Payment Required" enables machine-to-machine micropayments.
x402 is an open protocol that brings the HTTP 402 status code to life. It enables:
The protocol was developed by Coinbase to enable a new economy of paid APIs where machines transact directly with each other.
This is a complete working example showing the x402 payment flow:
┌──────────────────┐ ┌──────────────────┐
│ │ 1. GET /premium-data │ │
│ Client │ ───────────────────────────> │ Server │
│ (Payer) │ │ (Payee) │
│ │ 2. 402 Payment Required │ │
│ │ <─────────────────────────── │ │
│ │ + payment requirements │ │
│ │ │ │
│ Signs payment │ 3. GET + X-Payment header │ Verifies & │
│ authorization │ ───────────────────────────> │ settles via │
│ │ │ Facilitator │
│ │ 4. 200 OK + premium data │ │
│ │ <─────────────────────────── │ │
│ │ + X-Payment-Response │ │
└──────────────────┘ └──────────────────┘
Components included:
cmd/server/ - HTTP server with x402 middleware protecting endpointscmd/client/ - CLI client that handles 402 responses and makes paymentspkg/x402/ - Core types, middleware, and encoding utilitiespkg/wallet/ - Mock wallet for signing payment authorizationspkg/facilitator/ - Mock facilitator for verify/settle operationsNote: This demo uses mock implementations for wallet signing and payment settlement. In production, you would integrate with real blockchain wallets and the Coinbase x402 facilitator service.
go build ./...
Terminal 1 - Start the server:
go run ./cmd/server
You'll see:
===========================================
x402 Demo Server starting on http://localhost:8080
===========================================
Endpoints:
GET / - Service info (free)
GET /health - Health check (free)
GET /premium-data - Protected (requires payment)
Price: 10000 wei USDC on base-sepolia
Terminal 2 - Run the client:
go run ./cmd/client
The client will:
X-Payment header# Free endpoints
curl http://localhost:8080/
curl http://localhost:8080/health
# Protected endpoint (returns 402 without payment)
curl -i http://localhost:8080/premium-data
When a client requests a protected resource without payment:
{
"x402Version": 1,
"error": "Payment required to access this resource",
"accepts": [{
"scheme": "exact",
"network": "base-sepolia",
"maxAmountRequired": "10000",
"resource": "http://localhost:8080/premium-data",
"payTo": "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
"asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
"maxTimeoutSeconds": 60,
"mimeType": "application/json",
"description": "Access to premium data endpoint"
}]
}
The client sends payment as a base64-encoded JSON payload:
{
"x402Version": 1,
"scheme": "exact",
"network": "base-sepolia",
"payload": {
"signature": "0x...",
"authorization": {
"from": "0x742d35Cc...",
"to": "0x209693Bc...",
"value": "10000",
"validAfter": "1701648000",
"validBefore": "1701648300",
"nonce": "0x..."
}
}
}
On successful payment, the server returns settlement details:
{
"success": true,
"transaction": "0xabc123...",
"network": "base-sepolia",
"payer": "0x742d35Cc..."
}
| Setting | Value | Description |
|---|---|---|
| Server Port | :8080 |
HTTP server port |
| Price | 10000 |
0.01 USDC (6 decimals) |
| Network | base-sepolia |
Target blockchain |
| USDC Address | 0x036CbD53842c5426634e7929541eC2318f3dCF7e |
Base Sepolia USDC |
x402-go-demo/
├── cmd/
│ ├── server/main.go # HTTP server with protected endpoints
│ └── client/main.go # CLI client demonstrating payment flow
├── pkg/
│ ├── x402/
│ │ ├── types.go # Protocol types (PaymentPayload, etc.)
│ │ ├── constants.go # x402Version, header names
│ │ ├── encoding.go # Base64 encode/decode helpers
│ │ └── middleware.go # HTTP middleware for 402 enforcement
│ ├── wallet/
│ │ └── mock_wallet.go # Mock wallet for signing
│ └── facilitator/
│ ├── mock_server.go # Mock verify/settle implementation
│ └── client.go # HTTP client for facilitator API
├── go.mod
├── Makefile
└── README.md
To use this with real payments:
MIT
Your AI trading terminal assistant for US stocks, commodities, forex, and crypto.
Golang SDK for A2A Protocol
Publishes localhost services to the agentic web through self-hostable, trustless relays with x402 payments.
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Building blocks for Agentic payments (x402, MPP, AP2) for TypeScript, Rust, Go, Python, Ruby, PHP, Lua, Kotlin and Swift.
Go implementation of the x402 payment protocol