atp-protocol 1.2.0__py3-none-any.whl → 1.3.0__py3-none-any.whl
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.
- atp/__init__.py +9 -1
- atp/client.py +618 -0
- atp/config.py +5 -0
- atp/encryption.py +155 -0
- atp/middleware.py +442 -103
- atp/schemas.py +186 -0
- atp/settlement_client.py +608 -52
- atp_protocol-1.3.0.dist-info/METADATA +590 -0
- atp_protocol-1.3.0.dist-info/RECORD +11 -0
- atp_protocol-1.2.0.dist-info/METADATA +0 -401
- atp_protocol-1.2.0.dist-info/RECORD +0 -9
- {atp_protocol-1.2.0.dist-info → atp_protocol-1.3.0.dist-info}/LICENSE +0 -0
- {atp_protocol-1.2.0.dist-info → atp_protocol-1.3.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: atp-protocol
|
|
3
|
+
Version: 1.3.0
|
|
4
|
+
Summary: ATP Protocol - Agentic Trade Protocol. The easiest way to enable agent-to-agent payments powered by Solana.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: atp,atp-protocol,solana,blockchain,cryptocurrency,payment-gated,agent-to-agent,agent payments,agent execution,payment settlement,solana payments,usdc,sol,fastapi,agent api,payment gateway,settlement service,agent marketplace,decentralized payments,web3,crypto payments,agent infrastructure,payment middleware,automated settlement
|
|
7
|
+
Author: The Swarm Corporation
|
|
8
|
+
Requires-Python: >=3.10,<4.0
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Dist: cryptography
|
|
21
|
+
Requires-Dist: fastapi
|
|
22
|
+
Requires-Dist: httpx
|
|
23
|
+
Requires-Dist: loguru
|
|
24
|
+
Requires-Dist: pydantic
|
|
25
|
+
Requires-Dist: python-dotenv
|
|
26
|
+
Requires-Dist: setuptools
|
|
27
|
+
Requires-Dist: starlette
|
|
28
|
+
Requires-Dist: swarms
|
|
29
|
+
Project-URL: Documentation, https://github.com/The-Swarm-Corporation/ATP-Protocol
|
|
30
|
+
Project-URL: Homepage, https://github.com/The-Swarm-Corporation/ATP-Protocol
|
|
31
|
+
Project-URL: Repository, https://github.com/The-Swarm-Corporation/ATP-Protocol
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# ATP Protocol
|
|
35
|
+
|
|
36
|
+
**ATP (Agent Transaction Protocol)** enables automatic payment processing for AI agent APIs on Solana. Add billing to any FastAPI endpoint with a few lines of code.
|
|
37
|
+
|
|
38
|
+
> **Part of the [Swarms.ai](https://swarms.ai) ecosystem** - ATP Protocol is built for the [Swarms](https://github.com/kyegomez/swarms) multi-agent orchestration framework, providing seamless payment infrastructure for agent-to-agent transactions.
|
|
39
|
+
|
|
40
|
+
## What It Does
|
|
41
|
+
|
|
42
|
+
ATP Protocol makes it easy to charge for API usage:
|
|
43
|
+
|
|
44
|
+
- **Automatic billing** - Payment is processed automatically after each request
|
|
45
|
+
- **Solana payments** - Uses SOL or USDC for fast, cheap transactions
|
|
46
|
+
- **Token-based pricing** - Charges based on input/output token usage
|
|
47
|
+
- **Response encryption** - Agent responses are encrypted until payment is confirmed
|
|
48
|
+
- **Payment verification** - Responses are only decrypted after successful payment
|
|
49
|
+
- **Zero infrastructure** - No payment processors, no databases, just Solana
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
First, install the package:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install atp-protocol
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
Then add automatic billing to your FastAPI app:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from fastapi import FastAPI
|
|
65
|
+
from atp.middleware import ATPSettlementMiddleware
|
|
66
|
+
from atp.schemas import PaymentToken
|
|
67
|
+
|
|
68
|
+
app = FastAPI()
|
|
69
|
+
|
|
70
|
+
# Add the middleware
|
|
71
|
+
app.add_middleware(
|
|
72
|
+
ATPSettlementMiddleware,
|
|
73
|
+
allowed_endpoints=["/v1/chat", "/v1/completions"],
|
|
74
|
+
input_cost_per_million_usd=10.0, # $10 per million input tokens
|
|
75
|
+
output_cost_per_million_usd=30.0, # $30 per million output tokens
|
|
76
|
+
recipient_pubkey="YourSolanaWalletHere", # Your wallet receives 95%
|
|
77
|
+
payment_token=PaymentToken.SOL,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Your endpoint just needs to return usage data
|
|
81
|
+
@app.post("/v1/chat")
|
|
82
|
+
async def chat(request: dict):
|
|
83
|
+
return {
|
|
84
|
+
"output": "Response here",
|
|
85
|
+
"usage": {
|
|
86
|
+
"input_tokens": 150,
|
|
87
|
+
"output_tokens": 50,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Client makes request:**
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
curl -X POST http://localhost:8000/v1/chat \
|
|
96
|
+
-H "x-wallet-private-key: [1,2,3,...]" \
|
|
97
|
+
-d '{"message": "Hello!"}'
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**Response includes payment details (decrypted after payment):**
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"output": "Response here",
|
|
105
|
+
"usage": {"input_tokens": 150, "output_tokens": 50},
|
|
106
|
+
"atp_settlement": {
|
|
107
|
+
"status": "paid",
|
|
108
|
+
"transaction_signature": "5j7s8K9...",
|
|
109
|
+
"payment": {
|
|
110
|
+
"total_amount_sol": 0.0003,
|
|
111
|
+
"recipient": {"amount_sol": 0.000285},
|
|
112
|
+
"treasury": {"amount_sol": 0.000015}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**If payment fails, response remains encrypted:**
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"output": "encrypted_data_here...",
|
|
123
|
+
"usage": {"input_tokens": 150, "output_tokens": 50},
|
|
124
|
+
"atp_settlement": {
|
|
125
|
+
"error": "Settlement failed",
|
|
126
|
+
"message": "Insufficient funds"
|
|
127
|
+
},
|
|
128
|
+
"atp_settlement_status": "failed",
|
|
129
|
+
"atp_message": "Agent response is encrypted. Payment required to decrypt. Please provide a valid wallet private key and ensure payment succeeds."
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
That's it! Payment is automatically processed on Solana, and responses are protected until payment is confirmed.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## How It Works
|
|
138
|
+
|
|
139
|
+
The ATP Protocol uses a **Settlement Service** to handle all payment logic. Here's the flow:
|
|
140
|
+
|
|
141
|
+
```mermaid
|
|
142
|
+
sequenceDiagram
|
|
143
|
+
autonumber
|
|
144
|
+
participant C as Client
|
|
145
|
+
participant M as Middleware
|
|
146
|
+
participant E as Your Endpoint
|
|
147
|
+
participant SS as Settlement Service
|
|
148
|
+
participant S as Solana
|
|
149
|
+
|
|
150
|
+
C->>M: POST /v1/chat<br/>(x-wallet-private-key header)
|
|
151
|
+
M->>E: Forward request
|
|
152
|
+
E-->>M: Response + usage data
|
|
153
|
+
M->>M: Extract token counts<br/>(auto-detects format)
|
|
154
|
+
M->>M: Encrypt agent response<br/>(protect output)
|
|
155
|
+
M->>SS: Calculate payment<br/>(usage → USD → SOL/USDC)
|
|
156
|
+
SS->>SS: Split payment<br/>(95% recipient, 5% treasury)
|
|
157
|
+
SS->>S: Send payment transaction
|
|
158
|
+
S-->>SS: Transaction signature
|
|
159
|
+
SS-->>M: Settlement details
|
|
160
|
+
M->>M: Verify payment status
|
|
161
|
+
alt Payment succeeded
|
|
162
|
+
M->>M: Decrypt response
|
|
163
|
+
else Payment failed
|
|
164
|
+
M->>M: Keep response encrypted
|
|
165
|
+
end
|
|
166
|
+
M->>M: Add settlement info<br/>to response
|
|
167
|
+
M-->>C: Response + atp_settlement
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Step-by-Step
|
|
171
|
+
|
|
172
|
+
1. **Client sends request** with wallet private key in header
|
|
173
|
+
2. **Middleware intercepts** and forwards to your endpoint
|
|
174
|
+
3. **Your endpoint executes** and returns response with usage data
|
|
175
|
+
4. **Middleware extracts** token counts (supports OpenAI, Anthropic, Google formats)
|
|
176
|
+
5. **Middleware encrypts** agent response to protect output until payment is confirmed
|
|
177
|
+
6. **Settlement Service calculates** cost from token usage
|
|
178
|
+
7. **Settlement Service splits** payment: 95% to you, 5% to Swarms Treasury
|
|
179
|
+
8. **Settlement Service sends** Solana transaction
|
|
180
|
+
9. **Middleware verifies** payment status from settlement service
|
|
181
|
+
10. **If payment succeeded**: Middleware decrypts response and returns it
|
|
182
|
+
11. **If payment failed**: Response remains encrypted with error message
|
|
183
|
+
12. **Middleware adds** settlement info to response
|
|
184
|
+
|
|
185
|
+
### Architecture
|
|
186
|
+
|
|
187
|
+
```mermaid
|
|
188
|
+
flowchart TB
|
|
189
|
+
C[Client] -->|Request + Wallet Key| M[ATP Middleware]
|
|
190
|
+
M -->|Forward| E[Your Endpoint]
|
|
191
|
+
E -->|Response + Usage| M
|
|
192
|
+
M -->|Calculate Payment| SS[Settlement Service]
|
|
193
|
+
SS -->|Send Transaction| S[Solana]
|
|
194
|
+
S -->|Transaction Signature| SS
|
|
195
|
+
SS -->|Settlement Details| M
|
|
196
|
+
M -->|Response + Payment Info| C
|
|
197
|
+
|
|
198
|
+
SS -->|95%| R[Your Wallet]
|
|
199
|
+
SS -->|5%| T[Swarms Treasury]
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
The **Settlement Service** is a centralized service that handles:
|
|
203
|
+
|
|
204
|
+
- Parsing usage data from various API formats
|
|
205
|
+
- Calculating payment amounts
|
|
206
|
+
- Executing Solana transactions
|
|
207
|
+
- Verifying payments
|
|
208
|
+
|
|
209
|
+
The official facilitator at **`https://facilitator.swarms.world`** is ultra-fast and powered by Rust, ensuring low-latency payment processing. This keeps your middleware simple and ensures all settlement logic is immutable and centralized.
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Configuration
|
|
214
|
+
|
|
215
|
+
### Required Parameters
|
|
216
|
+
|
|
217
|
+
| Parameter | Description |
|
|
218
|
+
| --------- | ----------- |
|
|
219
|
+
| `allowed_endpoints` | List of endpoint paths to apply settlement (e.g., `["/v1/chat"]`) |
|
|
220
|
+
| `input_cost_per_million_usd` | Cost per million input tokens in USD |
|
|
221
|
+
| `output_cost_per_million_usd` | Cost per million output tokens in USD |
|
|
222
|
+
| `recipient_pubkey` | Your Solana wallet address (receives 95% of payment) |
|
|
223
|
+
|
|
224
|
+
### Optional Parameters
|
|
225
|
+
|
|
226
|
+
| Parameter | Default | Description |
|
|
227
|
+
| --------- | ------- | ----------- |
|
|
228
|
+
| `wallet_private_key_header` | `x-wallet-private-key` | HTTP header name for wallet key |
|
|
229
|
+
| `payment_token` | `PaymentToken.SOL` | `PaymentToken.SOL` or `PaymentToken.USDC` |
|
|
230
|
+
| `require_wallet` | `True` | Require wallet key or skip settlement when missing |
|
|
231
|
+
| `settlement_service_url` | From `ATP_SETTLEMENT_URL` env | Settlement service URL |
|
|
232
|
+
| `settlement_timeout` | From `ATP_SETTLEMENT_TIMEOUT` env or `300.0` | Timeout in seconds for settlement requests (default: 5 minutes) |
|
|
233
|
+
| `fail_on_settlement_error` | `False` | If `True`, raises HTTPException on settlement failure; if `False`, returns encrypted response with error |
|
|
234
|
+
| `skip_preflight` | `False` | Skip Solana transaction preflight simulation |
|
|
235
|
+
| `commitment` | `"confirmed"` | Solana commitment level (`processed`\|`confirmed`\|`finalized`) |
|
|
236
|
+
|
|
237
|
+
### Environment Variables
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# Settlement Service URL (default: https://facilitator.swarms.world)
|
|
241
|
+
# The official facilitator is ultra-fast and powered by Rust
|
|
242
|
+
ATP_SETTLEMENT_URL="https://facilitator.swarms.world"
|
|
243
|
+
|
|
244
|
+
# Settlement Service Timeout (default: 300.0 seconds / 5 minutes)
|
|
245
|
+
# Settlement operations may take longer due to blockchain confirmation times
|
|
246
|
+
# Increase this value if you experience timeout errors even when payments succeed
|
|
247
|
+
ATP_SETTLEMENT_TIMEOUT="300.0"
|
|
248
|
+
|
|
249
|
+
# Solana RPC URL (default: https://api.mainnet-beta.solana.com)
|
|
250
|
+
SOLANA_RPC_URL="https://api.mainnet-beta.solana.com"
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Payment Calculation
|
|
256
|
+
|
|
257
|
+
The middleware calculates cost from token usage:
|
|
258
|
+
|
|
259
|
+
```text
|
|
260
|
+
usd_cost = (input_tokens / 1,000,000 × input_rate) + (output_tokens / 1,000,000 × output_rate)
|
|
261
|
+
token_amount = usd_cost / token_price_usd
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
**Payment Split:**
|
|
265
|
+
|
|
266
|
+
- **95%** → Your wallet (`recipient_pubkey`)
|
|
267
|
+
- **5%** → Swarms Treasury (processing fee)
|
|
268
|
+
|
|
269
|
+
The fee is **deducted from the total** (not added on top).
|
|
270
|
+
|
|
271
|
+
### Example
|
|
272
|
+
|
|
273
|
+
If a request uses 1,000 input tokens and 500 output tokens:
|
|
274
|
+
|
|
275
|
+
- Input cost: `1,000 / 1,000,000 × $10 = $0.01`
|
|
276
|
+
- Output cost: `500 / 1,000,000 × $30 = $0.015`
|
|
277
|
+
- **Total: $0.025 USD**
|
|
278
|
+
|
|
279
|
+
At SOL price of $100:
|
|
280
|
+
|
|
281
|
+
- Payment: `$0.025 / $100 = 0.00025 SOL`
|
|
282
|
+
- You receive: `0.00025 × 0.95 = 0.0002375 SOL`
|
|
283
|
+
- Treasury receives: `0.00025 × 0.05 = 0.0000125 SOL`
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
287
|
+
## Supported Usage Formats
|
|
288
|
+
|
|
289
|
+
The middleware automatically detects usage from common API formats:
|
|
290
|
+
|
|
291
|
+
- **OpenAI**: `prompt_tokens`, `completion_tokens`, `total_tokens`
|
|
292
|
+
- **Anthropic**: `input_tokens`, `output_tokens`, `total_tokens`
|
|
293
|
+
- **Google/Gemini**: `promptTokenCount`, `candidatesTokenCount`, `totalTokenCount`
|
|
294
|
+
- **Generic**: `input_tokens`, `output_tokens`, `total_tokens`
|
|
295
|
+
- **Nested**: `usage.*`, `meta.usage`, `statistics.*`
|
|
296
|
+
|
|
297
|
+
Your endpoint can return usage in any of these formats - the middleware will find it.
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## Complete Example
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
from fastapi import FastAPI, JSONResponse
|
|
305
|
+
from atp.middleware import ATPSettlementMiddleware
|
|
306
|
+
from atp.schemas import PaymentToken
|
|
307
|
+
|
|
308
|
+
app = FastAPI()
|
|
309
|
+
|
|
310
|
+
# Configure middleware
|
|
311
|
+
app.add_middleware(
|
|
312
|
+
ATPSettlementMiddleware,
|
|
313
|
+
allowed_endpoints=["/v1/chat"],
|
|
314
|
+
input_cost_per_million_usd=10.0,
|
|
315
|
+
output_cost_per_million_usd=30.0,
|
|
316
|
+
recipient_pubkey="YourSolanaWalletHere",
|
|
317
|
+
payment_token=PaymentToken.SOL,
|
|
318
|
+
require_wallet=True,
|
|
319
|
+
fail_on_settlement_error=False, # Return encrypted response on error
|
|
320
|
+
settlement_timeout=300.0, # 5 minutes timeout
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
@app.post("/v1/chat")
|
|
324
|
+
async def chat(request: dict):
|
|
325
|
+
# Your business logic here
|
|
326
|
+
message = request.get("message", "")
|
|
327
|
+
|
|
328
|
+
# Simulate API call that returns usage
|
|
329
|
+
response_data = {
|
|
330
|
+
"output": f"You said: {message}",
|
|
331
|
+
"usage": {
|
|
332
|
+
"input_tokens": len(message.split()) * 2, # Rough estimate
|
|
333
|
+
"output_tokens": 50,
|
|
334
|
+
"total_tokens": len(message.split()) * 2 + 50,
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return JSONResponse(content=response_data)
|
|
339
|
+
|
|
340
|
+
if __name__ == "__main__":
|
|
341
|
+
import uvicorn
|
|
342
|
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
**Run it:**
|
|
346
|
+
|
|
347
|
+
```bash
|
|
348
|
+
python app.py
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**Test it:**
|
|
352
|
+
|
|
353
|
+
```bash
|
|
354
|
+
curl -X POST http://localhost:8000/v1/chat \
|
|
355
|
+
-H "Content-Type: application/json" \
|
|
356
|
+
-H "x-wallet-private-key: [1,2,3,...]" \
|
|
357
|
+
-d '{"message": "Hello, world!"}'
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Swarms Framework Integration
|
|
361
|
+
|
|
362
|
+
For a complete example showing how to integrate ATP Protocol with [Swarms](https://github.com/kyegomez/swarms) agents, see:
|
|
363
|
+
|
|
364
|
+
**[examples/example.py](examples/example.py)** or **[example.py](example.py)** (root directory)
|
|
365
|
+
|
|
366
|
+
This example demonstrates:
|
|
367
|
+
|
|
368
|
+
- Setting up a Swarms agent
|
|
369
|
+
- Creating FastAPI endpoints that use the agent
|
|
370
|
+
- Automatic payment processing with ATP middleware
|
|
371
|
+
- Usage tracking and billing for agent services
|
|
372
|
+
|
|
373
|
+
**Quick start with Swarms:**
|
|
374
|
+
|
|
375
|
+
```bash
|
|
376
|
+
# Install dependencies
|
|
377
|
+
pip install swarms atp-protocol
|
|
378
|
+
|
|
379
|
+
# Set your OpenAI API key
|
|
380
|
+
export OPENAI_API_KEY="your-key-here"
|
|
381
|
+
|
|
382
|
+
# Run the example
|
|
383
|
+
python examples/example.py
|
|
384
|
+
# or
|
|
385
|
+
python example.py
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
---
|
|
389
|
+
|
|
390
|
+
## Examples
|
|
391
|
+
|
|
392
|
+
ATP Protocol includes comprehensive examples showing how to integrate with various AI agent frameworks and APIs. All examples demonstrate automatic payment processing, token usage tracking, and Solana settlement.
|
|
393
|
+
|
|
394
|
+
### Framework Integration Examples
|
|
395
|
+
|
|
396
|
+
| Framework | Directory | Description | Documentation |
|
|
397
|
+
|-----------|-----------|-------------|--------------|
|
|
398
|
+
| **Swarms** | [`examples/swarms/`](examples/swarms/) | Swarms framework integration - native ATP Protocol support | [README](examples/swarms/README.md) |
|
|
399
|
+
| **LangChain** | [`examples/langchain/`](examples/langchain/) | LangChain agent integration with tools and conversational interface | [README](examples/langchain/README.md) |
|
|
400
|
+
| **AutoGen** | [`examples/autogen/`](examples/autogen/) | AutoGen multi-agent conversation framework integration | [README](examples/autogen/README.md) |
|
|
401
|
+
| **CrewAI** | [`examples/crewai/`](examples/crewai/) | CrewAI multi-agent crew workflows and task pipelines | [README](examples/crewai/README.md) |
|
|
402
|
+
| **Anthropic API** | [`examples/anthropic/`](examples/anthropic/) | Direct integration with Anthropic's Claude API | [README](examples/anthropic/README.md) |
|
|
403
|
+
|
|
404
|
+
### Standalone Examples
|
|
405
|
+
|
|
406
|
+
| Example | File | Description |
|
|
407
|
+
|---------|------|-------------|
|
|
408
|
+
| **Swarms Integration** | [`examples/example.py`](examples/example.py) | Complete Swarms framework integration example |
|
|
409
|
+
| **Full Flow** | [`examples/full_flow_example.py`](examples/full_flow_example.py) | End-to-end payment flow demonstration |
|
|
410
|
+
| **Settlement Service** | [`examples/settlement_service_example.py`](examples/settlement_service_example.py) | Direct settlement service usage |
|
|
411
|
+
| **Client Smoke Test** | [`examples/client_smoke_test.py`](examples/client_smoke_test.py) | Client testing and validation |
|
|
412
|
+
|
|
413
|
+
### Quick Start with Examples
|
|
414
|
+
|
|
415
|
+
Each example includes:
|
|
416
|
+
- **Server** (`server.py`) - FastAPI server with ATP middleware configured
|
|
417
|
+
- **Client** (`client.py`) - Example client with wallet authentication
|
|
418
|
+
- **README** - Framework-specific setup and usage instructions
|
|
419
|
+
|
|
420
|
+
**Getting started:**
|
|
421
|
+
|
|
422
|
+
1. Navigate to an example directory:
|
|
423
|
+
```bash
|
|
424
|
+
cd examples/swarms # or langchain, autogen, crewai, anthropic
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
2. Install dependencies (see example's README for specific requirements)
|
|
428
|
+
|
|
429
|
+
3. Configure environment variables:
|
|
430
|
+
```bash
|
|
431
|
+
# Create .env file
|
|
432
|
+
OPENAI_API_KEY="your-key" # For Swarms, LangChain, AutoGen, CrewAI
|
|
433
|
+
ANTHROPIC_API_KEY="your-key" # For Anthropic
|
|
434
|
+
ATP_PRIVATE_KEY="[1,2,3,...]"
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
4. Update `recipient_pubkey` in `server.py` with your Solana wallet address
|
|
438
|
+
|
|
439
|
+
5. Run the server:
|
|
440
|
+
```bash
|
|
441
|
+
python server.py
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
6. Test with the client:
|
|
445
|
+
```bash
|
|
446
|
+
python client.py
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
For detailed instructions, see the [Examples README](examples/README.md) or the framework-specific README in each example directory.
|
|
450
|
+
|
|
451
|
+
---
|
|
452
|
+
|
|
453
|
+
## Response Encryption & Payment Verification
|
|
454
|
+
|
|
455
|
+
ATP Protocol includes built-in **response encryption** to ensure users cannot access agent output until payment is confirmed. This provides strong protection against payment fraud.
|
|
456
|
+
|
|
457
|
+
### How It Works
|
|
458
|
+
|
|
459
|
+
1. **After endpoint execution**: The middleware encrypts sensitive response fields (e.g., `output`, `response`, `result`, `message`) before processing payment
|
|
460
|
+
2. **Payment processing**: Settlement is attempted via the Settlement Service
|
|
461
|
+
3. **Payment verification**: The middleware checks if payment status is `"paid"` and a transaction signature exists
|
|
462
|
+
4. **Conditional decryption**:
|
|
463
|
+
- ✅ **Payment succeeded**: Response is decrypted and returned to client
|
|
464
|
+
- ❌ **Payment failed**: Response remains encrypted with error message
|
|
465
|
+
|
|
466
|
+
### Encrypted Response Format
|
|
467
|
+
|
|
468
|
+
When payment fails, the response includes encrypted data and a clear message:
|
|
469
|
+
|
|
470
|
+
```json
|
|
471
|
+
{
|
|
472
|
+
"output": "encrypted_data_here...",
|
|
473
|
+
"usage": {"input_tokens": 150, "output_tokens": 50},
|
|
474
|
+
"atp_settlement": {
|
|
475
|
+
"error": "Settlement service unavailable",
|
|
476
|
+
"message": "Request timed out after 300.0s. The payment may have been sent successfully, but the settlement service did not respond in time.",
|
|
477
|
+
"type": "ReadTimeout"
|
|
478
|
+
},
|
|
479
|
+
"atp_settlement_status": "failed",
|
|
480
|
+
"atp_message": "Agent response is encrypted. Payment required to decrypt. Please provide a valid wallet private key and ensure payment succeeds."
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
### Settlement Error Handling
|
|
485
|
+
|
|
486
|
+
The middleware provides flexible error handling via the `fail_on_settlement_error` parameter:
|
|
487
|
+
|
|
488
|
+
- **`fail_on_settlement_error=False`** (default):
|
|
489
|
+
- Returns encrypted response with error details
|
|
490
|
+
- Client receives usage data and error information
|
|
491
|
+
- Useful for debugging and graceful degradation
|
|
492
|
+
|
|
493
|
+
- **`fail_on_settlement_error=True`**:
|
|
494
|
+
- Raises `HTTPException` (500) when settlement fails
|
|
495
|
+
- Request fails immediately
|
|
496
|
+
- Useful for strict payment requirements
|
|
497
|
+
|
|
498
|
+
### Timeout Handling
|
|
499
|
+
|
|
500
|
+
Settlement operations may take time due to blockchain confirmation. The middleware provides informative timeout messages:
|
|
501
|
+
|
|
502
|
+
- **ReadTimeout**: Payment may have succeeded, but settlement service didn't respond in time
|
|
503
|
+
- **ConnectTimeout**: Settlement service is unreachable
|
|
504
|
+
- **HTTP errors**: Network or service errors with status codes
|
|
505
|
+
|
|
506
|
+
Increase `settlement_timeout` if you experience timeouts even when payments succeed.
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
## Error Handling
|
|
511
|
+
|
|
512
|
+
- **Missing wallet key**: Returns `401 Unauthorized` if `require_wallet=True`
|
|
513
|
+
- **Missing usage data**: Logs warning and returns original response (no settlement)
|
|
514
|
+
- **Payment failure**:
|
|
515
|
+
- If `fail_on_settlement_error=False`: Returns encrypted response with error details
|
|
516
|
+
- If `fail_on_settlement_error=True`: Returns `500 Internal Server Error` and raises exception
|
|
517
|
+
- **Invalid private key**: Returns `500 Internal Server Error` with parsing error
|
|
518
|
+
- **Encryption failure**: Returns `500 Internal Server Error` without exposing agent output
|
|
519
|
+
- **Settlement timeout**: Returns encrypted response with timeout message (payment may have succeeded)
|
|
520
|
+
|
|
521
|
+
---
|
|
522
|
+
|
|
523
|
+
## How the Settlement Service Works
|
|
524
|
+
|
|
525
|
+
The Settlement Service is a centralized API that handles all payment logic. The official facilitator service at **`https://facilitator.swarms.world`** is ultra-fast and powered by Rust, ensuring low-latency payment processing.
|
|
526
|
+
|
|
527
|
+
The service provides:
|
|
528
|
+
|
|
529
|
+
- **`POST /v1/settlement/parse-usage`** - Parse usage from various formats
|
|
530
|
+
- **`POST /v1/settlement/calculate-payment`** - Calculate payment amounts
|
|
531
|
+
- **`POST /v1/settlement/settle`** - Execute payment transaction
|
|
532
|
+
|
|
533
|
+
The middleware calls these endpoints automatically. You can also call them directly if needed.
|
|
534
|
+
|
|
535
|
+
### Settlement Service Flow
|
|
536
|
+
|
|
537
|
+
1. **Parse usage** - Normalize token counts from any format
|
|
538
|
+
2. **Calculate cost** - Convert tokens to USD using your rates
|
|
539
|
+
3. **Fetch token price** - Get current SOL/USDC price
|
|
540
|
+
4. **Calculate payment** - Convert USD to token amount
|
|
541
|
+
5. **Split payment** - Calculate 95%/5% split
|
|
542
|
+
6. **Send transaction** - Execute Solana payment
|
|
543
|
+
7. **Verify** - Confirm transaction succeeded
|
|
544
|
+
8. **Return details** - Transaction signature and payment breakdown
|
|
545
|
+
|
|
546
|
+
All settlement logic is immutable and centralized in the service, ensuring consistency and reliability.
|
|
547
|
+
|
|
548
|
+
---
|
|
549
|
+
|
|
550
|
+
## Payment Tokens
|
|
551
|
+
|
|
552
|
+
ATP Protocol supports two payment tokens:
|
|
553
|
+
|
|
554
|
+
- **SOL** - Native Solana token (default)
|
|
555
|
+
- **USDC** - Stablecoin (treated as $1 USD)
|
|
556
|
+
|
|
557
|
+
Set `payment_token=PaymentToken.USDC` to use USDC instead of SOL.
|
|
558
|
+
|
|
559
|
+
---
|
|
560
|
+
|
|
561
|
+
## Security Considerations
|
|
562
|
+
|
|
563
|
+
| Security Aspect | Details |
|
|
564
|
+
|------------------------------|-------------------------------------------------------------------|
|
|
565
|
+
| **Private keys** | Only used in-memory during each request |
|
|
566
|
+
| **No key storage** | Keys are never persisted |
|
|
567
|
+
| **Settlement Service** | Handles all sensitive operations |
|
|
568
|
+
| **Response encryption** | Agent outputs are encrypted until payment is confirmed |
|
|
569
|
+
| **Payment verification** | Responses are only decrypted after successful payment verification |
|
|
570
|
+
| **Transaction verification** | Ensures payments are confirmed before a response is returned |
|
|
571
|
+
| **Encrypted failure mode** | Failed payments keep responses encrypted, preventing output access |
|
|
572
|
+
|
|
573
|
+
---
|
|
574
|
+
|
|
575
|
+
## About Swarms
|
|
576
|
+
|
|
577
|
+
ATP Protocol is part of the **[Swarms](https://swarms.ai)** ecosystem, the enterprise-grade production-ready multi-agent orchestration framework. Swarms provides the infrastructure for building and deploying autonomous agents at scale.
|
|
578
|
+
|
|
579
|
+
- **Framework**: [Swarms on GitHub](https://github.com/kyegomez/swarms)
|
|
580
|
+
- **Documentation**: [docs.swarms.world](https://docs.swarms.world)
|
|
581
|
+
- **Website**: [swarms.ai](https://swarms.ai)
|
|
582
|
+
|
|
583
|
+
ATP Protocol integrates seamlessly with Swarms agents, enabling agent-to-agent payments and automatic billing for agent services built on the Swarms framework.
|
|
584
|
+
|
|
585
|
+
---
|
|
586
|
+
|
|
587
|
+
## License
|
|
588
|
+
|
|
589
|
+
See [LICENSE](LICENSE) file for details.
|
|
590
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
atp/__init__.py,sha256=xCYA1cb5xw9IxqFsB1fC7jXBE48bHgKBq4ZhyW9AFyY,459
|
|
2
|
+
atp/client.py,sha256=GUZZ81PaJMia7mK_SS3uzU3qCpg0ChoLVYmQIRrfxQ8,24510
|
|
3
|
+
atp/config.py,sha256=EkswOYJ3Lj2WAw1Yy_2gbpk5FzJ3L--T6ARPixAOoW8,2336
|
|
4
|
+
atp/encryption.py,sha256=kgyhjf8qGq3oZjDIxGHzOlc5KOLPTi7nv0RApPZGk7o,5341
|
|
5
|
+
atp/middleware.py,sha256=br6wIfLQU_9ja62_k5R6htTe6SWqIT9b7FkBoeIYZOQ,29518
|
|
6
|
+
atp/schemas.py,sha256=29VtWKxtwlllAUOx2hoSEESpVWoKANdybOoTiTbFhnc,12344
|
|
7
|
+
atp/settlement_client.py,sha256=7e_271Bx1ZT2YzZH0HjAOx1krDXgPhMWVakRKsMMS3k,31395
|
|
8
|
+
atp_protocol-1.3.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
9
|
+
atp_protocol-1.3.0.dist-info/METADATA,sha256=zPazL_nEFxMCv3hnfpFrfw2TBl7EJNtbNjUnwBT5PNU,21578
|
|
10
|
+
atp_protocol-1.3.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
11
|
+
atp_protocol-1.3.0.dist-info/RECORD,,
|