OpenLibx402

An open-source library for AI-native x402 integrations in the Solana ecosystem. Built for Python and Node.js, distributed via PyPI and npm, with server implementations for FastAPI and Express.js. Accelerate.

Quick Start

Server (FastAPI)
from fastapi import FastAPI
from openlibx402_fastapi import payment_required

app = FastAPI()

@app.get("/premium-data")
@payment_required(
    amount="0.10",
    payment_address="YOUR_WALLET_ADDRESS",
    token_mint="USDC_MINT_ADDRESS"
)
async def get_premium_data():
    return {"data": "Premium content"}
Client (Auto-Payment)
from openlibx402_client import X402AutoClient
from solders.keypair import Keypair

client = X402AutoClient(wallet_keypair=keypair)

# Automatically handles 402 and pays
response = await client.fetch(
    "https://api.example.com/premium-data"
)
data = response.json()

AI Agent Integration

LangChain Agent
from langchain.chat_models import ChatOpenAI
from openlibx402_langchain import create_x402_agent
from solders.keypair import Keypair

# Load wallet
keypair = Keypair()

# Create agent with X402 support
agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(),
    max_payment="5.0"
)

# Agent can now autonomously pay for API access
response = agent.run(
    "Get the latest market data from https://api.example.com/premium-data "
    "and summarize the key trends"
)
LangGraph Workflow
from typing import TypedDict
from langgraph.graph import StateGraph, END
from openlibx402_langgraph import payment_node, check_payment_required
from solders.keypair import Keypair

class AgentState(TypedDict):
    api_url: str
    api_response: str
    payment_required: bool
    payment_completed: bool
    wallet_keypair: Keypair

workflow = StateGraph(AgentState)

workflow.add_node("fetch", fetch_api_node)
workflow.add_node("pay", payment_node)  # From openlibx402-langgraph
workflow.add_node("process", process_node)

workflow.set_entry_point("fetch")

workflow.add_conditional_edges(
    "fetch",
    check_payment_required,
    {
        "payment_required": "pay",
        "success": "process",
        "error": END
    }
)

workflow.add_edge("pay", "fetch")
workflow.add_edge("process", END)

app = workflow.compile()

Installation

Python
pip install openlibx402-core
pip install openlibx402-client
pip install openlibx402-fastapi
pip install openlibx402-langchain
pip install openlibx402-langgraph
Node.js / TypeScript
npm install @openlibx402/core
npm install @openlibx402/client
npm install @openlibx402/express
npm install @openlibx402/langchain
npm install @openlibx402/langgraph

Key Features

One-Line Integration
Add payments with a single decorator
AI-Native
Built for autonomous agent workflows
Instant Settlement
Payments settle in ~200ms
No Accounts
No API keys or subscriptions needed
Framework Support
FastAPI, Express, LangChain, LangGraph
Chain-Agnostic
Solana first, multi-chain ready

How It Works

AI
AI Agent
(Client)
API
API Server
(Server)
⬡⬡
Blockchain
(Solana)
1
GET /data
AI Agent requests premium content
API Server
2
402 Payment Required
Server returns payment request
AI Agent
3
Broadcast Payment
Agent sends payment to blockchain
Blockchain
4
Verify Transaction
Server confirms payment on-chain (~200ms)
API Server
5
GET /data + Auth
Agent retries with payment proof
API Server
6
200 OK + Data
Server returns premium content
AI Agent
Fully Automated Payment Flow
No user intervention required • Instant settlement • Sub-cent fees
═══════════════════════════════

Open Source • Built for Solana

═══════════════════════════════