OpenLibx402

Enable AI agents and web APIs to autonomously pay for services using HTTP 402 "Payment Required" and Solana blockchain. A multi-language library ecosystem implementing the X402 protocol for autonomous payments across Python, TypeScript, Go, Rust, Java, and Kotlin.

Python 3.8+
TypeScript
Go 1.21+
Rust 1.70+
Java 11+
Kotlin 1.9+

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/nextjs
npm install @openlibx402/langchain
npm install @openlibx402/langgraph
Go
go get github.com/openlibx402/openlibx402-go/core
go get github.com/openlibx402/openlibx402-go/client
go get github.com/openlibx402/openlibx402-go/nethttp
go get github.com/openlibx402/openlibx402-go/echo
Rust
# Add to Cargo.toml
[dependencies]
openlibx402-core = "0.1.0"
openlibx402-client = "0.1.0"
openlibx402-rocket = "0.1.0"
openlibx402-actix = "0.1.0"
Java (Maven)
<!-- Add to pom.xml -->
<dependency>
    <groupId>io.github.openlibx402</groupId>
    <artifactId>openlibx402-core</artifactId>
    <version>0.1.0</version>
</dependency>
Kotlin (Gradle)
// Add to build.gradle.kts
dependencies {
    implementation("io.github.openlibx402:openlibx402-core:0.1.0")
    implementation("io.github.openlibx402:openlibx402-client:0.1.0")
}

Key Features

โœจ One-Line Integration
Add payments to APIs with a single decorator
๐Ÿค– AI-Native
Built specifically for autonomous agent workflows
โšก Instant Settlement
Payments settle in ~200ms on Solana
๐Ÿ’ฐ Micropayments
Support payments as low as $0.001
๐Ÿ” No Accounts
No API keys, subscriptions, or manual billing
๐ŸŒ Chain-Agnostic
Solana first, architected for multi-chain
๐Ÿ› ๏ธ Framework Integrations
FastAPI, Express, LangChain, LangGraph, and more
๐ŸŒ Multi-Language
Python, TypeScript, Go, Rust, Java, Kotlin
๐Ÿงช Testing Support
Mock processors for development and testing

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

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•