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 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.
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.
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.