Go HTTP Client Example
Use x402 with Go's standard HTTP client
x402-http Example Client
Example client demonstrating how to use the x402 Go HTTP client to make requests to endpoints protected by the x402 payment protocol.
Prerequisites
- Go 1.24 or higher
- A running x402 server (see gin server example)
- Valid EVM and/or SVM private keys for making payments
Setup
- Install dependencies:
go mod download
- Copy
.env-exampleto.envand add your private keys:
cp .env-example .env
Required environment variables:
EVM_PRIVATE_KEY- Ethereum private key for EVM paymentsSVM_PRIVATE_KEY- Solana private key for SVM payments (optional)SERVER_URL- Server endpoint (defaults tohttp://localhost:4021/weather)
ā ļø Security Warning: Never use mainnet keys in .env files! Use testnet keys only.
- Run the client:
go run .
Available Examples
1. Builder Pattern (builder-pattern)
Configure the client by chaining .Register() calls to map network patterns to scheme clients.
go run . builder-pattern
2. Mechanism Helper Registration (mechanism-helper-registration)
Use mechanism helpers with wildcard network registration for clean, simple configuration.
go run . mechanism-helper-registration
Example Code
import (
x402 "github.com/coinbase/x402/go"
x402http "github.com/coinbase/x402/go/http"
evm "github.com/coinbase/x402/go/mechanisms/evm/exact/client"
evmsigners "github.com/coinbase/x402/go/signers/evm"
)
// Create signer
signer, err := evmsigners.NewClientSignerFromPrivateKey(os.Getenv("EVM_PRIVATE_KEY"))
// Configure client with builder pattern
client := x402.Newx402Client().
Register("eip155:*", evm.NewExactEvmScheme(signer))
// Wrap HTTP client with payment handling
httpClient := x402http.WrapHTTPClientWithPayment(
http.DefaultClient,
x402http.Newx402HTTPClient(client),
)
// Make request to paid endpoint (payment is handled automatically)
resp, err := httpClient.Get("http://localhost:4021/weather")
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
Example Output
Running example: builder-pattern
Making request to: http://localhost:4021/weather
ā
Response body:
{
"city": "San Francisco",
"weather": "foggy",
"temperature": 60,
"timestamp": "2024-01-01T00:00:00Z"
}
š° Payment Details:
Transaction: 0x1234567890abcdef...
Network: eip155:84532
Payer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Network Registration
Network identifiers use CAIP-2 format:
eip155:84532ā Base Sepoliaeip155:8453ā Base Mainnetsolana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1ā Solana Devnetsolana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpā Solana Mainnet
Wildcard Registration
Register all networks of a type with a single signer:
client.Register("eip155:*", evm.NewExactEvmScheme(evmSigner))
client.Register("solana:*", svm.NewExactSvmScheme(svmSigner))
Specific Network Registration
Register specific networks with different signers:
client.
Register("eip155:*", evm.NewExactEvmScheme(defaultSigner)). // Fallback for all EVM
Register("eip155:1", evm.NewExactEvmScheme(mainnetSigner)) // Override for Ethereum Mainnet
More specific registrations take precedence over wildcards.
Private Key Management
Creating Signers
Use the signer helpers for easy key management:
// EVM signer from hex private key (with or without 0x prefix)
evmSigner, err := evmsigners.NewClientSignerFromPrivateKey("0x1234...")
// SVM signer from base58 private key
svmSigner, err := svmsigners.NewClientSignerFromPrivateKey("5J7W...")
// From environment variable
evmSigner, err := evmsigners.NewClientSignerFromPrivateKey(os.Getenv("EVM_PRIVATE_KEY"))
Generating Test Keys
For testing, generate keys with these tools:
Ethereum:
# Using cast (foundry)
cast wallet new
Solana:
# Using solana CLI
solana-keygen new
Testing Against Local Server
- Start a local x402 server:
cd ../../servers/gin
go run main.go
- Run the client in another terminal:
cd ../../clients/http
go run . builder-pattern
Next Steps
See Advanced Examples for custom transport, retry logic and error handling.
Related Resources
- x402 Go Package Documentation
- Signer Helpers Documentation
- Server Examples ā build servers that can receive these payments
Related Content
Looking for more? Check out our other go examples or browse by client content.