Skip to main content

Python tools for the L402 protocol.

Project description

pyl402: L402 tools for Python

Take the L402 pill...

Overview

pyl402 is a Python package designed to handle HTTP requests with integrated payment functionality, specifically focusing on the Lightning Network protocol L402 and automatic handling of HTTP 402 Payment Required responses. It integrates seamlessly with Lightning wallets to provide a smooth experience when dealing with paid resources.

Note: This project is currently in alpha stage and is considered Work-In-Progress (WIP). Features and functionality are subject to change, and more testing is needed to ensure stability and reliability.

Installation

To install pyl402, you can use pip:

pip install pyl402

Please note that this package requires Python 3.7 or later.

Features

  • Automatic Token Handling: Manages L402 tokens automatically, storing and retrieving tokens as needed.
  • Integrated Payment: Automatically handles payment processes if a resource requires payment, through compatible wallet implementations.
  • Extensible: Easily extend the library to support different wallet types and token storage mechanisms.

Usage

Here's a quick example to get you started with the L402 Client:

from pyl402.wallet import AlbyWallet
from pyl402.token_store import MemoryTokenStore
from pyl402.client import L402Client

# Initialize wallet and token store
wallet = AlbyWallet(token="your_alby_api_token_here")
store = MemoryTokenStore()

# Create the L402 Client
client = L402Client(wallet=wallet, store=store)

# Use the client to send HTTP requests
response = client.get('http://rnd.ln.sulu.sh/randomnumber')
print(response.text)

This example demonstrates creating an L402 client using an Alby wallet and a memory-based token store to access a resource that may require payment.

Example with OpenAI SDK and Sulu Inference Endpoints

With this example you can access inference APIs just with Bitcoin and the Lightning Network!

import os
from openai import OpenAI

from pyl402.wallet import AlbyWallet
from pyl402.client import L402Client
from pyl402.token_store import MemoryTokenStore

# Create the L402 client
alby_wallet = AlbyWallet(os.getenv("ALBY_BEARER_TOKEN"))
token_store = MemoryTokenStore()
client = L402Client(wallet=alby_wallet, store=token_store)

client = OpenAI(
    http_client=client,
    base_url='https://suluai.ln.sulu.sh/v1',
)

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model="meta-llama/Llama-3-70b-chat-hf",
)
print(chat_completion.choices[0].message.content)

Contributions

Contributions are welcome! If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

Licensing

The code in this project is licensed under the MIT license. See LICENSE for details.

Disclaimer

This is an alpha release, and as such, it might contain bugs and incomplete features. We do not recommend using it in a production environment.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyl402-0.1.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

pyl402-0.1.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file pyl402-0.1.2.tar.gz.

File metadata

  • Download URL: pyl402-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for pyl402-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c2c6739c7b879e50f52c7287b64e3291dc00f331f97fade10b985171e2936e3f
MD5 5d33d031c23aacc6798a1c854f0ae82c
BLAKE2b-256 65055fd5868417f88afcb7bbca63a3e2f47a84ed0dfbda7fe4a1c8c138dd89a9

See more details on using hashes here.

File details

Details for the file pyl402-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pyl402-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/5.15.146.1-microsoft-standard-WSL2

File hashes

Hashes for pyl402-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e80e3afabc0ac82ba4d156d7c988b923599abc4bd7856f83fd6b7abc0f6a856
MD5 978ca6112b86ebcacd22ecfe20cfb2be
BLAKE2b-256 9cd41cde7b9b8bb7971be8121ad612e31be6eb4c34e4c46b41aececef2d86843

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page