feat: add x402 payment agent sample#4937
Open
up2itnow0822 wants to merge 3 commits intogoogle:mainfrom
Open
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. |
Collaborator
|
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! |
Author
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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