Go SDK

The T402 Go SDK provides comprehensive support for HTTP-native stablecoin payments.

Installation

go get github.com/t402-io/t402/go@latest

CLI Tool

Install the CLI for quick operations:

go install github.com/t402-io/t402/go/cmd/t402@latest

CLI Commands

# Verify a payment
t402 verify <base64-payload>
 
# Settle a payment
t402 settle <base64-payload>
 
# List supported networks
t402 supported
 
# Encode JSON to base64
t402 encode payment.json
 
# Decode base64 to JSON
t402 decode <base64-string>
 
# Show network info
t402 info eip155:8453
 
# Show version
t402 version

CLI Flags

-f, --facilitator URL    Facilitator URL (default: https://facilitator.t402.io)
-o, --output FORMAT      Output format: json, text (default: text)

Features

  • Multi-Chain: EVM, TON, TRON, Solana support
  • Server: Gin middleware for payment protection
  • Client: HTTP client with automatic 402 handling
  • Facilitator: Full facilitator implementation
  • CLI: Command-line tools for testing and debugging

Quick Start

Client

package main
 
import (
    t402 "github.com/t402-io/t402/go"
    "github.com/t402-io/t402/go/mechanisms/evm/exact/client"
)
 
func main() {
    // Create client
    c := t402.NewClient()
 
    // Register EVM scheme
    evmScheme := client.NewExactEvmScheme(privateKey)
    c.Register("eip155:8453", evmScheme)
 
    // Make request - 402 handled automatically
    resp, err := c.Fetch("https://api.example.com/premium")
}

Server

package main
 
import (
    "github.com/gin-gonic/gin"
    t402gin "github.com/t402-io/t402/go/http/gin"
)
 
func main() {
    r := gin.Default()
 
    r.Use(t402gin.PaymentMiddleware(t402gin.Config{
        Routes: map[string]t402gin.RouteConfig{
            "GET /api/premium": {
                Scheme:  "exact",
                Network: "eip155:8453",
                Price:   "$0.01",
                PayTo:   "0x...",
            },
        },
        FacilitatorURL: "https://facilitator.t402.io",
    }))
 
    r.GET("/api/premium", func(c *gin.Context) {
        c.JSON(200, gin.H{"data": "Premium content!"})
    })
 
    r.Run(":3000")
}

Modules

ModuleDescription
goCore client, server, facilitator
go/mechanisms/evmEVM chains (Ethereum, Base, etc.)
go/mechanisms/tonTON blockchain
go/mechanisms/tronTRON blockchain
go/mechanisms/svmSolana blockchain
go/httpHTTP client and server
go/http/ginGin middleware
go/signers/evmEVM wallet signers
go/signers/svmSolana wallet signers
go/extensions/bazaarAPI discovery extension
go/cmd/t402CLI tool

Multi-Chain Support

EVM Networks

import "github.com/t402-io/t402/go/mechanisms/evm"
 
// Get network config
config := evm.NetworkConfigs["eip155:8453"] // Base

TON Network

import "github.com/t402-io/t402/go/mechanisms/ton"
 
// Get network config
config := ton.NetworkConfigs[ton.TonMainnetCAIP2]

TRON Network

import "github.com/t402-io/t402/go/mechanisms/tron"
 
// Get network config
config := tron.NetworkConfigs[tron.TronMainnetCAIP2]

Solana

import "github.com/t402-io/t402/go/mechanisms/svm"
 
// Get network config
config := svm.NetworkConfigs[svm.SolanaMainnetCAIP2]

Facilitator Client

import t402http "github.com/t402-io/t402/go/http"
 
client := t402http.NewHTTPFacilitatorClient(&t402http.FacilitatorConfig{
    URL:     "https://facilitator.t402.io",
    Timeout: 30 * time.Second,
})
 
// Verify payment
result, err := client.Verify(ctx, payload, requirements)
 
// Settle payment
result, err := client.Settle(ctx, payload, requirements)
 
// Get supported networks
supported, err := client.GetSupported(ctx)

Core Interfaces

// Client interface
type X402Client interface {
    Register(network string, scheme SchemeNetworkClient)
    CreatePaymentPayload(requirements PaymentRequirements) (*PaymentPayload, error)
    Fetch(url string, opts ...RequestOption) (*http.Response, error)
}
 
// Server interface
type X402ResourceServer interface {
    Register(network string, scheme SchemeNetworkServer)
    Verify(payload *PaymentPayload, req PaymentRequirements) (*VerifyResponse, error)
    Settle(payload *PaymentPayload, req PaymentRequirements) (*SettleResponse, error)
}
 
// Facilitator interface
type X402Facilitator interface {
    Register(network string, scheme SchemeNetworkFacilitator)
    Verify(payload *PaymentPayload, req PaymentRequirements) (*VerifyResponse, error)
    Settle(payload *PaymentPayload, req PaymentRequirements) (*SettleResponse, error)
    GetSupported() *SupportedResponse
}

Requirements

  • Go 1.21+