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/x402pay

Quick 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 confirm and useDbId parameters

  • Transfer() - 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 HubClient instance

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

  1. 63% less code - 70 lines → 26 lines for auth

  2. Type safety - map[string]interface{} → *TransactionHistoryItem

  3. Automatic token management - No manual extraction or storage

  4. Better error handling - Structured errors with context

  5. Auto token refresh - Use GetHistoryWithAutoRefresh()

  6. Single source of truth - All auth/history logic in hubclient

Complete Examples

See the HubClient READMEarrow-up-right for comprehensive examples including:

  • Full authentication workflow

  • Transaction history queries

  • Error handling

  • Token refresh

  • Protocol endpoints

Best Practices

  1. Reuse client instances - Create once, use throughout your application

  2. Handle token refresh - Use GetHistoryWithAutoRefresh() or implement refresh logic

  3. Store tokens securely - Persist tokens in secure storage between sessions

  4. Validate parameters - Check limits and offsets before making requests

  5. Handle errors properly - Check for HTTPError type for detailed error info

  6. Use in tests - Replace manual HTTP calls with hubclient for cleaner, type-safe tests

Source Code

GitHub: sigweihq/x402payarrow-up-right Package: github.com/sigweihq/x402pay/pkg/hubclient

Last updated