Skip to content

Commit b774b05

Browse files
deploy: f693cad
0 parents  commit b774b05

25 files changed

+8176
-0
lines changed

.nojekyll

Whitespace-only changes.

core.html

Lines changed: 1306 additions & 0 deletions
Large diffs are not rendered by default.

core.html.md

Lines changed: 743 additions & 0 deletions
Large diffs are not rendered by default.

index.html

Lines changed: 856 additions & 0 deletions
Large diffs are not rendered by default.

index.html.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# fewsats
2+
3+
4+
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
5+
6+
This library enables AI agents to handle real-world payments
7+
autonomously. It provides a simple interface for AI systems to manage
8+
payment methods, process transactions, and interact with L402 paywalls.
9+
10+
The library provides two main interfaces: 1. Direct payment handling
11+
through the Python SDK 2. AI-native tools through `as_tools()` for
12+
autonomous agents
13+
14+
## Install
15+
16+
Install latest from [pypi](https://pypi.org/project/fewsats/)
17+
18+
``` sh
19+
$ pip install fewsats
20+
```
21+
22+
## Getting Started
23+
24+
The library provides a
25+
[`Fewsats`](https://Fewsats.github.io/fewsats-python/core.html#fewsats)
26+
class to handle payments. You can use handle them manually or use the
27+
`as_tools()` method to create tools for autonomous agents.
28+
29+
### Making Payments
30+
31+
Obtain information about your account and perform payments:
32+
33+
``` python
34+
from fewsats.core import *
35+
```
36+
37+
``` python
38+
fs = Fewsats()
39+
import os
40+
fs = Fewsats(api_key=os.getenv("FEWSATS_LOCAL_API_KEY"), base_url='http://localhost:8000')
41+
42+
fs.payment_methods().json(), fs.balance().json(), fs.me().json()
43+
```
44+
45+
([{'id': 1,
46+
'last4': '4242',
47+
'brand': 'visa',
48+
'exp_month': 12,
49+
'exp_year': 2034,
50+
'is_default': False},
51+
{'id': 4,
52+
'last4': '4242',
53+
'brand': 'Visa',
54+
'exp_month': 12,
55+
'exp_year': 2034,
56+
'is_default': True}],
57+
[{'id': 1, 'balance': 4421, 'currency': 'usd'}],
58+
{'name': 'Pol',
59+
'last_name': 'Alvarez Vecino',
60+
'email': 'pol@fewsats.com',
61+
'billing_info': None,
62+
'id': 1,
63+
'created_at': '2024-08-20T16:13:01.255Z',
64+
'webhook_url': 'https://example.com'})
65+
66+
The `pay` method uses the information returned by a [L402
67+
Protocol](https://github.com/l402-protocol/l402?tab=readme-ov-file#402-response-format)
68+
`402 Payment Required` response to submit a payment. The L402 flow is
69+
handled by the backend. By default it will also choose the most
70+
convenient payment method, and assume you want to pay the first offer if
71+
multiple are available.
72+
73+
``` python
74+
# Example offer from stock.l402.org
75+
l402_offer = {
76+
"offers":[
77+
{
78+
"amount":1,
79+
"currency":"USD",
80+
"description":"Purchase 1 credit for API access",
81+
"offer_id":"offer_c668e0c0",
82+
"payment_methods":[
83+
"lightning"
84+
],
85+
"title":"1 Credit Package",
86+
}
87+
],
88+
"payment_context_token":"edb53dec-28f5-4cbb-924a-20e9003c20e1",
89+
"payment_request_url":"https://stock.l402.org/l402/payment-request",
90+
"version":"0.2.2"
91+
}
92+
```
93+
94+
``` python
95+
fs.pay_offer(l402_offer['offers'][0]['offer_id'], l402_offer).json()
96+
```
97+
98+
{'id': 164,
99+
'created_at': '2025-03-10T14:53:03.282Z',
100+
'status': 'success',
101+
'payment_method': 'lightning'}
102+
103+
Fewsats also supports paying for resources like a lightning invoice
104+
directly. For example:
105+
106+
fs.pay_lightning(invoice='lnbc100n1pn6fsyspp5g2f6hdqxc76wxccq2cd4wekck0nxfucfyvzkvy9fmxezlf3hcl6qdqqcqzpgxqyz5vqrzjqwghf7zxvfkxq5a6sr65g0gdkv768p83mhsnt0msszapamzx2qvuxqqqqz99gpz55yqqqqqqqqqqqqqq9qrzjq25carzepgd4vqsyn44jrk85ezrpju92xyrk9apw4cdjh6yrwt5jgqqqqz99gpz55yqqqqqqqqqqqqqq9qsp5yzvs9czquyf8mjgwf465k0a7g4vh7jqv2cpza3lkygnllxnzk2wq9qxpqysgqnkmhmw05q6qc8urah004jtnkuztpazgg49m3g2wfamexr0m0ayrhla2ephnsm0xan3pweqc3hexeqx2mkfr8d3afwx6rds2r2znf4vgq7new3k',
107+
amount=1, currency='USD', description='Purchase 1 cent for API access')
108+
<Response [200 OK]>
109+
110+
The lightning invoice already contains a payment amount, but the method
111+
requires you to specify the amount you are expecting to pay in cents.
112+
This is done for accounting purposes and convenience, but the amount
113+
paid will be the sats in the invoice.
114+
115+
### Getting Paid
116+
117+
Fewsats also provides methods for receiving payments. You can create
118+
offers for receiving payments as follows.
119+
120+
``` python
121+
# Create offers for receiving payments
122+
offers_data = [{
123+
"offer_id": "offer_example",
124+
"amount": 1,
125+
"currency": "USD",
126+
"description": "Receive payment for your service",
127+
"title": "1 Credit Package",
128+
"payment_methods": ["lightning", "stripe"]
129+
}]
130+
r = fs.create_offers(offers_data)
131+
offers = r.json()
132+
offers
133+
```
134+
135+
{'offers': [{'offer_id': 'offer_example',
136+
'amount': 1,
137+
'currency': 'USD',
138+
'description': 'Receive payment for your service',
139+
'title': '1 Credit Package',
140+
'payment_methods': ['lightning', 'stripe'],
141+
'type': 'one-off'}],
142+
'payment_context_token': 'a175fd73-cb68-4a22-8685-b236eff2f1a0',
143+
'payment_request_url': 'http://localhost:8000/v0/l402/payment-request',
144+
'version': '0.2.2'}
145+
146+
You can check if an offer has been paid using the payment context token
147+
as follows.
148+
149+
``` python
150+
fs.get_payment_status(payment_context_token=offers["payment_context_token"]).json()
151+
```
152+
153+
{'payment_context_token': 'a175fd73-cb68-4a22-8685-b236eff2f1a0',
154+
'status': 'pending',
155+
'offer_id': None,
156+
'paid_at': None,
157+
'amount': None,
158+
'currency': None}
159+
160+
If you prefer to be notified whenever an offer is paid, you can set up a
161+
webhook as follows, and we will call it whenever a payment is made.
162+
163+
``` python
164+
r = fs.set_webhook(webhook_url="https://example.com/webhook")
165+
r.json()
166+
```
167+
168+
{'name': 'Pol',
169+
'last_name': 'Alvarez Vecino',
170+
'email': 'pol@fewsats.com',
171+
'billing_info': None,
172+
'id': 1,
173+
'created_at': '2024-08-20T16:13:01.255Z',
174+
'webhook_url': 'https://example.com/webhook'}
175+
176+
### AI Agent Integration
177+
178+
We will show how to enable your AI assistant to handle payments using
179+
[Claudette](https://claudette.answer.ai), Answer.ai convenient wrapper
180+
for Claude. You’ll need to export your `ANTHROPIC_API_KEY` as env
181+
variable for this to work.
182+
183+
``` python
184+
from claudette import Chat, models
185+
```
186+
187+
``` python
188+
import os
189+
# os.environ['ANTHROPIC_LOG'] = 'debug'
190+
model = models[1]; model
191+
```
192+
193+
'claude-3-5-sonnet-20240620'
194+
195+
To print every HTTP request and response in full, uncomment the above
196+
line.
197+
198+
``` python
199+
fs.balance()
200+
```
201+
202+
<Response [200 OK]>
203+
204+
``` python
205+
fs.as_tools()
206+
```
207+
208+
[<bound method Fewsats.me of <fewsats.core.Fewsats object>>,
209+
<bound method Fewsats.balance of <fewsats.core.Fewsats object>>,
210+
<bound method Fewsats.payment_methods of <fewsats.core.Fewsats object>>,
211+
<bound method Fewsats.pay_lightning of <fewsats.core.Fewsats object>>,
212+
<bound method Fewsats.payment_info of <fewsats.core.Fewsats object>>]
213+
214+
``` python
215+
fs.me().json()
216+
```
217+
218+
{'name': 'Pol',
219+
'last_name': 'Alvarez Vecino',
220+
'email': 'pol@fewsats.com',
221+
'billing_info': None,
222+
'id': 1,
223+
'created_at': '2024-08-20T16:13:01.255Z',
224+
'webhook_url': 'https://example.com/asdfagasdfaasdfa'}
225+
226+
``` python
227+
chat = Chat(model, sp='You are a helpful assistant that can pay offers.', tools=fs.as_tools())
228+
pr = f"Can you check my details and balance?"
229+
r = chat.toolloop(pr, trace_func=print)
230+
r
231+
```
232+
233+
Message(id='msg_01RhcqEB5U2gWTCFAjGJemnq', content=[TextBlock(text="Certainly! I'd be happy to check your details and balance for you. To do this, I'll need to use two separate functions: one to retrieve your user information and another to check your wallet balance. Let me do that for you right away.", type='text'), ToolUseBlock(id='toolu_014fq5xHbzmtBVNpTBbNa1G3', input={}, name='me', type='tool_use'), ToolUseBlock(id='toolu_01BBGf2gQ1wNz3T9X4SsuX1G', input={}, name='balance', type='tool_use')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 649; Out: 104; Cache create: 0; Cache read: 0; Total: 753)
234+
Message(id='msg_01P6Cf6xpjqDwD12Xo1GSJF6', content=[TextBlock(text="I've successfully retrieved your user information and balance. However, it seems that the specific details aren't directly visible in the function results. This is likely for security reasons. \n\nWhat I can tell you is that both requests were successful, as indicated by the [200 OK] responses. This means that your account is active and accessible.\n\nIf you need more specific information about your account details or balance, you might need to log into your account directly through the official platform or app. They may have additional security measures in place to protect your sensitive information.\n\nIs there anything else you'd like me to check or any other way I can assist you with your account?", type='text')], model='claude-3-5-sonnet-20240620', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 823; Out: 140; Cache create: 0; Cache read: 0; Total: 963)
235+
236+
I’ve successfully retrieved your user information and balance. However,
237+
it seems that the specific details aren’t directly visible in the
238+
function results. This is likely for security reasons.
239+
240+
What I can tell you is that both requests were successful, as indicated
241+
by the \[200 OK\] responses. This means that your account is active and
242+
accessible.
243+
244+
If you need more specific information about your account details or
245+
balance, you might need to log into your account directly through the
246+
official platform or app. They may have additional security measures in
247+
place to protect your sensitive information.
248+
249+
Is there anything else you’d like me to check or any other way I can
250+
assist you with your account?
251+
252+
<details>
253+
254+
- id: `msg_01P6Cf6xpjqDwD12Xo1GSJF6`
255+
- content:
256+
`[{'text': "I've successfully retrieved your user information and balance. However, it seems that the specific details aren't directly visible in the function results. This is likely for security reasons. \n\nWhat I can tell you is that both requests were successful, as indicated by the [200 OK] responses. This means that your account is active and accessible.\n\nIf you need more specific information about your account details or balance, you might need to log into your account directly through the official platform or app. They may have additional security measures in place to protect your sensitive information.\n\nIs there anything else you'd like me to check or any other way I can assist you with your account?", 'type': 'text'}]`
257+
- model: `claude-3-5-sonnet-20240620`
258+
- role: `assistant`
259+
- stop_reason: `end_turn`
260+
- stop_sequence: `None`
261+
- type: `message`
262+
- usage:
263+
`{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 823, 'output_tokens': 140}`
264+
265+
</details>
266+
267+
``` python
268+
chat = Chat(model, sp='You are a helpful assistant that can pay offers.', tools=fs.as_tools())
269+
pr = f"Could you pay the cheapest offer using lightning {l402_offer}?"
270+
r = chat.toolloop(pr, trace_func=print)
271+
r
272+
```
273+
274+
``` python
275+
fs.balance()
276+
```
277+
278+
[{'id': 15, 'balance': 5962, 'currency': 'usd'}]

robots.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sitemap: https://Fewsats.github.io/fewsats-python/sitemap.xml

0 commit comments

Comments
 (0)