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.

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())

Spending limits

By default every payment the server asks for is signed. Pass limits to refuse anything else:

client = create_payment_client(
    "0x...",
    max_amount=100_000,  # atomic USDC units (6 decimals): at most $0.10 per payment
    allowed_pay_to=["0x..."],  # only pay these recipients
)

If no payment option offered by the server fits, nothing is signed and libertai_x402.PaymentLimitError is raised.

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.0

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.0
File Size Uploaded Provenance Actions
libertai_x402-0.2.0.tar.gz 3.4 kB With provenance Details for libertai_x402-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 7.5 kB

Release files / libertai_x402-0.2.0.tar.gz

Download URL libertai_x402-0.2.0.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f0f570e46acc49999592ae9d7ae2468aa8d79256479ce9d77397ab6a269f8fc5
BLAKE2b-256 checksum
How to use checksums
b058d4489f83406f771711bac28b7943422f67445c290dcabf0a1d05531a445d
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 24, 2026.

Transparency log

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

Download URL libertai_x402-0.2.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1435ff00b6ca3f054bc286258856dcb09f29e2b3db8a866fadbe6535c1de3570
BLAKE2b-256 checksum
How to use checksums
582d73e8f49dc3dd66539207076d0e24c8fb4d30f533e49e0815e41d43ec2ece
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 24, 2026.

Transparency log

Security for libertai-x402 0.2.0

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.0.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 24, 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