> ## Documentation Index
> Fetch the complete documentation index at: https://docs.xenarch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Agent

> Minimal Python x402 agent example — check the payment gate, pay a USDC micropayment on Base, and fetch gated content. Copy-paste ready.

The simplest possible Xenarch agent. No frameworks, no abstractions — raw SDK calls showing the complete payment flow.

## Setup

```bash theme={null}
pip install xenarch[agent] python-dotenv httpx
```

Create `.env`:

```bash theme={null}
XENARCH_PRIVATE_KEY=0xYOUR_PRIVATE_KEY
```

## Code

```python main.py theme={null}
"""Minimal Xenarch agent: check gate, pay, get content."""

import sys

import httpx
from dotenv import load_dotenv

from xenarch.agent_client import check_gate, verify_payment
from xenarch.payment import execute_payment
from xenarch.wallet import load_wallet_or_create

load_dotenv()


def main(url: str) -> None:
    wallet = load_wallet_or_create()
    print(f"Wallet: {wallet.address}")
    print(f"Network: {wallet.network}\n")

    # 1. Check if the URL has a payment gate
    print(f"Checking gate for: {url}")
    gate = check_gate(url)
    if not gate:
        print("No Xenarch gate found — content is free.")
        return

    print(f"Gate found! Price: ${gate.price_usd} ({gate.asset} on {gate.network})\n")

    # 2. Execute on-chain USDC payment
    print("Executing payment...")
    result = execute_payment(
        wallet=wallet,
        splitter_address=gate.splitter,
        collector_address=gate.collector,
        price_usd=gate.price_usd,
    )
    print(f"Payment confirmed!")
    print(f"  TX: {result.tx_hash}")
    print(f"  Block: {result.block_number}\n")

    # 3. Verify payment and get access token
    print("Verifying payment...")
    verification = verify_payment(gate.verify_url, result.tx_hash)
    access_token = verification["access_token"]
    expires_at = verification["expires_at"]
    print(f"Access token received (expires: {expires_at})\n")

    # 4. Fetch the gated content with the access token
    print("Fetching gated content...")
    resp = httpx.get(
        url,
        headers={
            "Authorization": f"Bearer {access_token}",
            "User-Agent": "xenarch-example/1.0",
        },
    )
    print(f"Content retrieved successfully ({len(resp.content):,} bytes)")


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python main.py <url>")
        sys.exit(1)
    main(sys.argv[1])
```

## Run

```bash theme={null}
python main.py https://example.com/premium-article
```

## Output

```
Wallet: 0x1234...abcd
Network: base

Checking gate for: https://example.com/premium-article
Gate found! Price: $0.003 (USDC on base)

Executing payment...
Payment confirmed!
  TX: 0xabc123...
  Block: 28451023

Verifying payment...
Access token received (expires: 2026-04-10T15:05:00Z)

Fetching gated content...
Content retrieved successfully (1,234 bytes)
```

## What each step does

1. **`load_wallet_or_create()`** — loads your private key from env or `~/.xenarch/wallet.json`. Creates a new wallet on first run.
2. **`check_gate(url)`** — makes an HTTP request to the URL. If the server returns 402, parses the gate details.
3. **`execute_payment()`** — approves USDC and calls `split()` on the splitter contract. Waits for on-chain confirmation.
4. **`verify_payment()`** — submits the tx hash to Xenarch. Returns a time-limited access token.
5. **Content fetch** — re-requests the URL with the Bearer token. Server validates locally and returns content.
