Documentation
¶
Index ¶
- func PaymentMiddleware(routes x402http.RoutesConfig, server *x402.X402ResourceServer, ...) echo.MiddlewareFunc
- func PaymentMiddlewareFromConfig(routes x402http.RoutesConfig, opts ...MiddlewareOption) echo.MiddlewareFunc
- func PaymentMiddlewareFromHTTPServer(httpServer *x402http.HTTPServer, opts ...MiddlewareOption) echo.MiddlewareFunc
- func SetSettlementOverrides(c echo.Context, overrides *x402.SettlementOverrides)
- func SimpleX402Payment(payTo string, price string, network x402.Network, facilitatorURL string) echo.MiddlewareFunc
- func X402Payment(config Config) echo.MiddlewareFunc
- type Config
- type EchoAdapter
- type MiddlewareConfig
- type MiddlewareOption
- func WithErrorHandler(handler func(echo.Context, error)) MiddlewareOption
- func WithFacilitatorClient(client x402.FacilitatorClient) MiddlewareOption
- func WithPaywallConfig(config *x402http.PaywallConfig) MiddlewareOption
- func WithScheme(network x402.Network, schemeServer x402.SchemeNetworkServer) MiddlewareOption
- func WithSettlementHandler(handler func(echo.Context, *x402.SettleResponse)) MiddlewareOption
- func WithSyncFacilitatorOnStart(sync bool) MiddlewareOption
- func WithTimeout(timeout time.Duration) MiddlewareOption
- type SchemeConfig
- type SchemeRegistration
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PaymentMiddleware ¶
func PaymentMiddleware(routes x402http.RoutesConfig, server *x402.X402ResourceServer, opts ...MiddlewareOption) echo.MiddlewareFunc
PaymentMiddleware creates Echo middleware for x402 payment handling using a pre-configured server.
func PaymentMiddlewareFromConfig ¶
func PaymentMiddlewareFromConfig(routes x402http.RoutesConfig, opts ...MiddlewareOption) echo.MiddlewareFunc
PaymentMiddlewareFromConfig creates Echo middleware for x402 payment handling. This creates the server internally from the provided options.
func PaymentMiddlewareFromHTTPServer ¶
func PaymentMiddlewareFromHTTPServer(httpServer *x402http.HTTPServer, opts ...MiddlewareOption) echo.MiddlewareFunc
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 ¶
func SetSettlementOverrides(c echo.Context, overrides *x402.SettlementOverrides)
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 ¶
func SimpleX402Payment(payTo string, price string, network x402.Network, facilitatorURL string) echo.MiddlewareFunc
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 X402Payment ¶
func X402Payment(config Config) echo.MiddlewareFunc
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 ¶
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)
}
Config provides struct-based configuration for x402 payment middleware. This is a cleaner alternative to the variadic options pattern.
type EchoAdapter ¶
type EchoAdapter struct {
// contains filtered or unexported fields
}
EchoAdapter implements HTTPAdapter for Echo framework
func NewEchoAdapter ¶
func NewEchoAdapter(ctx echo.Context) *EchoAdapter
NewEchoAdapter creates a new Echo adapter
func (*EchoAdapter) GetAcceptHeader ¶
func (a *EchoAdapter) GetAcceptHeader() string
GetAcceptHeader gets the Accept header
func (*EchoAdapter) GetHeader ¶
func (a *EchoAdapter) GetHeader(name string) string
GetHeader gets a request header
func (*EchoAdapter) GetMethod ¶
func (a *EchoAdapter) GetMethod() string
GetMethod gets the HTTP method
func (*EchoAdapter) GetUserAgent ¶
func (a *EchoAdapter) GetUserAgent() string
GetUserAgent gets the User-Agent header
type MiddlewareConfig ¶
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
}
MiddlewareConfig configures the payment middleware
type MiddlewareOption ¶
type MiddlewareOption func(*MiddlewareConfig)
MiddlewareOption configures the middleware
func WithErrorHandler ¶
func WithErrorHandler(handler func(echo.Context, error)) MiddlewareOption
WithErrorHandler sets a custom error handler
func WithFacilitatorClient ¶
func WithFacilitatorClient(client x402.FacilitatorClient) MiddlewareOption
WithFacilitatorClient adds a facilitator client
func WithPaywallConfig ¶
func WithPaywallConfig(config *x402http.PaywallConfig) MiddlewareOption
WithPaywallConfig sets the paywall configuration
func WithScheme ¶
func WithScheme(network x402.Network, schemeServer x402.SchemeNetworkServer) MiddlewareOption
WithScheme registers a scheme server
func WithSettlementHandler ¶
func WithSettlementHandler(handler func(echo.Context, *x402.SettleResponse)) MiddlewareOption
WithSettlementHandler sets a custom settlement handler
func WithSyncFacilitatorOnStart ¶
func WithSyncFacilitatorOnStart(sync bool) MiddlewareOption
WithSyncFacilitatorOnStart sets whether to sync with facilitator on startup
func WithTimeout ¶
func WithTimeout(timeout time.Duration) MiddlewareOption
WithTimeout sets the context timeout for payment operations
type SchemeConfig ¶
type SchemeConfig struct {
Network x402.Network
Server x402.SchemeNetworkServer
}
SchemeConfig configures a payment scheme for a network.
type SchemeRegistration ¶
type SchemeRegistration struct {
Network x402.Network
Server x402.SchemeNetworkServer
}
SchemeRegistration registers a scheme with the server