pub struct X402Middleware<F> { /* private fields */ }server only.Expand description
The main X402 middleware instance for enforcing x402 payments on routes.
Create a single instance per application and use it to build payment layers for protected routes.
Implementations§
Source§impl<F> X402Middleware<F>
impl<F> X402Middleware<F>
Sourcepub const fn from_facilitator(facilitator: F) -> Self
pub const fn from_facilitator(facilitator: F) -> Self
Creates a middleware instance from any facilitator implementation.
Use this when you already have a configured facilitator (e.g. one with custom timeouts, caching, or a non-default HTTP client).
Sourcepub const fn facilitator(&self) -> &F
pub const fn facilitator(&self) -> &F
Returns a reference to the underlying facilitator.
Source§impl X402Middleware<Arc<FacilitatorClient>>
impl X402Middleware<Arc<FacilitatorClient>>
Sourcepub fn new(url: &str) -> Self
pub fn new(url: &str) -> Self
Creates a new middleware instance with a default facilitator URL.
§Panics
Panics if the facilitator URL is invalid.
Sourcepub fn try_new(url: &str) -> Result<Self, Box<dyn Error>>
pub fn try_new(url: &str) -> Result<Self, Box<dyn Error>>
Creates a new middleware instance with a facilitator URL.
§Errors
Returns an error if the URL is invalid.
Sourcepub fn facilitator_url(&self) -> &Url
pub fn facilitator_url(&self) -> &Url
Returns the configured facilitator URL.
Sourcepub fn with_supported_cache_ttl(&self, ttl: Duration) -> Self
pub fn with_supported_cache_ttl(&self, ttl: Duration) -> Self
Sets the TTL for caching the facilitator’s supported response.
Default is 10 minutes. Use FacilitatorClient::without_supported_cache()
to disable caching entirely.
Sourcepub fn with_facilitator_timeout(&self, timeout: Duration) -> Self
pub fn with_facilitator_timeout(&self, timeout: Duration) -> Self
Sets a per-request timeout for all facilitator HTTP calls (verify, settle, supported).
Without this, the underlying reqwest::Client uses no timeout by default,
which can cause requests to hang indefinitely if the facilitator is slow
or unreachable, eventually triggering OS-level TCP timeouts (typically 2–5 minutes).
A reasonable production value is 30 seconds.
Source§impl<F> X402Middleware<F>where
F: Clone,
impl<F> X402Middleware<F>where
F: Clone,
Sourcepub fn with_base_url(&self, base_url: Url) -> Self
pub fn with_base_url(&self, base_url: Url) -> Self
Sets the base URL used to construct resource URLs dynamically.
If X402LayerBuilder::with_resource is not called, this base URL is combined with
each request’s path/query to compute the resource. If not set, defaults to http://localhost/.
In production, prefer calling with_resource or setting a precise base_url.
Source§impl<TFacilitator> X402Middleware<TFacilitator>where
TFacilitator: Clone,
impl<TFacilitator> X402Middleware<TFacilitator>where
TFacilitator: Clone,
Sourcepub fn with_price_tag(
&self,
price_tag: PriceTag,
) -> X402LayerBuilder<StaticPriceTags, TFacilitator>
pub fn with_price_tag( &self, price_tag: PriceTag, ) -> X402LayerBuilder<StaticPriceTags, TFacilitator>
Sets the price tag for the protected route.
Creates a layer builder that can be further configured with additional price tags and resource information.
Sets multiple price tags for the protected route.
Convenience method for services that accept several payment options (e.g. multiple tokens / networks). Returns an empty-bypass builder when the list is empty — the middleware will pass requests through without payment enforcement.
Sourcepub fn with_dynamic_price<F, Fut>(
&self,
callback: F,
) -> X402LayerBuilder<DynamicPriceTags, TFacilitator>
pub fn with_dynamic_price<F, Fut>( &self, callback: F, ) -> X402LayerBuilder<DynamicPriceTags, TFacilitator>
Sets a dynamic price source for the protected route.
The callback receives request headers, URI, and base URL, and returns
a vector of V2 price tags.