Skip to main content

Python implementation of the client/server L402 protocol flow

Project description

L402 Python Library

This library simplifies the integration of internet-native paywalls into your Python applications by providing a client and server implementation of the L402 protocol.

HTTP servers like Flask and FastAPI can easily authorize and charge for requests, while clients can automatically gain access to protected services by obtaining credentials and paying invoices.

For learning more about the protocol, visit [l402.org]

Features

  • Built on top of the HTTP 402 Payment Required error code
  • Fine-grained authorization policies using Macaroons, giving you granular control over service access
  • Secure, fast, and cheap payments powered by the Lightning Network
  • Allows payments as small as a fraction of a cent
  • Machine-friendly design, perfect for seamless integration with AI-driven applications
  • Async-first architecture for optimal performance
  • Includes decorators and middlewares for popular HTTP servers: Flask, FastAPI, and Django
  • Drop-in replacement for the httpx client, making it easy to access protected services

Installation

The package is available at pypi and be installed with pip.

pip install l402

Usage

Client

To make requests that can handle 402 Payment Required responses:

from l402.client import httpx

# Configure the client with a preimage provider and credentials service.
httpx.configure(
    preimage_provider=MyPreimageProvider(),
    credentials_service=MyCredentialsService()
)

# Make a request
async with httpx.AsyncClient() as client:
    response = await client.get("https://example.com/protected")

Server

To protect routes and require payment:

from flask import Flask
from l402.server import Authenticator, Flask_l402_decorator

app = Flask(name)
authenticator = Authenticator(...)

# Paywall a route with a 1 USD cent payment required.
def pricing_func(amount: int, currency: str, description: str):
    price_in_cents = 1
    return price_in_cents, "USD", "L402 challenge"

@app.route('/protected')
@Flask_l402_decorator(authenticator, pricing_func)
def protected_route():
    return "Protected content"

Tutorial

If you want to learn more about the L402 protocol, how to set it up in your own server, or how to use the client library, we have a notebook tutorial to help you get started:

We'll guide you through the process step by step, providing code examples and explanations along the way. By the end of this tutorial, you'll have a solid understanding of how to integrate internet-native paywalls into your Python applications using the L402 Python Library

Contributing

Contributions are welcome! Please see the contributing guide for more information.

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

l402-0.0.5.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

l402-0.0.5-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file l402-0.0.5.tar.gz.

File metadata

  • Download URL: l402-0.0.5.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for l402-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a366dfb816636b6d8b7d1765b12430f35f0c432cdd4c7b54d4983f5a7e2d330c
MD5 82c66dfc5226890329f7da1d4085b5ce
BLAKE2b-256 083d1cd099612946bed1ffa32d11131c13d1446852f7f02456867f8937aadd8a

See more details on using hashes here.

File details

Details for the file l402-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: l402-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for l402-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 35b1eef8ca243699a46fb69906a7b71423a4945a71e006198154b7e9dbbec192
MD5 071a634a6d94172a1adf0b73f240f6e6
BLAKE2b-256 2b80d304acd0e56a56d08c4909a34714516bc22bc33c642e24bcc176c6b58ad9

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