HubClient SDK (Go)
The HubClient SDK is a Go client library for interacting with x402-hub. It provides type-safe access to protocol endpoints, authentication, and transaction history.
Installation
go get github.com/sigweihq/x402payQuick Start
import (
"fmt"
"github.com/sigweihq/x402pay/pkg/hubclient"
x402paytypes "github.com/sigweihq/x402pay/pkg/types"
)
func main() {
// Create client (uses https://hub.sigwei.com by default)
client := hubclient.NewHubClient(nil)
// Get authentication message
msg, err := client.Auth.GetAuthMessage("0xYourWalletAddress")
if err != nil {
panic(err)
}
// Sign message with wallet (user does this)
signature := signWithWallet(msg.Message)
// Login
auth, err := client.Auth.Login(msg.Message, signature)
if err != nil {
panic(err)
}
fmt.Println("Logged in:", auth.User.WalletAddress)
// Get transaction history
history, err := client.History.GetHistory(&x402paytypes.HistoryParams{
Network: "base",
Limit: 50,
})
for _, tx := range history.Transactions {
fmt.Printf("Transaction: %s (%s)\n", tx.Status, tx.Network)
}
}Architecture
HubClient is organized into three main components:
Hub-Specific Extensions
In addition to standard x402 protocol endpoints, HubClient provides:
SettleWithOptions() - Enhanced settle with
confirmanduseDbIdparametersTransfer() - Convenient endpoint for EVM transfers
Supported() - Query supported networks and schemes
Authentication
Basic Authentication Flow
Check Authentication Status
Get Current User
Token Refresh
Manual Token Management
Logout
Transaction History
Basic Query
Auto-Refresh
Use GetHistoryWithAutoRefresh() to automatically refresh expired tokens:
Pagination
x402 Protocol Operations
Check Supported Networks
Verify Payment
Settle Payment (Standard)
Settle Payment with Options
Transfer (Hub-Specific)
Configuration
Default Configuration
Custom Configuration
Error Handling
HTTP Errors
Common Errors
401 Unauthorized: Invalid credentials or expired token
Solution: Refresh token or re-authenticate
403 Forbidden: Insufficient permissions
400 Bad Request: Invalid parameters
500 Internal Server Error: Server error
Type Reference
Auth Types
History Types
Thread Safety
Auth token management is thread-safe (uses
sync.RWMutex)HTTP client is thread-safe (from
net/http)Multiple goroutines can safely share a single
HubClientinstance
Testing with HubClient
HubClient significantly simplifies E2E and integration tests by eliminating manual HTTP calls and JSON parsing.
Before: Manual HTTP Calls (~70 lines)
After: Using HubClient (~15 lines)
Benefits in Tests
63% less code - 70 lines → 26 lines for auth
Type safety -
map[string]interface{}→*TransactionHistoryItemAutomatic token management - No manual extraction or storage
Better error handling - Structured errors with context
Auto token refresh - Use
GetHistoryWithAutoRefresh()Single source of truth - All auth/history logic in hubclient
Complete Examples
See the HubClient README for comprehensive examples including:
Full authentication workflow
Transaction history queries
Error handling
Token refresh
Protocol endpoints
Best Practices
Reuse client instances - Create once, use throughout your application
Handle token refresh - Use
GetHistoryWithAutoRefresh()or implement refresh logicStore tokens securely - Persist tokens in secure storage between sessions
Validate parameters - Check limits and offsets before making requests
Handle errors properly - Check for
HTTPErrortype for detailed error infoUse in tests - Replace manual HTTP calls with hubclient for cleaner, type-safe tests
Related Documentation
Source Code
GitHub: sigweihq/x402pay Package: github.com/sigweihq/x402pay/pkg/hubclient
Last updated