Metadata-Version: 2.2
Name: l402
Version: 0.1.0
Summary: Payments for AI agents using L402 protocol
Home-page: https://github.com/Fewsats/l402-python
Author: 
Author-email: 
License: Apache Software License 2.0
Keywords: nbdev jupyter notebook python
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastcore
Requires-Dist: fastlite
Provides-Extra: dev
Requires-Dist: claudette; extra == "dev"
Requires-Dist: fastapi; extra == "dev"
Requires-Dist: uvicorn; extra == "dev"
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# l402-python


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Usage

### Installation

Install latest from the GitHub
[repository](https://github.com/Fewsats/l402-python):

``` sh
$ pip install git+https://github.com/Fewsats/l402-python.git
```

or from [pypi](https://pypi.org/project/l402-python/)

``` sh
$ pip install l402_python
```

### Documentation

Documentation can be found hosted on this GitHub
[repository](https://github.com/Fewsats/l402-python)’s
[pages](https://Fewsats.github.io/l402-python/). Additionally you can
find package manager specific guidelines on
[conda](https://anaconda.org/Fewsats/l402-python) and
[pypi](https://pypi.org/project/l402-python/) respectively.

## How to use

### Server

``` python
# 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

``` python
from uuid import uuid4
```

``` python
ps = PaymentServer(
    payment_request_url="http://localhost:8000/payment_request",
    onchain_provider=create_test_wallet(fund=False),
)
```

``` python
@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
    )
```

``` python
@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

``` python
w = create_test_wallet(fund=False)
c = Client(onchain_provider=CoinbaseProvider(wallet=w))
```

``` python
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'})

``` python
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.
