Http

http/echo

github.com/x402-foundation/x402/go/v2/http/echo

import "github.com/x402-foundation/x402/go/v2/http/echo"

Functions

func NewEchoAdapter(ctx echo.Context) *EchoAdapter

Source: http/echo/middleware.go:36

NewEchoAdapter creates a new Echo adapter

func PaymentMiddleware(routes x402http.RoutesConfig, server *x402.X402ResourceServer, opts ...MiddlewareOption) echo.MiddlewareFunc

Source: http/echo/middleware.go:176

PaymentMiddleware creates Echo middleware for x402 payment handling using a pre-configured server.

func PaymentMiddlewareFromConfig(routes x402http.RoutesConfig, opts ...MiddlewareOption) echo.MiddlewareFunc

Source: http/echo/middleware.go:249

PaymentMiddlewareFromConfig creates Echo middleware for x402 payment handling. This creates the server internally from the provided options.

func PaymentMiddlewareFromHTTPServer(httpServer *x402http.HTTPServer, opts ...MiddlewareOption) echo.MiddlewareFunc

Source: http/echo/middleware.go:220

PaymentMiddlewareFromHTTPServer creates Echo middleware using a pre-configured HTTPServer. This allows registering hooks (e.g., OnProtectedRequest) on the server before attaching to the router.

Example:

resourceServer := x402.Newx402ResourceServer(
    x402.WithFacilitatorClient(facilitator),
).Register("eip155:*", evm.NewExactEvmScheme())

httpServer := x402http.Wrappedx402HTTPResourceServer(routes, resourceServer).
    OnProtectedRequest(requestHook)

e.Use(echomw.PaymentMiddlewareFromHTTPServer(httpServer))

func SetSettlementOverrides(c echo.Context, overrides *x402.SettlementOverrides)

Source: http/echo/middleware.go:22

SetSettlementOverrides sets settlement overrides on the Echo response for partial settlement. The middleware extracts these before settlement and strips the header from the client response.

func SimpleX402Payment(payTo string, price string, network x402.Network, facilitatorURL string) echo.MiddlewareFunc

Source: http/echo/builder.go:146

SimpleX402Payment creates middleware with minimal configuration. Uses a single route pattern and facilitator for the simplest possible setup.

Args:

payTo: Payment recipient address
price: Payment amount (e.g., "$0.001")
network: Payment network
facilitatorURL: Facilitator server URL

Returns:

Echo middleware function

Example:

e.Use(echomw.SimpleX402Payment(
    "0x123...",
    "$0.001",
    "eip155:8453",
    "https://facilitator.example.com",
))

func WithErrorHandler(handler func(echo.Context, error)) MiddlewareOption

Source: http/echo/middleware.go:151

WithErrorHandler sets a custom error handler

func WithFacilitatorClient(client x402.FacilitatorClient) MiddlewareOption

Source: http/echo/middleware.go:120

WithFacilitatorClient adds a facilitator client

func WithPaywallConfig(config *x402http.PaywallConfig) MiddlewareOption

Source: http/echo/middleware.go:137

WithPaywallConfig sets the paywall configuration

func WithScheme(network x402.Network, schemeServer x402.SchemeNetworkServer) MiddlewareOption

Source: http/echo/middleware.go:127

WithScheme registers a scheme server

func WithSettlementHandler(handler func(echo.Context, *x402.SettleResponse)) MiddlewareOption

Source: http/echo/middleware.go:158

WithSettlementHandler sets a custom settlement handler

func WithSyncFacilitatorOnStart(sync bool) MiddlewareOption

Source: http/echo/middleware.go:144

WithSyncFacilitatorOnStart sets whether to sync with facilitator on startup

func WithTimeout(timeout time.Duration) MiddlewareOption

Source: http/echo/middleware.go:165

WithTimeout sets the context timeout for payment operations

func X402Payment(config Config) echo.MiddlewareFunc

Source: http/echo/builder.go:67

X402Payment creates payment middleware using struct-based configuration. This is a cleaner, more readable alternative to PaymentMiddlewareFromConfig with variadic options.

Example:

e.Use(echomw.X402Payment(echomw.Config{
    Routes: routes,
    Facilitator: facilitatorClient,
    Schemes: []echomw.SchemeConfig{
        {Network: "eip155:*", Server: evm.NewExactEvmServer()},
        {Network: "solana:*", Server: svm.NewExactSvmServer()},
    },
    SyncFacilitatorOnStart: true,
    Timeout: 30 * time.Second,
}))

Types

type Config

Source: http/echo/builder.go:13

Config provides struct-based configuration for x402 payment middleware. This is a cleaner alternative to the variadic options pattern.

