Payments for AI agents using L402 protocol
Project description
l402-python
Usage
Installation
Install latest from the GitHub repository:
$ pip install git+https://github.com/Fewsats/l402-python.git
or from pypi
$ pip install l402_python
Documentation
Documentation can be found hosted on this GitHub repository’s pages. Additionally you can find package manager specific guidelines on conda and pypi respectively.
How to use
Server
# Create FastAPI app
app = FastAPI()
server = ServerManager(app).start()
INFO: Started server process [47447]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Payment Server
from uuid import uuid4
ps = PaymentServer(
payment_request_url="http://localhost:8000/payment_request",
onchain_provider=create_test_wallet(fund=False),
)
@app.get("/offers")
def offers():
offers_list = [Offer(
amount=1,
currency='USD',
description='Purchase 1 credit for API access',
offer_id=str(uuid4()),
payment_methods=['onchain'], # maybe not needed because we can generate it from the payment provider
title='1 Credit Package',
type='one-time'
)]
offers_response = ps.create_offers(offers_list)
return JSONResponse(
content=offers_response.model_dump(),
status_code=402
)
@app.post("/payment_request")
async def create_payment_request(request: PaymentRequest):
payment_request = ps.create_payment_request(**request.model_dump())
return JSONResponse(
content=payment_request,
status_code=200
)
Client
w = create_test_wallet(fund=False)
c = Client(onchain_provider=CoinbaseProvider(wallet=w))
r1 = httpx.get("http://localhost:8000/offers")
r1.status_code, r1.json()
INFO: 127.0.0.1:50799 - "GET /offers HTTP/1.1" 402 Payment Required
INFO: 127.0.0.1:50800 - "POST /payment_request HTTP/1.1" 200 OK
(402,
{'offers': [{'amount': 1,
'currency': 'USD',
'description': 'Purchase 1 credit for API access',
'offer_id': '3731f7ea-b00e-4389-9370-fd6acedffd0d',
'payment_methods': ['onchain'],
'title': '1 Credit Package',
'type': 'one-time'}],
'payment_context_token': '34b9a1f8-422b-44c2-8063-9109f5d42e17',
'payment_request_url': 'http://localhost:8000/payment_request',
'version': '0.2.2'})
if r1.status_code == HTTPStatus.PAYMENT_REQUIRED:
try:
ps = c.pay(r1.json())
except Exception as e:
print(e)
Insufficient funds: have 0, need 1.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
l402-0.1.0.tar.gz
(10.5 kB
view details)
Built Distribution
l402-0.1.0-py3-none-any.whl
(9.2 kB
view details)
File details
Details for the file l402-0.1.0.tar.gz
.
File metadata
- Download URL: l402-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
7885abb8a2919e522049f01b63368d6c41c1b99ac083f48eb23c9c3c8fbf55ec
|
|
MD5 |
f4f8ffd9bebbaa1cfcdc3769564e787f
|
|
BLAKE2b-256 |
def920fbbda5a112ef1ba30dd40c9acb9519966de74c0779543c3ba3ec47eb05
|
File details
Details for the file l402-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: l402-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
18bb9d0e53c2f83b6bd12d2a47dcdfe02085ce1cae2709cc35863ecae5e41299
|
|
MD5 |
286f73ee5968472d64373d9f7bb62533
|
|
BLAKE2b-256 |
cbcd25de9216010719139f7ace4fe1c6c900e1319629d26f763f3f3f752cfa29
|