Moon

 · 大约4小时 ago

A Rust-based implementation of the x402 protocol

The x402 protocol is a proposed standard for making blockchain payments directly through HTTP using native 402 Payment Required status code.

A Rust-based implementation of the x402 protocol.

This repository provides:

  • x402-rs (current crate):
    • Core protocol types, facilitator traits, and logic for on-chain payment verification and settlement
    • Facilitator binary - production-grade HTTP server to verify and settle x402 payments
  • x402-axum - Axum middleware for accepting x402 payments,
  • x402-reqwest - Wrapper for reqwest for transparent x402 payments,
  • x402-axum-example - an example of x402-axum usage.
  • x402-reqwest-example - an example of x402-reqwest usage.

About x402

The x402 protocol is a proposed standard for making blockchain payments directly through HTTP using native 402 Payment Required status code.

Servers declare payment requirements for specific routes. Clients send cryptographically signed payment payloads. Facilitators verify and settle payments on-chain.

Getting Started###

Run facilitator

docker run --env-file .env -p 8080:8080 ukstv/x402-facilitator

Or build locally:

docker build -t x402-rs . docker run --env-file .env -p 8080:8080 x402-rs

See the #facilitator">Facilitator section below for full usage details

Protect Axum Routes

Use x402-axum to gate your routes behind on-chain payments:

let x402 = X402Middleware::try_from("https://x402.org/facilitator/").unwrap(); let usdc = USDCDeployment::by_network(Network::BaseSepolia);

let app = Router::new().route("/paid-content", get(handler).layer( x402.with_price_tag(usdc.amount("0.025").pay_to("0xYourAddress").unwrap()) ), );

See x402-axum crate docs.

Send x402 paymentsUse x402-reqwest to send payments:

let signer: PrivateKeySigner = "0x...".parse()?; // never hardcode real keys!

let client = reqwest::Client::new() .with_payments(signer) .prefer(USDCDeployment::by_network(Network::Base)) .max(USDCDeployment::by_network(Network::Base).amount("1.00")?) .build();

let res = client .get("https://example.com/protected") .send() .await?;

See x402-reqwest crate docs.