Raxol.Payments.Req.AutoPay (Raxol Payments v0.2.0)

Copy Markdown View Source

Req plugin that transparently handles HTTP 402 Payment Required responses.

Intercepts 402 responses, detects the payment protocol (x402 or MPP), checks spending budget, signs a payment, and retries the request with payment credentials attached. Retries at most once per request to prevent infinite loops.

Usage

req =
  Req.new(url: "https://api.example.com/data")
  |> Raxol.Payments.Req.AutoPay.attach(
    wallet: Raxol.Payments.Wallets.Env,
    protocols: [:x402, :mpp],
    ledger: ledger_pid,
    policy: SpendingPolicy.dev(),
    agent_id: :my_agent
  )

{:ok, response} = Req.get(req)

Options

  • :wallet (required) -- module implementing Raxol.Payments.Wallet
  • :protocols -- list of protocol atoms, default [:x402, :mpp]
  • :ledger -- Ledger server for budget tracking (optional)
  • :policy -- SpendingPolicy struct; when set, approved_domains and require_confirmation_above are enforced against the request URL host before the wallet is asked to sign
  • :agent_id -- identifier for ledger tracking (required if ledger given)
  • :on_confirm -- 2-arity function (amount, domain) -> :approve | :deny called when policy.require_confirmation_above is exceeded. Runs synchronously inside the Req response step. Absent or non-:approve return denies the payment.

Summary

Functions

Attach the auto-pay plugin to a Req request.

Functions

attach(req, opts)

@spec attach(
  Req.Request.t(),
  keyword()
) :: Req.Request.t()

Attach the auto-pay plugin to a Req request.