x402_axum/
lib.rs

1//! Axum middleware and helpers for enforcing [x402](https://www.x402.org) payments.
2//!
3//! This crate provides an [`X402Middleware`] Axum layer for protecting routes with payment enforcement,
4//! as well as a [`FacilitatorClient`] for communicating with remote x402 facilitators.
5//!
6//! ## Quickstart
7//!
8//! ```rust,no_run
9//! use axum::{Router, routing::get, Json};
10//! use axum::response::IntoResponse;
11//! use http::StatusCode;
12//! use serde_json::json;
13//! use x402_axum::{X402Middleware, IntoPriceTag};
14//! use x402_rs::network::{Network, USDCDeployment};
15//!
16//! let x402 = X402Middleware::try_from("https://facilitator.example.com/").unwrap();
17//! // You can construct `TokenAsset` manually. Here we use known USDC on Base Sepolia
18//! let usdc = USDCDeployment::by_network(Network::BaseSepolia).pay_to("0xADDRESS");
19//!
20//! let app: Router = Router::new().route(
21//!     "/paywall",
22//!     get(my_handler).layer(
23//!         x402.with_description("Premium Content")
24//!             .with_price_tag(usdc.amount(0.025).unwrap()),
25//!     ),
26//! );
27//!
28//! async fn my_handler() -> impl IntoResponse {
29//!     (StatusCode::OK, Json(json!({ "hello": "world" })))
30//! }
31//! ```
32//! See [`X402Middleware`] for full configuration options.
33//! For low-level interaction with the facilitator, see [`facilitator_client::FacilitatorClient`].
34//!
35//! ## Defining Prices
36//!
37//! To define price tags for your protected routes, see the [`price`] module.
38//! It provides builder-style helpers like [`IntoPriceTag`] and types like [`PriceTag`]
39//! for working with tokens, networks, and payment amounts.
40
41pub mod facilitator_client;
42pub mod layer;
43pub mod price;
44
45pub use layer::X402Middleware;
46pub use price::*;