type Config struct {
	// Routes maps HTTP patterns to payment requirements
	Routes x402http.RoutesConfig

	// Facilitator is a single facilitator client (most common case)
	// Use this OR Facilitators (not both)
	Facilitator x402.FacilitatorClient

	// Facilitators is an array of facilitator clients (for fallback/redundancy)
	// Use this OR Facilitator (not both)
	Facilitators []x402.FacilitatorClient

	// Schemes to register with the server
	Schemes []SchemeConfig

	// PaywallConfig for browser-based payment UI (optional)
	PaywallConfig *x402http.PaywallConfig

	// SyncFacilitatorOnStart fetches supported kinds from facilitators on startup
	// Default: true
	SyncFacilitatorOnStart bool

	// Timeout for payment operations
	// Default: 30 seconds
	Timeout time.Duration

	// ErrorHandler for custom error handling (optional)
	ErrorHandler func(echo.Context, error)

	// SettlementHandler called after successful settlement (optional)
	SettlementHandler func(echo.Context, *x402.SettleResponse)
}
Fields
  • Routes x402http.RoutesConfig

    Routes maps HTTP patterns to payment requirements

  • Facilitator x402.FacilitatorClient

    Facilitator is a single facilitator client (most common case) Use this OR Facilitators (not both)

  • Facilitators []x402.FacilitatorClient

    Facilitators is an array of facilitator clients (for fallback/redundancy) Use this OR Facilitator (not both)

  • Schemes []SchemeConfig

    Schemes to register with the server

  • PaywallConfig *x402http.PaywallConfig

    PaywallConfig for browser-based payment UI (optional)

  • SyncFacilitatorOnStart bool

    SyncFacilitatorOnStart fetches supported kinds from facilitators on startup Default: true

  • Timeout time.Duration

    Timeout for payment operations Default: 30 seconds

  • ErrorHandler func(echo.Context, error)

    ErrorHandler for custom error handling (optional)

  • SettlementHandler func(echo.Context, *x402.SettleResponse)

    SettlementHandler called after successful settlement (optional)

type EchoAdapter

Source: http/echo/middleware.go:31

EchoAdapter implements HTTPAdapter for Echo framework

type EchoAdapter struct {
	// contains filtered or unexported fields
}

func GetAcceptHeader() string

Source: http/echo/middleware.go:70

GetAcceptHeader gets the Accept header

func GetHeader(name string) string

Source: http/echo/middleware.go:41

GetHeader gets a request header

func GetMethod() string

Source: http/echo/middleware.go:46

GetMethod gets the HTTP method

func GetPath() string

Source: http/echo/middleware.go:51

GetPath gets the request path

func GetURL() string

Source: http/echo/middleware.go:56

GetURL gets the full request URL

func GetUserAgent() string

Source: http/echo/middleware.go:75

GetUserAgent gets the User-Agent header

type MiddlewareConfig

Source: http/echo/middleware.go:84

MiddlewareConfig configures the payment middleware

type MiddlewareConfig struct {
	// Routes configuration
	Routes x402http.RoutesConfig

	// Facilitator client(s)
	FacilitatorClients []x402.FacilitatorClient

	// Scheme registrations
	Schemes []SchemeRegistration

	// Paywall configuration
	PaywallConfig *x402http.PaywallConfig

	// Sync with facilitator on start
	SyncFacilitatorOnStart bool

	// Custom error handler
	ErrorHandler func(echo.Context, error)

	// Custom settlement handler
	SettlementHandler func(echo.Context, *x402.SettleResponse)

	// Context timeout for payment operations
	Timeout time.Duration
}
Fields
  • Routes x402http.RoutesConfig

    Routes configuration

  • FacilitatorClients []x402.FacilitatorClient

    Facilitator client(s)

  • Schemes []SchemeRegistration

    Scheme registrations

  • PaywallConfig *x402http.PaywallConfig

    Paywall configuration

  • SyncFacilitatorOnStart bool

    Sync with facilitator on start

  • ErrorHandler func(echo.Context, error)

    Custom error handler

  • SettlementHandler func(echo.Context, *x402.SettleResponse)

    Custom settlement handler

  • Timeout time.Duration

    Context timeout for payment operations

type MiddlewareOption

Source: http/echo/middleware.go:117

MiddlewareOption configures the middleware

type MiddlewareOption func(*MiddlewareConfig)

type SchemeConfig

Source: http/echo/builder.go:47

SchemeConfig configures a payment scheme for a network.

type SchemeConfig struct {
	Network x402.Network
	Server  x402.SchemeNetworkServer
}
Fields
  • Network x402.Network
  • Server x402.SchemeNetworkServer

type SchemeRegistration

Source: http/echo/middleware.go:111

SchemeRegistration registers a scheme with the server

type SchemeRegistration struct {
	Network x402.Network
	Server  x402.SchemeNetworkServer
}
Fields
  • Network x402.Network
  • Server x402.SchemeNetworkServer