Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/crewai-prepay-check/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.venv/
__pycache__/
*.py[cod]
test-ca.pem
.env
.env.*
307 changes: 307 additions & 0 deletions examples/crewai-prepay-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
# CrewAI x402 Pre-payment Check

A read-only example demonstrating how a CrewAI agent can
inspect an x402 endpoint before considering payment.

The custom tool wraps the published Agent402 Pay Server CLI,
extracts its structured findings and pricing information,
and enables the agent to make a pre-payment assessment.

The example never signs transactions or performs payments.
It requires no wallet, private key or RPC connection.

## Architecture

```text
User-provided HTTPS URL
|
v
CrewAI Agent
|
v
PrepayCheckTool
|
v
Agent402 Pay Server CLI
|
v
x402 Endpoint
|
v
JSON Findings and Pricing
|
v
Agent Assessment and Verified Result
```

## Requirements

- Python 3.11 or 3.12.
- Node.js 20 or later.
- npm and npx.
- A CrewAI-compatible LLM provider.
- Internet connectivity.

The default configuration uses Groq. An API key is
required for this provider, but no wallet credentials
are needed.

## Tested environment

| Component | Version |
| --- | --- |
| Operating system | Ubuntu Linux |
| Python | 3.12.3 |
| Node.js | 20.19.5 |
| npm | 10.8.2 |
| CrewAI | 0.130.0 |
| LiteLLM | 1.72.0 |
| Pydantic | 2.14.4 |
| Trustme | 1.2.1 |
| Agent402 Pay Server | 0.15.0 |
| LLM provider | Groq |
| LLM model | qwen/qwen3.8-27b |

## Installation

From the example directory:

```bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
```

For Windows PowerShell:

```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt
```

## Configure the language model

### Groq

Create an API key at:

https://console.groq.com/keys

Linux/macOS:

```bash
read -rsp "Groq API key: " GROQ_API_KEY
echo
export GROQ_API_KEY

export CREWAI_MODEL="groq/qwen/qwen3.8-27b"
```

Windows PowerShell:

```powershell
$env:GROQ_API_KEY = "<your-api-key>"
$env:CREWAI_MODEL = "groq/qwen/qwen3.8-27b"
```

Never commit API credentials to source control.

### Optional: Ollama

The example also supports a locally hosted Ollama model.

```bash
export CREWAI_MODEL="ollama/qwen2.5:7b"
export OLLAMA_BASE_URL="http://localhost:11434"
```

The selected model must be capable of executing the
CrewAI agent's tool workflow.

The Ollama configuration is supported by the implementation
but was not used for the recorded integration tests.

## Inspect a live x402 endpoint

```bash
python main.py https://agent402.tools/api/uuid
```

### Verified result

Tested on December 5, 2026.

The following output was produced by the verified tool
result:

```text
Endpoint: https://agent402.tools/api/uuid
Price: accepts[0]: Base, 0.001 USDC (amount "1000").
Price: accepts[11]: Base, 0.001 USDC (amount "1000").
Price: accepts[12]: Solana mainnet, 0.001 USDC (amount "1000").
Price: accepts[15]: Base, 0.001 USDC (amount "1000").
fixes: 0
Decision: No required fixes identified. No payment attempted.
Nothing was signed or paid.
```

Live endpoint pricing and findings may change over time.

A zero fixes count does not constitute payment authorization
or guarantee the safety of an endpoint.

## Test a broken x402 endpoint

The included test server returns HTTP 402 with a deliberately
malformed PAYMENT-REQUIRED header.

Start the server:

```bash
python test_server.py
```

In a separate terminal, activate the virtual environment
and configure Node.js to trust the generated test CA.

Linux/macOS:

```bash
export NODE_EXTRA_CA_CERTS="$PWD/test-ca.pem"

python main.py https://127.0.0.1:8443/paid
```

Windows PowerShell:

```powershell
$env:NODE_EXTRA_CA_CERTS = (Resolve-Path ./test-ca.pem).Path

python main.py https://127.0.0.1:8443/paid
```

TLS certificate verification remains enabled.

### Verified result

Tested on December 5, 2026.

```text
Endpoint: https://127.0.0.1:8443/paid
Price: unavailable
fixes: 1
Decision: I will not pay.
Nothing was signed or paid.
```

The underlying CLI returned the following finding:

```json
{
"level": "fix",
"code": "header_undecodable",
"message": "PAYMENT-REQUIRED is not base64 JSON that @x402/core can decode."
}
```

Stop the test server using Ctrl+C.

The generated test-ca.pem is a local testing artifact
and must not be committed.

## Test non-HTTPS rejection

```bash
python main.py http://example.com/paid
```

### Verified result

```text
Error: Only valid HTTPS URLs without embedded credentials are allowed.
I will not pay.
```

The URL is rejected before starting the agent or invoking
the endpoint-checking CLI.

## Custom tool

The custom CrewAI tool is named:

```text
x402_prepay_check
```

It invokes the published npm CLI:

```bash
npx -y @agent402-dev/pay-server@0.15.0 check <url> --json
```

The CLI executes from a temporary working directory.

This prevents npm workspace resolution from selecting the
unbuilt local pay-server package instead of the published
npm package when the example runs inside the monorepo.

The temporary directory is automatically removed after
execution.

## Tool response

| Field | Description |
| --- | --- |
| url | Inspected endpoint URL |
| fixes | Required fixes count, or null on failure |
| findings | Structured findings returned by the CLI |
| amounts | Pricing information extracted from price findings |
| signedOrPaid | Payment and signing status |
| error | Error description, when applicable |

The tool processes structured CLI output even when the
CLI returns a nonzero exit code, since a nonzero exit
code can indicate that required fixes were identified.

Repeated checks of the same URL during a tool instance's
lifetime reuse the cached result.

## Payment decision

The agent is instructed to refuse payment when:

- Required fixes are identified.
- The fixes count is unavailable.
- Inspection fails.

When no required fixes are identified, the agent reports
the result without attempting payment.

The verified tool result is displayed independently of
the agent's interpretation.

The example exposes no wallet, signing or payment tools.

## Exit codes

| Code | Meaning |
| --- | --- |
| 0 | Inspection completed with no required fixes |
| 1 | Required fixes found, invalid input or execution failure |

## Security considerations

- Only HTTPS URLs without embedded credentials are accepted.
- TLS certificate verification remains enabled.
- The subprocess uses an argument list without shell execution.
- CLI execution has a defined timeout.
- Tool findings are treated as untrusted data by the agent.
- No wallet credentials or signing capabilities are provided.
- API credentials are supplied through environment variables.

This is a pre-payment inspection example, not a production
payment authorization system or a comprehensive endpoint
security scanner.

Nothing is signed or paid.
Loading