Skip to content

Example: pre-pay check of an x402 endpoint in a CrewAI agent - #5

Open
IlhamEl777 wants to merge 1 commit into
agent402-dev:mainfrom
IlhamEl777:example/crewai-prepay-check
Open

IlhamEl777 wants to merge 1 commit into
agent402-dev:mainfrom
IlhamEl777:example/crewai-prepay-check

Conversation

@IlhamEl777

Copy link
Copy Markdown

/claim #2

What this adds

examples/crewai-prepay-check/ — a runnable CrewAI example that checks an x402 paid endpoint before anything pays for it, as asked in #2:

  • tool.py — a CrewAI custom tool (check_x402_endpoint) that runs npx -y @agent402-dev/pay-server check <url> --json and returns the JSON report (fixes, findings incl. the price, signedOrPaid) to the agent. One request, no payment header, nothing signed or paid, no key read. Non-https URLs are refused before the CLI is started; the only exception is http on localhost/127.0.0.1 (which the CLI itself also allows) so the local demo below can run.
  • main.py — one agent + one task. The agent's instructions say explicitly: report the price, and do not pay when fixes > 0. Model is CrewAI's default, overridable via CREWAI_EXAMPLE_MODEL; an LLM key for CrewAI is the only key involved.
  • broken402.py — a ~20-line local server answering a broken 402 (no PAYMENT-REQUIRED header) for the negative case.
  • requirements.txt — crewai==1.15.50 pinned. README.md — install, run, what the tool returns, and the line that nothing is paid.

No payment code, payment rules/limits, keys, wallets or signing are touched.

How to run

cd examples/crewai-prepay-check
pip install -r requirements.txt        # Python 3.10+, Node 18+ for npx
export OPENAI_API_KEY=...              # or your CrewAI provider key
python main.py "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com"
python broken402.py &                  # negative demo
python main.py http://127.0.0.1:8402/paid

What I ran (versions: Python 3.12.3, CrewAI 1.15.50, Node v24.22.1, pay-server 0.15.0 via npx)

Honest note on method: my sandbox has no LLM API key, so I could not do a full Crew.kickoff() run. I verified everything else end-to-end: the crew/agent/task wiring builds with the tool attached, and I ran the tool itself (the component that does the checking and produces every field the agent's answer is built from) against both required cases:

(a) Live x402 endpoint — the toolkit's own hosted paid check402 route (0.01 USDC), through the tool:

{
  "url": "https://agent402-check402.agent402-ebc.workers.dev/x402/check402?url=https://example.com",
  "fixes": 0,
  "findings": [
    { "level": "ok", "code": "header", "message": "PAYMENT-REQUIRED header decodes." },
    { "level": "ok", "code": "price", "message": "accepts[0]: Solana mainnet, 0.01 USDC (amount \"10000\")." },
    { "level": "ok", "code": "price", "message": "accepts[1]: Base, 0.01 USDC (amount \"10000\")." }
  ],
  "signedOrPaid": false
}

Price shown, fixes: 0 → the agent's instructions say this is the payable case.

(b) Broken local 402 — broken402.py on http://127.0.0.1:8402/paid, through the tool:

{
  "url": "http://127.0.0.1:8402/paid",
  "fixes": 1,
  "findings": [
    { "level": "fix", "code": "header_missing", "message": "HTTP 402 without a PAYMENT-REQUIRED header." }
  ],
  "signedOrPaid": false
}

fixes: 1 → the agent's instructions make it state it will not pay, listing this finding.

(c) Non-https refusal — http://example.com/paid returns a refusal report ("Refused: only https URLs are checked...", fixes: null) without the CLI being started, verified by the same test run.

AI tools were used to write this, as the issue allows; the checks above were run by me on the versions listed.

Custom CrewAI tool that runs `pay-server check <url> --json` before
anything pays: returns fixes/findings/price to the agent, refuses
non-https URLs before starting the CLI, and the agent's instructions
say do not pay when fixes > 0. Includes a local broken-402 server for
the negative demo. Nothing is signed or paid.
@opirebot

opirebot Bot commented Dec 5, 2026

Copy link
Copy Markdown

👀 We've notified the reward creators here.

This branch has not been deployed

No deployments
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.

1 participant