Go SDK
The T402 Go SDK provides comprehensive support for HTTP-native stablecoin payments.
Installation
go get github.com/t402-io/t402/sdks/go@latestCLI Tool
Install the CLI for quick operations:
go install github.com/t402-io/t402/sdks/go/cmd/t402@latestCLI 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 versionCLI Flags
-f, --facilitator URL Facilitator URL (default: https://facilitator.t402.io)
-o, --output FORMAT Output format: json, text (default: text)Features
- Multi-Chain: All 11 blockchain families (EVM, Solana, TON, TRON, NEAR, Aptos, Tezos, Polkadot, Stacks, Cosmos, Bitcoin)
- Server: HTTP middleware for payment protection (Gin, Echo, Chi, Fiber)
- Client: HTTP client with automatic 402 handling
- Facilitator: Full facilitator implementation
- CLI: Command-line tools for testing and debugging
- MCP Server: AI agent payment integration (6 tools)
- Smart Bridge Router: Cross-chain USDT0 bridging via LayerZero
Quick Start
Client
package main
import (
t402 "github.com/t402-io/t402/sdks/go"
"github.com/t402-io/t402/sdks/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/sdks/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
| Module | Description |
|---|---|
sdks/go | Core client, server, facilitator |
sdks/go/mechanisms/evm | EVM chains (Ethereum, Base, etc.) |
sdks/go/mechanisms/svm | Solana blockchain |
sdks/go/mechanisms/ton | TON blockchain |
sdks/go/mechanisms/tron | TRON blockchain |
sdks/go/mechanisms/near | NEAR Protocol |
sdks/go/mechanisms/aptos | Aptos blockchain |
sdks/go/mechanisms/tezos | Tezos blockchain |
sdks/go/mechanisms/polkadot | Polkadot Asset Hub |
sdks/go/mechanisms/stacks | Stacks (Bitcoin L2) |
sdks/go/mechanisms/cosmos | Cosmos/Noble |
sdks/go/mechanisms/btc | Bitcoin (PSBT) and Lightning (BOLT11) |
sdks/go/http | HTTP client and server |
sdks/go/http/gin | Gin middleware |
sdks/go/http/echo | Echo middleware |
sdks/go/http/chi | Chi middleware |
sdks/go/http/fiber | Fiber middleware |
sdks/go/signers/evm | EVM wallet signers |
sdks/go/signers/svm | Solana wallet signers |
sdks/go/signers/ton | TON wallet signers |
sdks/go/signers/tron | TRON wallet signers |
sdks/go/signers/near | NEAR wallet signers |
sdks/go/signers/aptos | Aptos wallet signers |
sdks/go/signers/tezos | Tezos wallet signers |
sdks/go/signers/polkadot | Polkadot wallet signers |
sdks/go/signers/stacks | Stacks wallet signers |
sdks/go/signers/cosmos | Cosmos wallet signers |
sdks/go/extensions/bazaar | API discovery extension |
sdks/go/mcp | MCP server library |
sdks/go/cmd/t402 | CLI tool |
sdks/go/cmd/t402-mcp | MCP server binary |
Module Structure
Multi-Chain Support
EVM Networks
import "github.com/t402-io/t402/sdks/go/mechanisms/evm"
// Get network config
config := evm.NetworkConfigs["eip155:8453"] // BaseTON Network
import "github.com/t402-io/t402/sdks/go/mechanisms/ton"
// Get network config
config := ton.NetworkConfigs[ton.TonMainnetCAIP2]TRON Network
import "github.com/t402-io/t402/sdks/go/mechanisms/tron"
// Get network config
config := tron.NetworkConfigs[tron.TronMainnetCAIP2]Solana
import "github.com/t402-io/t402/sdks/go/mechanisms/svm"
// Get network config
config := svm.NetworkConfigs[svm.SolanaMainnetCAIP2]NEAR Protocol
import "github.com/t402-io/t402/sdks/go/mechanisms/near"
// Get network config
config := near.NetworkConfigs[near.NearMainnetCAIP2]Aptos
import "github.com/t402-io/t402/sdks/go/mechanisms/aptos"
// Get network config
config := aptos.NetworkConfigs[aptos.AptosMainnetCAIP2]Tezos
import "github.com/t402-io/t402/sdks/go/mechanisms/tezos"
// Get network config
config := tezos.NetworkConfigs[tezos.TezosMainnetCAIP2]Polkadot Asset Hub
import "github.com/t402-io/t402/sdks/go/mechanisms/polkadot"
// Get network config
config := polkadot.NetworkConfigs[polkadot.PolkadotAssetHubCAIP2]Stacks (Bitcoin L2)
import "github.com/t402-io/t402/sdks/go/mechanisms/stacks"
// Get network config
config := stacks.NetworkConfigs[stacks.StacksMainnetCAIP2]Facilitator Client
import t402http "github.com/t402-io/t402/sdks/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
}Documentation
Requirements
- Go 1.24+