Skip to content

Add OpenAI Agents SDK x402 tool example - #422

Merged
M0nsxx merged 3 commits into
nirium-protocol:mainfrom
Edgadafi:feat/365-openai-agents-x402
Nov 21, 2026
Merged

M0nsxx merged 3 commits into
nirium-protocol:mainfrom
Edgadafi:feat/365-openai-agents-x402

Conversation

@Edgadafi

Copy link
Copy Markdown
Contributor

Closes #365

Summary
Adds examples/openai-agents-x402/: an OpenAI Agents SDK @function_tool that wraps Nirium's x402_fetch() with a per-payment spend cap. Payments above the cap are refused before signing and return a clear X402SpendCapError the agent can explain.

What's included

  • nirium_tool.py: @function_tool wrapper with max_amount_per_payment
  • agent.py: OpenAI Agents SDK Agent using the tool
  • test_openai_tool.py: 8 offline tests (no network, no LLM)
  • README.md, pinned requirements.txt, .env.example, .gitignore

Tests
8/8 offline tests pass, including test_payment_above_cap_refused (nothing is signed when over the cap).

Testnet evidence (own Friendbot keys)

Versions tested
nirium==0.12.7, openai-agents==0.25.0, python-dotenv==1.0.0, pytest==8.3.0, pytest-asyncio==0.24.0

Demo (34 s screen recording)
https://github.com/user-attachments/assets/8d178454-afff-4d86-81c2-d5c34935e8fc

Testnet only. No secrets committed.

@drips-wave

drips-wave Bot commented Nov 20, 2026

Copy link
Copy Markdown

@Edgadafi Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

…col#365)

Co-authored-by: Edgadafi <Edgadafi@users.noreply.github.com>

@Eras256 Eras256 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted review (Claude)

Thanks Edgar, the tool is clean and the tests cover the cap well. I checked both payments on testnet (144d2090 and 1bed22cb): your account pays 0.02 USDC each time.

One item from #365 is still missing. The payments go to GC4Q5TWW…, which is Nirium's own testnet endpoint. The issue asks the agent to "pay a testnet x402Serve route you run". Please start a small route with x402Serve (Node SDK) on testnet with your own payTo address, point the tool at it, and put that paid call and the refused one in the README, with the hashes on Stellar Expert.

nirium-protocol#365 asks the agent to pay a testnet x402Serve route you run. agent.py
already expected a server on localhost:3402, but none was in the PR, so
the earlier payments went to Nirium's testnet endpoint.

server/ adds that route: /paid/market-summary at X402_PRICE (below the
agent's $1 cap) and /paid/full-report at $2 (above it), paid to
SELLER_PUBLIC_KEY. agent.py Test 2 now calls the $2 route and expects
the refusal. README and .env.example explain the setup; the evidence
table for a run against your own route is left PENDING for the author.

Checked locally: both routes answer an x402 v2 402 on stellar:testnet
(200000 and 20000000 units to payTo); nirium 0.12.7 with a $1 cap
refuses /paid/full-report before signing; the 8 offline tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Eras256

Eras256 commented Nov 20, 2026

Copy link
Copy Markdown
Member

AI-assisted comment (Claude)

Edgar, I pushed 2cacf10 to your branch with the missing piece, so you only need to run it:

  • server/: your own x402Serve route on testnet. /paid/market-summary costs X402_PRICE ($0.02, below the $1 cap) and /paid/full-report costs $2 (above it). Payments go to SELLER_PUBLIC_KEY.
  • agent.py: Test 2 now calls the $2 route, so the refusal happens against your server too.
  • .env.example and README: setup steps, plus an evidence table marked PENDING.

I checked it locally: both routes answer an x402 v2 402 on stellar:testnet, the Python SDK with a $1 cap refuses /paid/full-report with "Nothing was signed or sent", and your 8 offline tests still pass.

What's left is yours, with your own keys:

  1. A second testnet account for SELLER_PUBLIC_KEY (it needs a USDC trustline to receive), and a free FACILITATOR_API_KEY from https://channels.openzeppelin.com/testnet/gen
  2. cd server && npm install && npm start, then python agent.py
  3. Fill the PENDING table with the seller account, the paid tx hash and the refusal message, and update the video if you can.

Once that's in, this is ready to merge.

@Edgadafi

Copy link
Copy Markdown
Contributor Author

Thanks @Eras256 for 2cacf10, the local server made this much easier to verify. I ran it with my own keys on stellar:testnet and filled the evidence table in ae185cf:

  • Seller (payTo): GDYJA25NGJWWMGTAC73LG4J2X6FCVSTC7CTVJZKT3EZSBUYM5WOKIV5Z
  • Paid /paid/market-summary via @function_tool: 2d9235ab60a8908735cdc9cb4ab49f82dc48a9ae2b59c245cf94ffcafec9b0a6 (0.02 USDC). The attached video is a second run with the same setup: bee03b8b35a69e5e17ecec49bdeb3c2f36f3f4fd0f35dbd736c321cb406dc209.
  • /paid/full-report ($2) refused by the $1 cap, no transaction signed.

I also fixed agent.py to use Runner.run(agent, …) and .final_output, since Agent has no .run() in openai-agents 0.25.0. Ready for another look.

Demo video
https://github.com/user-attachments/assets/167e83dc-0d71-415f-9cf1-2c6908bb905d

@M0nsxx M0nsxx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted review (Claude)

Own-route testnet evidence checked on Horizon: both payments (2d9235ab…, bee03b8b…) go from Edgar's payer to his own seller GDYJA25N…IV5Z, 0.02 USDC each, and the $2 call is refused by the cap before signing. agent.py now uses Runner.run + final_output (openai-agents 0.25.0). CI green, no conflicts. Approved.

@Eras256
Eras256 dismissed their stale review November 21, 2026 18:52

Edgar filled the own-route testnet evidence in ae185cf (verified on Horizon) and fixed Runner.run. Nothing left from this review.

@M0nsxx
M0nsxx merged commit 25c739c into nirium-protocol:main Nov 21, 2026
20 checks passed
M0nsxx pushed a commit that referenced this pull request Nov 21, 2026
)

Edgar's OpenAI Agents SDK example merged 21 Nov with his own testnet
run (tx 2d9235ab..., 0.02 USDC to his own seller account, verified on
Horizon). Wave count 12 -> 13, outside total 36 -> 37, counted with the
pulls API across nirium-protocol repos.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build with Nirium: an x402 payment tool for the OpenAI Agents SDK (Python), on testnet

3 participants