x402-fastapi-kit 0.3.0__tar.gz
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.
- x402_fastapi_kit-0.3.0/.env.example +44 -0
- x402_fastapi_kit-0.3.0/.github/workflows/publish.yml +22 -0
- x402_fastapi_kit-0.3.0/.gitignore +13 -0
- x402_fastapi_kit-0.3.0/LICENSE +21 -0
- x402_fastapi_kit-0.3.0/PKG-INFO +425 -0
- x402_fastapi_kit-0.3.0/README.md +376 -0
- x402_fastapi_kit-0.3.0/deploy/Makefile +45 -0
- x402_fastapi_kit-0.3.0/deploy/Procfile +1 -0
- x402_fastapi_kit-0.3.0/deploy/railway.toml +9 -0
- x402_fastapi_kit-0.3.0/examples/app.py +219 -0
- x402_fastapi_kit-0.3.0/pyproject.toml +80 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/.env.example +8 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/app.py +123 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/requirements.txt +3 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/__init__.py +0 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/council_tax.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/crime_stats.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/epc_ratings.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/flood_risk.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/planning.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/rental_yields.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/sold_prices.py +37 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/routes/stamp_duty.py +23 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/services/__init__.py +0 -0
- x402_fastapi_kit-0.3.0/templates/uk-property-api/tests/__init__.py +0 -0
- x402_fastapi_kit-0.3.0/tests/__init__.py +1 -0
- x402_fastapi_kit-0.3.0/tests/conftest.py +13 -0
- x402_fastapi_kit-0.3.0/tests/test_auth.py +106 -0
- x402_fastapi_kit-0.3.0/tests/test_config.py +74 -0
- x402_fastapi_kit-0.3.0/tests/test_events.py +157 -0
- x402_fastapi_kit-0.3.0/tests/test_infra.py +385 -0
- x402_fastapi_kit-0.3.0/tests/test_middleware.py +235 -0
- x402_fastapi_kit-0.3.0/tests/test_phase2_integration.py +336 -0
- x402_fastapi_kit-0.3.0/tests/test_phase3.py +375 -0
- x402_fastapi_kit-0.3.0/tests/test_rate_limit.py +110 -0
- x402_fastapi_kit-0.3.0/tests/test_utils.py +59 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/__init__.py +57 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/analytics/__init__.py +323 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/app.py +135 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/auth/__init__.py +221 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/cache.py +247 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/cli/__init__.py +1 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/cli/main.py +169 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/client/__init__.py +353 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/config/__init__.py +191 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/config/settings.py +4 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/discovery/__init__.py +208 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/events.py +180 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/mcp/__init__.py +235 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/middleware/__init__.py +22 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/middleware/payment.py +454 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/middleware/rate_limit.py +205 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/middleware/redis_backend.py +133 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/middleware/types.py +65 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/py.typed +0 -0
- x402_fastapi_kit-0.3.0/x402_fastapi_kit/utils/__init__.py +52 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# x402-fastapi-kit configuration
|
|
2
|
+
# Copy to .env and fill in your values
|
|
3
|
+
|
|
4
|
+
# REQUIRED: Your wallet address (EVM hex or Solana base58)
|
|
5
|
+
X402_PAY_TO=0xYOUR_WALLET_ADDRESS_HERE
|
|
6
|
+
|
|
7
|
+
# Network (default: Base Sepolia testnet)
|
|
8
|
+
X402_NETWORK=eip155:84532
|
|
9
|
+
|
|
10
|
+
# Facilitator URL (default: testnet facilitator)
|
|
11
|
+
X402_FACILITATOR_URL=https://x402.org/facilitator
|
|
12
|
+
|
|
13
|
+
# Default price for gated routes
|
|
14
|
+
X402_DEFAULT_PRICE=$0.01
|
|
15
|
+
|
|
16
|
+
# Server
|
|
17
|
+
X402_HOST=0.0.0.0
|
|
18
|
+
X402_PORT=8000
|
|
19
|
+
X402_DEBUG=true
|
|
20
|
+
|
|
21
|
+
# Optional: Solana address (for dual-chain support)
|
|
22
|
+
# X402_SVM_PAY_TO=YourSolanaAddress
|
|
23
|
+
|
|
24
|
+
# Optional: CDP credentials (for mainnet facilitator)
|
|
25
|
+
# X402_CDP_API_KEY_ID=
|
|
26
|
+
# X402_CDP_API_KEY_SECRET=
|
|
27
|
+
|
|
28
|
+
# ── Phase 2: Access Control ──────────────────────────────
|
|
29
|
+
|
|
30
|
+
# Rate limiting (enabled by default)
|
|
31
|
+
X402_RATE_LIMIT_ENABLED=true
|
|
32
|
+
X402_RATE_LIMIT_RPM=60
|
|
33
|
+
# Per-route limits: path=rpm,path=rpm
|
|
34
|
+
# X402_RATE_LIMIT_ROUTES=/weather=30,/forecast=10
|
|
35
|
+
|
|
36
|
+
# API key fallback auth
|
|
37
|
+
X402_API_KEYS_ENABLED=false
|
|
38
|
+
# Keys as name=secret,name=secret
|
|
39
|
+
# X402_API_KEYS=partner1=sk_live_abc123,internal=sk_agent_xyz
|
|
40
|
+
|
|
41
|
+
# Wallet allowlist
|
|
42
|
+
X402_ALLOWLIST_ENABLED=false
|
|
43
|
+
# Comma-separated addresses
|
|
44
|
+
# X402_ALLOWLIST_ADDRESSES=0xPartner1...,0xMyAgent...
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
publish:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
environment: pypi
|
|
10
|
+
permissions:
|
|
11
|
+
id-token: write
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
- name: Install build tools
|
|
18
|
+
run: pip install build
|
|
19
|
+
- name: Build package
|
|
20
|
+
run: python -m build
|
|
21
|
+
- name: Publish to PyPI
|
|
22
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 x402-fastapi-kit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: x402-fastapi-kit
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Production-ready starter kit for building x402 payment-gated FastAPI services
|
|
5
|
+
Project-URL: Homepage, https://github.com/your-org/x402-fastapi-kit
|
|
6
|
+
Project-URL: Documentation, https://github.com/your-org/x402-fastapi-kit#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/your-org/x402-fastapi-kit
|
|
8
|
+
Project-URL: Issues, https://github.com/your-org/x402-fastapi-kit/issues
|
|
9
|
+
Author: x402-fastapi-kit contributors
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: api,crypto,fastapi,micropayments,monetization,payments,usdc,x402
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: FastAPI
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
23
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: fastapi>=0.100.0
|
|
27
|
+
Requires-Dist: httpx>=0.25.0
|
|
28
|
+
Requires-Dist: pydantic-settings>=2.0
|
|
29
|
+
Requires-Dist: pydantic>=2.0
|
|
30
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
31
|
+
Requires-Dist: uvicorn>=0.23.0
|
|
32
|
+
Provides-Extra: all
|
|
33
|
+
Requires-Dist: redis>=5.0.0; extra == 'all'
|
|
34
|
+
Requires-Dist: x402[all]; extra == 'all'
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: httpx>=0.25.0; extra == 'dev'
|
|
37
|
+
Requires-Dist: mypy>=1.5; extra == 'dev'
|
|
38
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
39
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
40
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
41
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
42
|
+
Provides-Extra: evm
|
|
43
|
+
Requires-Dist: x402[evm,fastapi]; extra == 'evm'
|
|
44
|
+
Provides-Extra: redis
|
|
45
|
+
Requires-Dist: redis>=5.0.0; extra == 'redis'
|
|
46
|
+
Provides-Extra: svm
|
|
47
|
+
Requires-Dist: x402[fastapi,svm]; extra == 'svm'
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
|
|
50
|
+
# x402-fastapi-kit
|
|
51
|
+
|
|
52
|
+
**Production-ready starter kit for building x402 payment-gated FastAPI services.**
|
|
53
|
+
|
|
54
|
+
Turn any FastAPI endpoint into a paid API with one decorator. Built on [Coinbase's x402 protocol](https://x402.org) — the open standard for internet-native payments.
|
|
55
|
+
|
|
56
|
+
## Features
|
|
57
|
+
|
|
58
|
+
- **One-line payment gating** — `@require_payment(price="$0.01")` on any route
|
|
59
|
+
- **`PaymentApp`** — batteries-included FastAPI subclass with middleware pre-wired
|
|
60
|
+
- **Environment-driven config** — Pydantic Settings with `.env` support
|
|
61
|
+
- **Multi-chain** — EVM (Base, Ethereum, Polygon) and Solana out of the box
|
|
62
|
+
- **Introspection** — built-in `/x402/health` and `/x402/routes` endpoints
|
|
63
|
+
- **CLI scaffolding** — `x402kit init my-api` generates a ready-to-run project
|
|
64
|
+
- **Spec-compliant** — x402 v2 responses, `PAYMENT-REQUIRED` header, facilitator verification
|
|
65
|
+
- **API key fallback** — `X-API-Key` header auth for traditional clients
|
|
66
|
+
- **Wallet allowlist** — free-pass access for partners and your own AI agents
|
|
67
|
+
- **Rate limiting** — sliding-window rate limiter with per-route limits and `429` responses
|
|
68
|
+
- **Event hooks** — `@bus.on("payment.verified")` callbacks for logging, analytics, alerts
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
### 1. Install
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install x402-fastapi-kit
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 2. Set your wallet
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
export X402_PAY_TO=0xYourWalletAddress
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 3. Write your app
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from x402_fastapi_kit import PaymentApp, require_payment
|
|
88
|
+
|
|
89
|
+
app = PaymentApp()
|
|
90
|
+
|
|
91
|
+
@app.get("/free")
|
|
92
|
+
async def free():
|
|
93
|
+
return {"msg": "hello, no payment needed"}
|
|
94
|
+
|
|
95
|
+
@app.get("/premium")
|
|
96
|
+
@require_payment(price="$0.05", description="Premium data")
|
|
97
|
+
async def premium():
|
|
98
|
+
return {"data": "this costs $0.05 per request"}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 4. Run
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
uvicorn app:app --reload
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Try it:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
curl http://localhost:8000/free # → 200 OK
|
|
111
|
+
curl http://localhost:8000/premium # → 402 Payment Required
|
|
112
|
+
curl http://localhost:8000/x402/routes # → list of gated routes
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Configuration
|
|
116
|
+
|
|
117
|
+
All settings are loaded from environment variables (prefix `X402_`) or a `.env` file:
|
|
118
|
+
|
|
119
|
+
| Variable | Default | Description |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| `X402_PAY_TO` | *(required)* | Wallet address (EVM or Solana) |
|
|
122
|
+
| `X402_NETWORK` | `eip155:84532` | CAIP-2 network ID |
|
|
123
|
+
| `X402_FACILITATOR_URL` | `https://x402.org/facilitator` | Facilitator service URL |
|
|
124
|
+
| `X402_DEFAULT_PRICE` | `$0.01` | Default price for gated routes |
|
|
125
|
+
| `X402_SCHEME` | `exact` | Payment scheme |
|
|
126
|
+
| `X402_MAX_TIMEOUT_SECONDS` | `300` | Payment expiry |
|
|
127
|
+
| `X402_HOST` | `0.0.0.0` | Server bind host |
|
|
128
|
+
| `X402_PORT` | `8000` | Server bind port |
|
|
129
|
+
| `X402_DEBUG` | `false` | Debug mode |
|
|
130
|
+
| `X402_SVM_PAY_TO` | *(optional)* | Solana address for dual-chain |
|
|
131
|
+
| `X402_RATE_LIMIT_ENABLED` | `true` | Enable rate limiting |
|
|
132
|
+
| `X402_RATE_LIMIT_RPM` | `60` | Default requests per minute per client |
|
|
133
|
+
| `X402_RATE_LIMIT_ROUTES` | *(empty)* | Per-route limits: `/weather=30,/forecast=10` |
|
|
134
|
+
| `X402_API_KEYS_ENABLED` | `false` | Enable API key fallback auth |
|
|
135
|
+
| `X402_API_KEYS` | *(empty)* | Keys as `name=secret,name=secret` |
|
|
136
|
+
| `X402_ALLOWLIST_ENABLED` | `false` | Enable wallet allowlist |
|
|
137
|
+
| `X402_ALLOWLIST_ADDRESSES` | *(empty)* | Comma-separated wallet addresses |
|
|
138
|
+
|
|
139
|
+
### Supported Networks
|
|
140
|
+
|
|
141
|
+
| Network | CAIP-2 ID |
|
|
142
|
+
|---|---|
|
|
143
|
+
| Base Sepolia (testnet) | `eip155:84532` |
|
|
144
|
+
| Base Mainnet | `eip155:8453` |
|
|
145
|
+
| Ethereum Mainnet | `eip155:1` |
|
|
146
|
+
| Polygon Mainnet | `eip155:137` |
|
|
147
|
+
| Solana Devnet | `solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1` |
|
|
148
|
+
| Solana Mainnet | `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp` |
|
|
149
|
+
|
|
150
|
+
## Access Control
|
|
151
|
+
|
|
152
|
+
The middleware runs a 4-step pipeline on every gated request. The first match wins:
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
Request → [1] Wallet Allowlist → [2] API Key → [3] Rate Limit → [4] x402 Payment
|
|
156
|
+
(bypass) (bypass) (429) (402/200)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### API Key Fallback
|
|
160
|
+
|
|
161
|
+
Support traditional clients alongside x402 payers:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from x402_fastapi_kit import PaymentApp, APIKeyManager, require_payment
|
|
165
|
+
|
|
166
|
+
keys = APIKeyManager()
|
|
167
|
+
keys.add_key("partner-1", "sk_live_abc123")
|
|
168
|
+
keys.add_key("internal", APIKeyManager.generate_key())
|
|
169
|
+
|
|
170
|
+
app = PaymentApp(api_key_manager=keys)
|
|
171
|
+
|
|
172
|
+
@app.get("/data")
|
|
173
|
+
@require_payment(price="$0.05")
|
|
174
|
+
async def data():
|
|
175
|
+
return {"result": "works with X-PAYMENT or X-API-Key header"}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
curl -H "X-API-Key: sk_live_abc123" http://localhost:8000/data # 200 — bypassed
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Wallet Allowlist
|
|
183
|
+
|
|
184
|
+
Give free access to partners, your own agents, or test wallets:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
from x402_fastapi_kit import PaymentApp, WalletAllowlist
|
|
188
|
+
|
|
189
|
+
allowlist = WalletAllowlist()
|
|
190
|
+
allowlist.add("0xPartnerWallet...", label="acme-corp")
|
|
191
|
+
allowlist.add("0xMyAgentWallet...", label="internal-bot")
|
|
192
|
+
|
|
193
|
+
app = PaymentApp(wallet_allowlist=allowlist)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
curl -H "X-WALLET: 0xPartnerWallet..." http://localhost:8000/data # 200 — free
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Rate Limiting
|
|
201
|
+
|
|
202
|
+
Protect your facilitator from abuse with per-client sliding-window limits:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from x402_fastapi_kit import PaymentApp, RateLimiter
|
|
206
|
+
|
|
207
|
+
limiter = RateLimiter(
|
|
208
|
+
requests_per_minute=60,
|
|
209
|
+
route_limits={"/expensive": 10, "/cheap": 200},
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
app = PaymentApp(rate_limiter=limiter)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Allowlisted wallets and API key holders bypass rate limits (they're checked first).
|
|
216
|
+
|
|
217
|
+
## Event Hooks
|
|
218
|
+
|
|
219
|
+
Get notified at every decision point in the pipeline:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
from x402_fastapi_kit import PaymentApp, EventBus, EventType
|
|
223
|
+
|
|
224
|
+
bus = EventBus()
|
|
225
|
+
|
|
226
|
+
@bus.on(EventType.PAYMENT_VERIFIED)
|
|
227
|
+
async def on_paid(event):
|
|
228
|
+
print(f"💰 {event.route} — tx: {event.tx_hash}")
|
|
229
|
+
|
|
230
|
+
@bus.on(EventType.RATE_LIMITED)
|
|
231
|
+
async def on_throttled(event):
|
|
232
|
+
alert(f"⚠️ Client {event.client_id} rate limited on {event.route}")
|
|
233
|
+
|
|
234
|
+
# Wildcard — see everything
|
|
235
|
+
bus.on_all(lambda e: metrics.emit(e.event_type, e.route))
|
|
236
|
+
|
|
237
|
+
app = PaymentApp(event_bus=bus)
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Event types: `payment.verified`, `payment.rejected`, `request.gated`, `auth.api_key`, `auth.allowlist`, `rate.limited`, `facilitator.error`
|
|
241
|
+
|
|
242
|
+
## Service Discovery
|
|
243
|
+
|
|
244
|
+
Make your API findable by AI agents and the x402 ecosystem:
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
from x402_fastapi_kit import PaymentApp, ServiceManifest
|
|
248
|
+
|
|
249
|
+
manifest = ServiceManifest(
|
|
250
|
+
name="Weather API",
|
|
251
|
+
description="Real-time weather data",
|
|
252
|
+
tags=["weather", "data", "climate"],
|
|
253
|
+
homepage="https://weather.example.com",
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
app = PaymentApp(manifest=manifest)
|
|
257
|
+
# Now serves /.well-known/x402 automatically
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Enrich your OpenAPI schema so tools can read pricing from `/openapi.json`:
|
|
261
|
+
|
|
262
|
+
```python
|
|
263
|
+
app.enrich_openapi_schema()
|
|
264
|
+
# Each gated endpoint now has x-x402 metadata in the spec
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Register with the Bazaar discovery layer:
|
|
268
|
+
|
|
269
|
+
```python
|
|
270
|
+
from x402_fastapi_kit import register_with_bazaar
|
|
271
|
+
|
|
272
|
+
await register_with_bazaar("https://weather.example.com", settings, manifest)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Client SDK (Buyer Side)
|
|
276
|
+
|
|
277
|
+
Make x402 payments from Python — for AI agents, scripts, and services that *consume* paid APIs:
|
|
278
|
+
|
|
279
|
+
```python
|
|
280
|
+
from x402_fastapi_kit import X402Client, PaymentPolicy, DryRunSigner
|
|
281
|
+
|
|
282
|
+
# Dry-run mode (no real wallet, for testing)
|
|
283
|
+
client = X402Client(signer=DryRunSigner())
|
|
284
|
+
|
|
285
|
+
# With spending controls
|
|
286
|
+
client = X402Client(
|
|
287
|
+
signer=my_wallet_signer,
|
|
288
|
+
policy=PaymentPolicy(
|
|
289
|
+
max_per_request="1.00",
|
|
290
|
+
max_per_session="10.00",
|
|
291
|
+
preferred_network="eip155:8453",
|
|
292
|
+
),
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
# Transparent 402 → pay → retry
|
|
296
|
+
resp = await client.get("https://api.example.com/weather")
|
|
297
|
+
print(resp.json())
|
|
298
|
+
|
|
299
|
+
# Check spending
|
|
300
|
+
print(f"Total spent: ${client.total_spent}")
|
|
301
|
+
print(f"Receipts: {client.receipts}")
|
|
302
|
+
|
|
303
|
+
# Discover a service
|
|
304
|
+
manifest = await client.discover("https://api.example.com")
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Implement `PaymentSigner` for real wallets:
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
from x402_fastapi_kit import PaymentSigner
|
|
311
|
+
|
|
312
|
+
class MyWalletSigner(PaymentSigner):
|
|
313
|
+
async def sign(self, requirements: dict) -> str:
|
|
314
|
+
# Sign with your wallet (viem, web3.py, solders, etc.)
|
|
315
|
+
return signed_payload_string
|
|
316
|
+
|
|
317
|
+
@property
|
|
318
|
+
def address(self) -> str:
|
|
319
|
+
return "0xYourAddress"
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Analytics Dashboard
|
|
323
|
+
|
|
324
|
+
Live revenue and request monitoring, powered by the EventBus:
|
|
325
|
+
|
|
326
|
+
```python
|
|
327
|
+
from x402_fastapi_kit import PaymentApp, EventBus
|
|
328
|
+
|
|
329
|
+
bus = EventBus()
|
|
330
|
+
app = PaymentApp(event_bus=bus, analytics=True)
|
|
331
|
+
# Serves:
|
|
332
|
+
# /x402/stats → JSON snapshot (revenue, routes, auth breakdown)
|
|
333
|
+
# /x402/dashboard → live HTML dashboard (auto-refreshes every 5s)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
The dashboard shows: total revenue, payment count, per-route breakdown, auth method distribution, rate limit events, and a live event feed.
|
|
337
|
+
|
|
338
|
+
## CLI
|
|
339
|
+
|
|
340
|
+
```bash
|
|
341
|
+
# Scaffold a new project
|
|
342
|
+
x402kit init my-paid-api
|
|
343
|
+
|
|
344
|
+
# Show version and networks
|
|
345
|
+
x402kit info
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## API Reference
|
|
349
|
+
|
|
350
|
+
### `PaymentApp`
|
|
351
|
+
|
|
352
|
+
A `FastAPI` subclass that auto-wires the payment middleware, discovers `@require_payment` routes, and adds introspection endpoints.
|
|
353
|
+
|
|
354
|
+
```python
|
|
355
|
+
from x402_fastapi_kit import PaymentApp
|
|
356
|
+
|
|
357
|
+
app = PaymentApp(
|
|
358
|
+
title="My API",
|
|
359
|
+
settings=X402Settings(pay_to="0x..."),
|
|
360
|
+
)
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### `@require_payment`
|
|
364
|
+
|
|
365
|
+
Decorator to gate a route. Apply **after** the route decorator:
|
|
366
|
+
|
|
367
|
+
```python
|
|
368
|
+
@app.get("/data")
|
|
369
|
+
@require_payment(
|
|
370
|
+
price="$0.05", # required
|
|
371
|
+
description="My data", # shown in 402 response
|
|
372
|
+
network="eip155:8453", # override global network
|
|
373
|
+
scheme="exact", # payment scheme
|
|
374
|
+
pay_to="0xOther", # override global pay_to
|
|
375
|
+
)
|
|
376
|
+
async def get_data():
|
|
377
|
+
return {"data": "paid content"}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### `PaymentGate`
|
|
381
|
+
|
|
382
|
+
ASGI middleware (used internally by `PaymentApp`, or add manually):
|
|
383
|
+
|
|
384
|
+
```python
|
|
385
|
+
from fastapi import FastAPI
|
|
386
|
+
from x402_fastapi_kit import PaymentGate, X402Settings
|
|
387
|
+
|
|
388
|
+
app = FastAPI()
|
|
389
|
+
app.add_middleware(PaymentGate, settings=X402Settings())
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## How It Works
|
|
393
|
+
|
|
394
|
+
```
|
|
395
|
+
Client Your FastAPI Facilitator
|
|
396
|
+
│ │ │
|
|
397
|
+
│ GET /premium │ │
|
|
398
|
+
│──────────────────────────▶│ │
|
|
399
|
+
│ │ (no X-PAYMENT header) │
|
|
400
|
+
│ 402 + payment details │ │
|
|
401
|
+
│◀──────────────────────────│ │
|
|
402
|
+
│ │ │
|
|
403
|
+
│ GET /premium │ │
|
|
404
|
+
│ X-PAYMENT: <signed> │ │
|
|
405
|
+
│──────────────────────────▶│ │
|
|
406
|
+
│ │ POST /verify │
|
|
407
|
+
│ │─────────────────────────▶│
|
|
408
|
+
│ │ { valid: true } │
|
|
409
|
+
│ │◀─────────────────────────│
|
|
410
|
+
│ 200 + data │ │
|
|
411
|
+
│◀──────────────────────────│ │
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
## Development
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
git clone https://github.com/your-org/x402-fastapi-kit
|
|
418
|
+
cd x402-fastapi-kit
|
|
419
|
+
pip install -e ".[dev]"
|
|
420
|
+
pytest
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
## License
|
|
424
|
+
|
|
425
|
+
MIT
|