Skip to main content

libertai-x402

x402 utilities that handle payments automatically for LibertAI services

Install

pip install libertai-x402

0.2.0 is a breaking release: the client now speaks standard x402 v2 (PAYMENT-REQUIRED / PAYMENT-SIGNATURE headers) and is built on the official x402 package. 0.1.x clients cannot pay the current LibertAI API.

0.2.1 changes the default behaviour: payment limits now apply unless you change them (see Payment limits). Payments to anything other than the LibertAI API, or above 5 USDC, are refused by default, and passing None now disables a limit instead of meaning "no limit configured".

Prerequisite: Permit2 approval

Chat endpoints use the upto scheme, which settles through Permit2. Before the first upto payment, the paying wallet must approve the Permit2 contract 0x000000000022D473030F116dDEE9F6B43aC78BA3 to spend its Base USDC (0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913), once. Fixed-price endpoints (images, TTS, embeddings) use the exact scheme (EIP-3009) and need no approval.

Usage with OpenAI SDK

import asyncio
from openai import AsyncOpenAI
from libertai_x402 import create_payment_client

async def main():
    http_client = create_payment_client("0x...")

    client = AsyncOpenAI(
        base_url="https://api.libertai.io/v1",
        api_key="x402",
        http_client=http_client,
    )

    response = await client.chat.completions.create(
        model="qwen3.5-27b",
        messages=[{"role": "user", "content": "Hello!"}],
    )

    print(response.choices[0].message.content)

asyncio.run(main())

Set api_key to "x402" — the wrapper strips this dummy value before sending requests. Real API keys (any other value) are preserved.

Usage with httpx

import asyncio
from libertai_x402 import create_payment_client

async def main():
    async with create_payment_client("0x...") as client:
        resp = await client.post(
            "https://api.libertai.io/v1/chat/completions",
            json={
                "model": "qwen3.5-27b",
                "messages": [{"role": "user", "content": "Hello!"}],
            },
        )
        print(resp.json())

asyncio.run(main())

Payment limits

Every payment is checked before it is signed or sent. Defaults target the LibertAI API:

Kwarg Default Check
allowed_origins (LIBERTAI_API_ORIGIN,) (https://api.libertai.io) only 402s from these origins are paid, and signed payments are only sent there
allowed_pay_to (LIBERTAI_PAY_TO,) (0x48E5A3e497D0C092D5B0Ba22B5A86ADA94227DFa) recipient (case-insensitive)
allowed_assets (BASE_USDC,) (0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913) token contract (case-insensitive)
max_amount DEFAULT_MAX_AMOUNT (5_000_000, 5 USDC) atomic USDC units (6 decimals) per payment
max_timeout_seconds DEFAULT_MAX_TIMEOUT_SECONDS (86400) signature validity requested by the server

Extend a default with the exported constants, or pass None to disable a check:

from libertai_x402 import LIBERTAI_API_ORIGIN, LIBERTAI_PAY_TO, create_payment_client

client = create_payment_client(
    "0x...",
    allowed_origins=[LIBERTAI_API_ORIGIN, "https://other.example"],
    allowed_pay_to=[LIBERTAI_PAY_TO, "0x..."],
    max_amount=100_000,  # at most $0.10 per payment
    max_timeout_seconds=None,  # no timeout check
)

If a payment falls outside the limits, nothing is signed or sent and libertai_x402.PaymentLimitError is raised. Requests that are not answered with 402 work for any origin. A redirect in answer to a paid request is returned as-is, never followed, so a payment is never repeated or forwarded elsewhere.

How it works

  1. Makes the request normally
  2. If the server responds with 402, decodes the payment requirements from the PAYMENT-REQUIRED header
  3. Signs an exact (EIP-3009) or upto (Permit2) payment on Base (eip155:8453) with the provided private key
  4. Retries the request with the signed payment in the PAYMENT-SIGNATURE header

License

MIT

Metadata

Release files for libertai-x402 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libertai-x402 0.2.1
File Size Uploaded Provenance Actions
libertai_x402-0.2.1.tar.gz 4.8 kB With provenance Details for libertai_x402-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for libertai-x402 0.2.1
File Interpreter ABI Platform Actions
libertai_x402-0.2.1-py3-none-any.whl Python 3 none any Details for libertai_x402-0.2.1-py3-none-any.whl

Total release size: 10.5 kB

Release files / libertai_x402-0.2.1.tar.gz

Download URL libertai_x402-0.2.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b6de10b7d1190ca2b04026b399bfe834dccec10ce60f3a595bea055531c594d1
BLAKE2b-256 checksum
How to use checksums
ce109a967681e27cc06a3c4fa72422dacfcf05955d853db9f3c945671beb7b38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / libertai_x402-0.2.1-py3-none-any.whl

Download URL libertai_x402-0.2.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8c725598ee071b40492adbeb734ed3360956b0eaf362d7636a918d6fe7b130b
BLAKE2b-256 checksum
How to use checksums
0075016991774ef0f8d50884f8b2e2cc94e0db3907ba14f61e2130ec51522e1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for libertai-x402 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for libertai_x402-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page