Skip to main content

Module upto

Module upto 

Source
Available on crate feature server only.
Expand description

HTTP-level support for the x402 “upto” scheme.

The upto scheme lets a buyer authorise a maximum payment and lets the resource server decide the actual charge at request time (e.g. for usage-based pricing). This module defines the UptoActualAmount response extension that handlers use to communicate the final charge back to the r402 middleware.

§Flow

client          middleware           handler
  |                 |                    |
  | POST + sig ---->| verify             |
  |                 |  ok                |
  |                 |---- request ------>|
  |                 |                    |  (compute usage,
  |                 |                    |   set extension)
  |                 |<--- response + ext |
  |                 | read UptoActualAmount
  |                 |    → override
  |                 |    → settle(actual)
  |<------- resp ---|

§Example

use axum::response::IntoResponse;
use r402_http::server::UptoActualAmount;

async fn handler() -> impl IntoResponse {
    let mut response = "Hello".into_response();
    response
        .extensions_mut()
        .insert(UptoActualAmount::new("125000")); // 0.125 USDC
    response
}

§Compatibility

Only SettlementMode::Sequential honours this extension: concurrent and background modes spawn settlement before the handler returns, so the override has nowhere to land. Mixing upto with those modes silently charges the signed maximum.

Structs§

UptoActualAmount
Response extension instructing the r402 middleware to settle the upto payment for this specific amount (base units as a decimal string).