Go SDK

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

Installation

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

Quick Start

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
    resp, err := c.Fetch("https://api.example.com/premium")
}

Modules

ModuleDescription
goCore client and server
go/mechanisms/evmEVM mechanism
go/mechanisms/tonTON mechanism
go/mechanisms/tronTRON mechanism
go/http/ginGin middleware

Gin Middleware

package main
 
import (
    "github.com/gin-gonic/gin"
    t402 "github.com/t402-io/t402/go"
    t402gin "github.com/t402-io/t402/go/http/gin"
)
 
func main() {
    r := gin.Default()
 
    r.Use(t402gin.PaymentMiddleware(t402gin.Config{
        Routes: map[string]t402.PaymentRequirements{
            "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")
}

Requirements

  • Go 1.21+