agentic-x402 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +223 -0
- package/SKILL.md +277 -0
- package/bin/cli.ts +137 -0
- package/bin/x402.js +57 -0
- package/config/example.env +54 -0
- package/package.json +51 -0
- package/scripts/commands/balance.ts +99 -0
- package/scripts/commands/create-link.ts +200 -0
- package/scripts/commands/fetch-paid.ts +131 -0
- package/scripts/commands/link-info.ts +133 -0
- package/scripts/commands/pay.ts +191 -0
- package/scripts/core/client.ts +114 -0
- package/scripts/core/config.ts +100 -0
- package/scripts/core/utils.ts +112 -0
package/README.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# agentic-x402
|
|
2
|
+
|
|
3
|
+
Agent skill for x402 payments — pay for and sell gated content using USDC on Base.
|
|
4
|
+
|
|
5
|
+
## What is x402?
|
|
6
|
+
|
|
7
|
+
[x402](https://docs.x402.org/) is an open payment standard built around HTTP 402 Payment Required. It enables services to charge for API access directly over HTTP using crypto payments.
|
|
8
|
+
|
|
9
|
+
This skill gives AI agents the ability to:
|
|
10
|
+
- **Pay** for x402-gated resources automatically
|
|
11
|
+
- **Fetch** content with automatic payment handling
|
|
12
|
+
- **Create** payment links to sell content (via 21.cash)
|
|
13
|
+
- **Manage** wallet balances (USDC + ETH for gas)
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -g agentic-x402
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or install locally:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm install
|
|
25
|
+
cp config/example.env .env
|
|
26
|
+
# Edit .env and add your EVM_PRIVATE_KEY
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# 1. Configure your wallet
|
|
33
|
+
export EVM_PRIVATE_KEY=0x...your_private_key...
|
|
34
|
+
|
|
35
|
+
# 2. Check balance
|
|
36
|
+
x402 balance
|
|
37
|
+
|
|
38
|
+
# 3. Pay for a resource
|
|
39
|
+
x402 pay https://api.example.com/paid-endpoint
|
|
40
|
+
|
|
41
|
+
# 4. Fetch with auto-payment
|
|
42
|
+
x402 fetch https://api.example.com/data --json
|
|
43
|
+
|
|
44
|
+
# 5. Create a payment link (requires x402-links-server)
|
|
45
|
+
x402 create-link --name "My API" --price 1.00 --url https://api.example.com/premium
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Commands
|
|
49
|
+
|
|
50
|
+
### balance — Check Wallet Balances
|
|
51
|
+
|
|
52
|
+
Show your wallet address, USDC balance (for payments), and ETH balance (for gas). Warns if either balance is low.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
x402 balance
|
|
56
|
+
x402 balance --json
|
|
57
|
+
x402 balance --full
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
| Flag | Description | Default |
|
|
61
|
+
|------|-------------|---------|
|
|
62
|
+
| `--json` | Output as JSON (address, network, chainId, balances) | — |
|
|
63
|
+
| `--full` | Show full wallet address instead of truncated | — |
|
|
64
|
+
| `-h, --help` | Show help | — |
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### pay — Pay for an x402-Gated Resource
|
|
69
|
+
|
|
70
|
+
Make a request to an x402-gated URL. If the server responds with HTTP 402, the CLI extracts payment requirements, verifies the amount is within your max limit, and automatically signs and sends payment. Shows the response body after successful payment.
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
x402 pay https://api.example.com/data
|
|
74
|
+
x402 pay https://api.example.com/submit --method POST --body '{"data":"value"}'
|
|
75
|
+
x402 pay https://api.example.com/data --max 5
|
|
76
|
+
x402 pay https://api.example.com/data --dry-run
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
| Flag | Description | Default |
|
|
80
|
+
|------|-------------|---------|
|
|
81
|
+
| `<url>` | The URL of the x402-gated resource (positional) | **required** |
|
|
82
|
+
| `--method` | HTTP method | `GET` |
|
|
83
|
+
| `--body` | Request body (for POST/PUT requests) | — |
|
|
84
|
+
| `--header` | Add custom header (can be used multiple times) | — |
|
|
85
|
+
| `--max` | Maximum payment in USD (overrides config) | from config |
|
|
86
|
+
| `--dry-run` | Show payment details without paying | — |
|
|
87
|
+
| `-h, --help` | Show help | — |
|
|
88
|
+
|
|
89
|
+
If the URL does not require payment (non-402 response), the response is returned directly.
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
### fetch — Fetch with Automatic Payment
|
|
94
|
+
|
|
95
|
+
A simpler, pipe-friendly version of `pay`. Uses the x402 SDK's wrapped fetch to handle 402 responses automatically. Supports `--json` and `--raw` output modes for easy integration with scripts and agents.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
x402 fetch https://api.example.com/data
|
|
99
|
+
x402 fetch https://api.example.com/data --json
|
|
100
|
+
x402 fetch https://api.example.com/data --raw
|
|
101
|
+
x402 fetch https://api.example.com/submit --method POST --body '{"key":"value"}'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
| Flag | Description | Default |
|
|
105
|
+
|------|-------------|---------|
|
|
106
|
+
| `<url>` | The URL to fetch (positional) | **required** |
|
|
107
|
+
| `--method` | HTTP method | `GET` |
|
|
108
|
+
| `--body` | Request body (for POST/PUT) | — |
|
|
109
|
+
| `--header` | Add header as `"Key: Value"` | — |
|
|
110
|
+
| `--json` | Output as JSON only (for piping to other tools) | — |
|
|
111
|
+
| `--raw` | Output raw response body only (no headers or status) | — |
|
|
112
|
+
| `-h, --help` | Show help | — |
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### create-link — Create a Payment Link
|
|
117
|
+
|
|
118
|
+
Create a payment link via the [21.cash](https://21.cash) x402-links-server. You can gate a URL or text content behind a USDC payment. Requires `X402_LINKS_API_URL` and `X402_LINKS_API_KEY` environment variables.
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
x402 create-link --name "Premium Guide" --price 5.00 --url https://mysite.com/guide.pdf
|
|
122
|
+
x402 create-link --name "Secret Message" --price 0.50 --text "The secret is..."
|
|
123
|
+
x402 create-link --name "API Access" --price 1.00 --url https://api.example.com/data --webhook https://mysite.com/webhook
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
| Flag | Description | Default |
|
|
127
|
+
|------|-------------|---------|
|
|
128
|
+
| `--name` | Name of the payment link | **required** |
|
|
129
|
+
| `--price` | Price in USD (e.g., `"5.00"` or `"0.10"`) | **required** |
|
|
130
|
+
| `--url` | URL to gate behind payment | — |
|
|
131
|
+
| `--text` | Text content to gate behind payment | — |
|
|
132
|
+
| `--desc` | Description of the link | — |
|
|
133
|
+
| `--webhook` | Webhook URL for payment notifications | — |
|
|
134
|
+
| `--json` | Output as JSON | — |
|
|
135
|
+
| `-h, --help` | Show help | — |
|
|
136
|
+
|
|
137
|
+
> **Note:** Either `--url` or `--text` is required. The link is deployed as a smart contract on Base.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
### link-info — Get Payment Link Details
|
|
142
|
+
|
|
143
|
+
Look up details of a payment link by its router address or full URL.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
x402 link-info 0x1234...5678
|
|
147
|
+
x402 link-info https://21.cash/pay/0x1234...5678
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
| Flag | Description | Default |
|
|
151
|
+
|------|-------------|---------|
|
|
152
|
+
| `<address>` | Router contract address or full payment URL (positional) | **required** |
|
|
153
|
+
| `--json` | Output as JSON | — |
|
|
154
|
+
| `-h, --help` | Show help | — |
|
|
155
|
+
|
|
156
|
+
## Configuration
|
|
157
|
+
|
|
158
|
+
Config is loaded from these locations (in order of priority):
|
|
159
|
+
|
|
160
|
+
1. Environment variables
|
|
161
|
+
2. `.env` file in current directory
|
|
162
|
+
3. `~/.x402/.env` (global config)
|
|
163
|
+
|
|
164
|
+
### Environment Variables
|
|
165
|
+
|
|
166
|
+
#### Required
|
|
167
|
+
|
|
168
|
+
| Variable | Description |
|
|
169
|
+
|----------|-------------|
|
|
170
|
+
| `EVM_PRIVATE_KEY` | Your wallet private key (0x-prefixed). Used to sign payment authorizations. |
|
|
171
|
+
|
|
172
|
+
#### Network Settings
|
|
173
|
+
|
|
174
|
+
| Variable | Description | Default |
|
|
175
|
+
|----------|-------------|---------|
|
|
176
|
+
| `X402_NETWORK` | `mainnet` (Base, chain 8453) or `testnet` (Base Sepolia, chain 84532) | `mainnet` |
|
|
177
|
+
| `X402_MAX_PAYMENT_USD` | Safety limit — payments exceeding this are rejected unless `--max` is used | `10` |
|
|
178
|
+
| `X402_FACILITATOR_URL` | Custom facilitator URL | Coinbase (mainnet) / x402.org (testnet) |
|
|
179
|
+
| `X402_SLIPPAGE_BPS` | Slippage tolerance in basis points (100 bps = 1%) | `50` |
|
|
180
|
+
| `X402_VERBOSE` | Enable verbose logging (`1` = on, `0` = off) | `0` |
|
|
181
|
+
|
|
182
|
+
#### 21.cash Integration (for link commands)
|
|
183
|
+
|
|
184
|
+
| Variable | Description |
|
|
185
|
+
|----------|-------------|
|
|
186
|
+
| `X402_LINKS_API_URL` | Base URL of x402-links-server (e.g., `https://21.cash`) |
|
|
187
|
+
| `X402_LINKS_API_KEY` | API key for programmatic link creation |
|
|
188
|
+
|
|
189
|
+
## Global Options
|
|
190
|
+
|
|
191
|
+
| Flag | Description |
|
|
192
|
+
|------|-------------|
|
|
193
|
+
| `-h, --help` | Show help for a command |
|
|
194
|
+
| `-v, --version` | Show CLI version |
|
|
195
|
+
|
|
196
|
+
## CLI Reference
|
|
197
|
+
|
|
198
|
+
| Category | Command | Description |
|
|
199
|
+
|----------|---------|-------------|
|
|
200
|
+
| **Info** | `balance` | Check wallet USDC and ETH balances |
|
|
201
|
+
| **Payments** | `pay <url>` | Pay for an x402-gated resource (verbose output) |
|
|
202
|
+
| **Payments** | `fetch <url>` | Fetch with auto-payment (pipe-friendly `--json`/`--raw`) |
|
|
203
|
+
| **Links** | `create-link` | Create a payment link to sell content (21.cash) |
|
|
204
|
+
| **Links** | `link-info <addr>` | Get info about a payment link |
|
|
205
|
+
|
|
206
|
+
## For Agents
|
|
207
|
+
|
|
208
|
+
This skill is designed for use with AI agents (Claude Code, OpenClaw, etc.). The `SKILL.md` file contains full agent-readable documentation. Agents can use the `--json` flag on all commands for structured output:
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
# Structured balance output
|
|
212
|
+
x402 balance --json
|
|
213
|
+
|
|
214
|
+
# Pipe-friendly fetch
|
|
215
|
+
x402 fetch https://api.example.com/data --json
|
|
216
|
+
|
|
217
|
+
# Raw output for further processing
|
|
218
|
+
x402 fetch https://api.example.com/data --raw
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## License
|
|
222
|
+
|
|
223
|
+
MIT
|
package/SKILL.md
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: agentic-x402
|
|
3
|
+
description: Make x402 payments to access gated APIs and content. Fetch paid resources, check wallet balance, and create payment links. Use when encountering 402 Payment Required responses or when the user wants to pay for web resources with crypto.
|
|
4
|
+
license: MIT
|
|
5
|
+
compatibility: Requires Node.js 20+, network access to x402 facilitators and EVM chains
|
|
6
|
+
metadata:
|
|
7
|
+
author: monemetrics
|
|
8
|
+
version: "0.1.0"
|
|
9
|
+
allowed-tools: Bash(x402:*) Bash(npm:*) Read
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# x402 Agent Skill
|
|
13
|
+
|
|
14
|
+
Pay for x402-gated APIs and content using USDC on Base. This skill enables agents to autonomously make crypto payments when accessing paid web resources.
|
|
15
|
+
|
|
16
|
+
## Quick Reference
|
|
17
|
+
|
|
18
|
+
| Command | Description |
|
|
19
|
+
|---------|-------------|
|
|
20
|
+
| `x402 balance` | Check USDC and ETH balances |
|
|
21
|
+
| `x402 pay <url>` | Pay for a gated resource |
|
|
22
|
+
| `x402 fetch <url>` | Fetch with auto-payment |
|
|
23
|
+
| `x402 create-link` | Create payment link (seller) |
|
|
24
|
+
| `x402 link-info <addr>` | Get payment link details |
|
|
25
|
+
|
|
26
|
+
## Setup
|
|
27
|
+
|
|
28
|
+
1. Install dependencies:
|
|
29
|
+
```bash
|
|
30
|
+
cd {baseDir} && npm install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
2. Configure wallet (create `.env` in working directory):
|
|
34
|
+
```bash
|
|
35
|
+
EVM_PRIVATE_KEY=0x...your_private_key...
|
|
36
|
+
X402_NETWORK=mainnet # or testnet
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
3. Verify setup:
|
|
40
|
+
```bash
|
|
41
|
+
x402 balance
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Paying for Resources
|
|
45
|
+
|
|
46
|
+
### When you encounter HTTP 402 Payment Required
|
|
47
|
+
|
|
48
|
+
Use `x402 pay` to make the payment and access the content:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
x402 pay https://api.example.com/paid-endpoint
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The command will:
|
|
55
|
+
1. Check payment requirements
|
|
56
|
+
2. Verify amount is within limits
|
|
57
|
+
3. Process the payment
|
|
58
|
+
4. Return the gated content
|
|
59
|
+
|
|
60
|
+
### Automatic payment with fetch
|
|
61
|
+
|
|
62
|
+
Use `x402 fetch` for seamless payment handling:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
x402 fetch https://api.example.com/data --json
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
This wraps fetch with x402 payment handling - if the resource requires payment, it's handled automatically.
|
|
69
|
+
|
|
70
|
+
### Payment limits
|
|
71
|
+
|
|
72
|
+
By default, payments are limited to $10 USD. Override with `--max`:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
x402 pay https://expensive-api.com/data --max 50
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Or set globally:
|
|
79
|
+
```bash
|
|
80
|
+
export X402_MAX_PAYMENT_USD=25
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Dry run
|
|
84
|
+
|
|
85
|
+
Preview payment without executing:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
x402 pay https://api.example.com/data --dry-run
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Creating Payment Links (Seller)
|
|
92
|
+
|
|
93
|
+
Create payment links to monetize your own content using x402-links-server:
|
|
94
|
+
|
|
95
|
+
### Setup for link creation
|
|
96
|
+
|
|
97
|
+
Add to `.env`:
|
|
98
|
+
```bash
|
|
99
|
+
X402_LINKS_API_URL=https://your-x402-links-server.com
|
|
100
|
+
X402_LINKS_API_KEY=your_api_key
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Create a link
|
|
104
|
+
|
|
105
|
+
Gate a URL:
|
|
106
|
+
```bash
|
|
107
|
+
x402 create-link --name "Premium API" --price 1.00 --url https://api.example.com/premium
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Gate text content:
|
|
111
|
+
```bash
|
|
112
|
+
x402 create-link --name "Secret" --price 0.50 --text "The secret message..."
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
With webhook notification:
|
|
116
|
+
```bash
|
|
117
|
+
x402 create-link --name "Guide" --price 5.00 --url https://mysite.com/guide --webhook https://mysite.com/payment-hook
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Get link info
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
x402 link-info 0x1234...5678
|
|
124
|
+
x402 link-info https://21.cash/pay/0x1234...5678
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Command Reference
|
|
128
|
+
|
|
129
|
+
### x402 balance
|
|
130
|
+
|
|
131
|
+
Check wallet balances.
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
x402 balance [--json] [--full]
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Options:
|
|
138
|
+
- `--json`: Output as JSON
|
|
139
|
+
- `--full`: Show full addresses
|
|
140
|
+
|
|
141
|
+
### x402 pay
|
|
142
|
+
|
|
143
|
+
Pay for an x402-gated resource.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
x402 pay <url> [options]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Options:
|
|
150
|
+
- `--method <METHOD>`: HTTP method (default: GET)
|
|
151
|
+
- `--body <JSON>`: Request body for POST/PUT
|
|
152
|
+
- `--max <USD>`: Maximum payment limit
|
|
153
|
+
- `--dry-run`: Preview without paying
|
|
154
|
+
|
|
155
|
+
### x402 fetch
|
|
156
|
+
|
|
157
|
+
Fetch with automatic payment.
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
x402 fetch <url> [options]
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Options:
|
|
164
|
+
- `--method <METHOD>`: HTTP method (default: GET)
|
|
165
|
+
- `--body <JSON>`: Request body
|
|
166
|
+
- `--json`: Output JSON only (for piping)
|
|
167
|
+
- `--raw`: Output raw response body
|
|
168
|
+
|
|
169
|
+
### x402 create-link
|
|
170
|
+
|
|
171
|
+
Create a payment link.
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
x402 create-link --name <name> --price <usd> [options]
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Required:
|
|
178
|
+
- `--name <name>`: Link name
|
|
179
|
+
- `--price <usd>`: Price in USD (e.g., "5.00")
|
|
180
|
+
|
|
181
|
+
Content (one required):
|
|
182
|
+
- `--url <url>`: URL to gate
|
|
183
|
+
- `--text <content>`: Text to gate
|
|
184
|
+
|
|
185
|
+
Options:
|
|
186
|
+
- `--desc <text>`: Description
|
|
187
|
+
- `--webhook <url>`: Webhook for notifications
|
|
188
|
+
- `--json`: Output as JSON
|
|
189
|
+
|
|
190
|
+
### x402 link-info
|
|
191
|
+
|
|
192
|
+
Get payment link details.
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
x402 link-info <router-address> [--json]
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Environment Variables
|
|
199
|
+
|
|
200
|
+
| Variable | Description | Default |
|
|
201
|
+
|----------|-------------|---------|
|
|
202
|
+
| `EVM_PRIVATE_KEY` | Wallet private key | Required |
|
|
203
|
+
| `X402_NETWORK` | `mainnet` or `testnet` | `mainnet` |
|
|
204
|
+
| `X402_MAX_PAYMENT_USD` | Max payment limit | `10` |
|
|
205
|
+
| `X402_FACILITATOR_URL` | Custom facilitator | Auto |
|
|
206
|
+
| `X402_LINKS_API_URL` | x402-links-server URL | - |
|
|
207
|
+
| `X402_LINKS_API_KEY` | API key for links | - |
|
|
208
|
+
| `X402_VERBOSE` | Enable debug logging | `0` |
|
|
209
|
+
|
|
210
|
+
## Supported Networks
|
|
211
|
+
|
|
212
|
+
| Network | Chain ID | CAIP-2 ID |
|
|
213
|
+
|---------|----------|-----------|
|
|
214
|
+
| Base Mainnet | 8453 | eip155:8453 |
|
|
215
|
+
| Base Sepolia | 84532 | eip155:84532 |
|
|
216
|
+
|
|
217
|
+
## Payment Token
|
|
218
|
+
|
|
219
|
+
All payments use **USDC** (USD Coin) on the selected network.
|
|
220
|
+
|
|
221
|
+
- Base Mainnet: `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`
|
|
222
|
+
- Base Sepolia: `0x036CbD53842c5426634e7929541eC2318f3dCF7e`
|
|
223
|
+
|
|
224
|
+
## How x402 Works
|
|
225
|
+
|
|
226
|
+
1. Client requests a resource
|
|
227
|
+
2. Server responds with `402 Payment Required` + payment details
|
|
228
|
+
3. Client signs a payment authorization (USDC transfer)
|
|
229
|
+
4. Client retries request with payment signature
|
|
230
|
+
5. Server verifies payment via facilitator
|
|
231
|
+
6. Server settles payment on-chain
|
|
232
|
+
7. Server returns the gated content
|
|
233
|
+
|
|
234
|
+
The x402 protocol is gasless for buyers - the facilitator sponsors gas fees.
|
|
235
|
+
|
|
236
|
+
## Troubleshooting
|
|
237
|
+
|
|
238
|
+
### "Missing required environment variable: EVM_PRIVATE_KEY"
|
|
239
|
+
|
|
240
|
+
Set your wallet private key:
|
|
241
|
+
```bash
|
|
242
|
+
export EVM_PRIVATE_KEY=0x...
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Or create a `.env` file in your working directory.
|
|
246
|
+
|
|
247
|
+
### "Payment exceeds max limit"
|
|
248
|
+
|
|
249
|
+
Increase the limit:
|
|
250
|
+
```bash
|
|
251
|
+
x402 pay https://... --max 50
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Low balance warnings
|
|
255
|
+
|
|
256
|
+
Fund your wallet with:
|
|
257
|
+
- **USDC** for payments
|
|
258
|
+
- **ETH** for gas (small amount, ~0.001 ETH)
|
|
259
|
+
|
|
260
|
+
### Network mismatch
|
|
261
|
+
|
|
262
|
+
Ensure your wallet has funds on the correct network:
|
|
263
|
+
- `X402_NETWORK=mainnet` → Base mainnet
|
|
264
|
+
- `X402_NETWORK=testnet` → Base Sepolia
|
|
265
|
+
|
|
266
|
+
## Security Notes
|
|
267
|
+
|
|
268
|
+
- Never share your private key
|
|
269
|
+
- Start with testnet to verify setup
|
|
270
|
+
- Set reasonable payment limits
|
|
271
|
+
- Review payment amounts before confirming
|
|
272
|
+
|
|
273
|
+
## Links
|
|
274
|
+
|
|
275
|
+
- [x402 Protocol Docs](https://docs.x402.org/)
|
|
276
|
+
- [x402 GitHub](https://github.com/coinbase/x402)
|
|
277
|
+
- [Base Network](https://base.org/)
|
package/bin/cli.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env npx tsx
|
|
2
|
+
// x402 CLI - Agent skill for x402 payments
|
|
3
|
+
|
|
4
|
+
import { spawn } from 'child_process';
|
|
5
|
+
import { resolve, dirname } from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
|
|
8
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const scriptsDir = resolve(__dirname, '../scripts');
|
|
10
|
+
|
|
11
|
+
interface Command {
|
|
12
|
+
script: string;
|
|
13
|
+
description: string;
|
|
14
|
+
category: 'info' | 'payments' | 'links';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const commands: Record<string, Command> = {
|
|
18
|
+
// Info commands
|
|
19
|
+
balance: {
|
|
20
|
+
script: 'commands/balance.ts',
|
|
21
|
+
description: 'Check wallet USDC and ETH balances',
|
|
22
|
+
category: 'info',
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
// Payment commands
|
|
26
|
+
pay: {
|
|
27
|
+
script: 'commands/pay.ts',
|
|
28
|
+
description: 'Pay for an x402-gated resource',
|
|
29
|
+
category: 'payments',
|
|
30
|
+
},
|
|
31
|
+
fetch: {
|
|
32
|
+
script: 'commands/fetch-paid.ts',
|
|
33
|
+
description: 'Fetch URL with automatic x402 payment',
|
|
34
|
+
category: 'payments',
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
// Link commands (21cash integration)
|
|
38
|
+
'create-link': {
|
|
39
|
+
script: 'commands/create-link.ts',
|
|
40
|
+
description: 'Create a payment link to sell content',
|
|
41
|
+
category: 'links',
|
|
42
|
+
},
|
|
43
|
+
'link-info': {
|
|
44
|
+
script: 'commands/link-info.ts',
|
|
45
|
+
description: 'Get info about a payment link',
|
|
46
|
+
category: 'links',
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function showHelp() {
|
|
51
|
+
console.log(`
|
|
52
|
+
x402 - Agent skill for x402 payments
|
|
53
|
+
|
|
54
|
+
Usage: x402 <command> [arguments]
|
|
55
|
+
|
|
56
|
+
Info Commands:
|
|
57
|
+
balance Check wallet USDC and ETH balances
|
|
58
|
+
|
|
59
|
+
Payment Commands:
|
|
60
|
+
pay <url> Pay for an x402-gated resource
|
|
61
|
+
fetch <url> Fetch URL with automatic x402 payment
|
|
62
|
+
|
|
63
|
+
Link Commands (21cash integration):
|
|
64
|
+
create-link Create a payment link to sell content
|
|
65
|
+
link-info <addr> Get info about a payment link
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
-h, --help Show this help
|
|
69
|
+
-v, --version Show version
|
|
70
|
+
|
|
71
|
+
Environment Variables:
|
|
72
|
+
EVM_PRIVATE_KEY Wallet private key (required)
|
|
73
|
+
X402_NETWORK Network: mainnet or testnet (default: mainnet)
|
|
74
|
+
X402_MAX_PAYMENT_USD Max payment limit in USD (default: 10)
|
|
75
|
+
X402_FACILITATOR_URL Custom facilitator URL
|
|
76
|
+
X402_LINKS_API_URL x402-links-server URL (for link commands)
|
|
77
|
+
X402_LINKS_API_KEY API key for x402-links-server
|
|
78
|
+
|
|
79
|
+
Examples:
|
|
80
|
+
x402 balance
|
|
81
|
+
x402 pay https://api.example.com/data
|
|
82
|
+
x402 fetch https://api.example.com/resource --json
|
|
83
|
+
x402 create-link --name "Guide" --price 5.00 --url https://mysite.com/guide
|
|
84
|
+
|
|
85
|
+
For command-specific help: x402 <command> --help
|
|
86
|
+
`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function showVersion() {
|
|
90
|
+
console.log('agentic-x402 v0.1.0');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function main() {
|
|
94
|
+
const args = process.argv.slice(2);
|
|
95
|
+
|
|
96
|
+
if (args.length === 0 || args[0] === '-h' || args[0] === '--help' || args[0] === 'help') {
|
|
97
|
+
showHelp();
|
|
98
|
+
process.exit(0);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (args[0] === '-v' || args[0] === '--version' || args[0] === 'version') {
|
|
102
|
+
showVersion();
|
|
103
|
+
process.exit(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const commandName = args[0];
|
|
107
|
+
const command = commands[commandName];
|
|
108
|
+
|
|
109
|
+
if (!command) {
|
|
110
|
+
console.error(`Unknown command: ${commandName}`);
|
|
111
|
+
console.error('Run "x402 --help" for available commands.');
|
|
112
|
+
process.exit(1);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const scriptPath = resolve(scriptsDir, command.script);
|
|
116
|
+
const commandArgs = args.slice(1);
|
|
117
|
+
|
|
118
|
+
// Run the script with tsx
|
|
119
|
+
const child = spawn('npx', ['tsx', scriptPath, ...commandArgs], {
|
|
120
|
+
stdio: 'inherit',
|
|
121
|
+
env: {
|
|
122
|
+
...process.env,
|
|
123
|
+
X402_CWD: process.cwd(),
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
child.on('close', (code) => {
|
|
128
|
+
process.exit(code ?? 0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
child.on('error', (err) => {
|
|
132
|
+
console.error('Failed to run command:', err.message);
|
|
133
|
+
process.exit(1);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
main();
|
package/bin/x402.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// x402 CLI wrapper - runs the TypeScript CLI via tsx
|
|
3
|
+
|
|
4
|
+
import { spawn } from 'child_process';
|
|
5
|
+
import { dirname, resolve } from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
import { existsSync } from 'fs';
|
|
8
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const cliPath = resolve(__dirname, 'cli.ts');
|
|
11
|
+
|
|
12
|
+
// Find tsx binary
|
|
13
|
+
function findTsx() {
|
|
14
|
+
// Check local node_modules first
|
|
15
|
+
const localTsx = resolve(__dirname, '../node_modules/.bin/tsx');
|
|
16
|
+
if (existsSync(localTsx)) {
|
|
17
|
+
return localTsx;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Fall back to npx
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const tsxPath = findTsx();
|
|
25
|
+
|
|
26
|
+
// Suppress experimental warnings
|
|
27
|
+
const env = {
|
|
28
|
+
...process.env,
|
|
29
|
+
NODE_NO_WARNINGS: '1',
|
|
30
|
+
X402_CWD: process.cwd(),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
let child;
|
|
34
|
+
|
|
35
|
+
if (tsxPath) {
|
|
36
|
+
// Use local tsx directly
|
|
37
|
+
child = spawn(tsxPath, [cliPath, ...process.argv.slice(2)], {
|
|
38
|
+
stdio: 'inherit',
|
|
39
|
+
env,
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
// Fall back to npx tsx
|
|
43
|
+
child = spawn('npx', ['tsx', cliPath, ...process.argv.slice(2)], {
|
|
44
|
+
stdio: 'inherit',
|
|
45
|
+
env,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
child.on('close', (code) => {
|
|
50
|
+
process.exit(code ?? 0);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
child.on('error', (err) => {
|
|
54
|
+
console.error('Failed to start x402 CLI:', err.message);
|
|
55
|
+
console.error('Make sure tsx is installed: npm install -g tsx');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
});
|