feat: add x402 payment agent sample#4937
Conversation
Adds a sample agent that handles HTTP 402 (Payment Required) responses using the x402 protocol. When fetching from a paid API, the agent: 1. Receives HTTP 402 with payment requirements 2. Evaluates cost against configurable spending policy (per-tx cap, daily limit, recipient allowlist) 3. Signs a USDC payment proof 4. Retries the request with the payment header Two tools: - fetch_paid_api: handles the full 402→pay→retry cycle - get_spending_status: reports daily spend and recent payments Includes mock_server.py for local testing (402 without payment, 200 with valid X-PAYMENT header). This pattern was recently merged into NVIDIA's NeMo Agent Toolkit Examples (NVIDIA/NeMo-Agent-Toolkit-Examples#17) and is adapted here for the Google ADK agent framework.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @up2itnow0822, thank you for your contribution! Before we can review this PR, please address the following points from our contribution guidelines:
This information will help us to review your PR more efficiently. Thanks! |
Testing EvidenceAssociated issue: #4938 Mock server outputWithout payment (HTTP 402):With payment (HTTP 200):Code verification:Formatting: |
Improvements based on community feedback (google#4938): 1. Structured payment events — every payment decision emits a typed event (payment_required, payment_receipt, payment_denied, payment_approval_required) with request_id for end-to-end correlation and operator audit. 2. Human-in-the-loop for above-cap payments — instead of silently denying payments that exceed the per-transaction cap, hold them for explicit approval via the new approve_payment tool. Hard denials (daily limit, bad recipient) remain automatic. 3. Request ID tracking — unique request_id flows through all events and the spending log for reconciliation. 4. Pending approvals in status — get_spending_status now shows held payments awaiting approval and session audit event count.
|
Hi @up2itnow0822 , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
Summary
Adds a sample agent that demonstrates autonomous API payment handling using the x402 protocol.
What it does
When fetching from a premium API, the agent:
Tools
fetch_paid_api— handles the full 402 → evaluate policy → pay → retry cycleget_spending_status— reports daily spend, remaining budget, and recent paymentsTesting
Includes
mock_server.pyfor local e2e testing:The mock server returns HTTP 402 for unauthenticated requests and HTTP 200 with sample market data when a valid
X-PAYMENTheader is present.Testing plan
Why this matters
Four major agent toolkits hit trending this week with zero payment infrastructure. As agents start accessing premium APIs and services, they need a standard way to handle paywalled endpoints. The x402 protocol (by Coinbase) provides this — HTTP 402 as the native payment negotiation mechanism.
This pattern was recently merged into NVIDIA's NeMo Agent Toolkit and is adapted here for the ADK framework.
Files