http/nethttp
import "github.com/x402-foundation/x402/go/v2/http/nethttp"
Functions
func NewNetHTTPAdapter(r *http.Request) *NetHTTPAdapter
Source: http/nethttp/adapter.go:14
NewNetHTTPAdapter creates a new adapter wrapping the given HTTP request.
func PayloadFromContext(ctx context.Context) (*types.PaymentPayload, bool)
Source: http/nethttp/context.go:22
PayloadFromContext retrieves the payment payload from the request context. Returns nil and false if no payload is present.
func PaymentMiddleware(routes x402http.RoutesConfig, server *x402.X402ResourceServer, opts ...MiddlewareOption) func(http.Handler) http.Handler
Source: http/nethttp/middleware.go:122
PaymentMiddleware creates net/http middleware for x402 payment handling using a pre-configured server.
func PaymentMiddlewareFromConfig(routes x402http.RoutesConfig, opts ...MiddlewareOption) func(http.Handler) http.Handler
Source: http/nethttp/middleware.go:151
PaymentMiddlewareFromConfig creates net/http middleware for x402 payment handling. This creates the server internally from the provided options.
func PaymentMiddlewareFromHTTPServer(httpServer *x402http.HTTPServer, opts ...MiddlewareOption) func(http.Handler) http.Handler
Source: http/nethttp/middleware.go:202
PaymentMiddlewareFromHTTPServer creates net/http 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)
handler := nethttp.PaymentMiddlewareFromHTTPServer(httpServer)(mux)func RequirementsFromContext(ctx context.Context) (*types.PaymentRequirements, bool)
Source: http/nethttp/context.go:33
RequirementsFromContext retrieves the payment requirements from the request context. Returns nil and false if no requirements are present.
func SetSettlementOverrides(w http.ResponseWriter, overrides *x402.SettlementOverrides)
Source: http/nethttp/middleware.go:21
SetSettlementOverrides sets settlement overrides on the 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) func(http.Handler) http.Handler
Source: http/nethttp/builder.go:128
SimpleX402Payment creates middleware with minimal configuration. Uses a single route pattern and facilitator for the simplest possible setup.
Example:
mux := http.NewServeMux()
handler := nethttp.SimpleX402Payment(
"0x123...",
"$0.001",
"eip155:8453",
"https://facilitator.example.com",
)(mux)func WithErrorHandler(handler func(w http.ResponseWriter, r *http.Request, err error)) MiddlewareOption
Source: http/nethttp/middleware.go:97
WithErrorHandler sets a custom error handler.
func WithFacilitatorClient(client x402.FacilitatorClient) MiddlewareOption
Source: http/nethttp/middleware.go:66
WithFacilitatorClient adds a facilitator client.
func WithPaywallConfig(config *x402http.PaywallConfig) MiddlewareOption
Source: http/nethttp/middleware.go:83
WithPaywallConfig sets the paywall configuration.
func WithScheme(network x402.Network, schemeServer x402.SchemeNetworkServer) MiddlewareOption
Source: http/nethttp/middleware.go:73
WithScheme registers a scheme server.
func WithSettlementHandler(handler func(w http.ResponseWriter, r *http.Request, resp *x402.SettleResponse)) MiddlewareOption
Source: http/nethttp/middleware.go:104
WithSettlementHandler sets a custom settlement handler.
func WithSyncFacilitatorOnStart(sync bool) MiddlewareOption
Source: http/nethttp/middleware.go:90
WithSyncFacilitatorOnStart sets whether to sync with facilitator on startup.
func WithTimeout(timeout time.Duration) MiddlewareOption
Source: http/nethttp/middleware.go:111
WithTimeout sets the context timeout for payment operations.
func X402Payment(config Config) func(http.Handler) http.Handler
Source: http/nethttp/builder.go:67
X402Payment creates payment middleware using struct-based configuration. This is a cleaner, more readable alternative to PaymentMiddlewareFromConfig with variadic options.
Example:
mux := http.NewServeMux()
handler := nethttp.X402Payment(nethttp.Config{
Routes: routes,
Facilitator: facilitatorClient,
Schemes: []nethttp.SchemeConfig{
{Network: "eip155:*", Server: evm.NewExactEvmServer()},
},
SyncFacilitatorOnStart: true,
Timeout: 30 * time.Second,
})(mux)Types
type Config
Source: http/nethttp/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(w http.ResponseWriter, r *http.Request, err error)
// SettlementHandler called after successful settlement (optional).
SettlementHandler func(w http.ResponseWriter, r *http.Request, resp *x402.SettleResponse)
}Fields
Routes x402http.RoutesConfigRoutes maps HTTP patterns to payment requirements.
Facilitator x402.FacilitatorClientFacilitator is a single facilitator client (most common case). Use this OR Facilitators (not both).
Facilitators []x402.FacilitatorClientFacilitators is an array of facilitator clients (for fallback/redundancy). Use this OR Facilitator (not both).
Schemes []SchemeConfigSchemes to register with the server.
PaywallConfig *x402http.PaywallConfigPaywallConfig for browser-based payment UI (optional).
SyncFacilitatorOnStart boolSyncFacilitatorOnStart fetches supported kinds from facilitators on startup. Default: true
Timeout time.DurationTimeout for payment operations. Default: 30 seconds
ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)ErrorHandler for custom error handling (optional).
SettlementHandler func(w http.ResponseWriter, r *http.Request, resp *x402.SettleResponse)SettlementHandler called after successful settlement (optional).
type MiddlewareConfig
Source: http/nethttp/middleware.go:30
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(w http.ResponseWriter, r *http.Request, err error)
// Custom settlement handler
SettlementHandler func(w http.ResponseWriter, r *http.Request, resp *x402.SettleResponse)
// Context timeout for payment operations
Timeout time.Duration
}Fields
Routes x402http.RoutesConfigRoutes configuration
FacilitatorClients []x402.FacilitatorClientFacilitator client(s)
Schemes []SchemeRegistrationScheme registrations
PaywallConfig *x402http.PaywallConfigPaywall configuration
SyncFacilitatorOnStart boolSync with facilitator on start
ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)Custom error handler
SettlementHandler func(w http.ResponseWriter, r *http.Request, resp *x402.SettleResponse)Custom settlement handler
Timeout time.DurationContext timeout for payment operations
type MiddlewareOption
Source: http/nethttp/middleware.go:63
MiddlewareOption configures the middleware.
type MiddlewareOption func(*MiddlewareConfig)type NetHTTPAdapter
Source: http/nethttp/adapter.go:9
NetHTTPAdapter implements HTTPAdapter for the standard net/http library.
type NetHTTPAdapter struct {
// contains filtered or unexported fields
}func GetAcceptHeader() string
Source: http/nethttp/adapter.go:47
GetAcceptHeader gets the Accept header.
func GetHeader(name string) string
Source: http/nethttp/adapter.go:19
GetHeader gets a request header by name.
func GetMethod() string
Source: http/nethttp/adapter.go:24
GetMethod gets the HTTP method.
func GetPath() string
Source: http/nethttp/adapter.go:29
GetPath gets the request path.
func GetURL() string
Source: http/nethttp/adapter.go:34
GetURL gets the full request URL.
func GetUserAgent() string
Source: http/nethttp/adapter.go:52
GetUserAgent gets the User-Agent header.
type SchemeConfig
Source: http/nethttp/builder.go:47
SchemeConfig configures a payment scheme for a network.
type SchemeConfig struct {
Network x402.Network
Server x402.SchemeNetworkServer
}Fields
Network x402.NetworkServer x402.SchemeNetworkServer
type SchemeRegistration
Source: http/nethttp/middleware.go:57
SchemeRegistration registers a scheme with the server.
type SchemeRegistration struct {
Network x402.Network
Server x402.SchemeNetworkServer
}Fields
Network x402.NetworkServer x402.SchemeNetworkServer
