Skip to content

OpenLibx402: Autonomous Payments for AI Agents

Enable AI agents and web APIs to autonomously pay for services using HTTP 402 "Payment Required" and Solana blockchain

License: MIT Python 3.8+ TypeScript

What is OpenLibx402?

OpenLibx402 is a library ecosystem that implements the X402 protocol - an open standard for enabling AI agents to autonomously pay for API access, data, and digital services using the HTTP 402 "Payment Required" status code and blockchain micropayments.

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 Design - Solana first, architected for multi-chain
πŸ› οΈ Framework Integrations - FastAPI, LangChain, LangGraph, and more

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)

1
2
3
4
5
6
7
8
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()

LangChain Agent

from openlibx402_langchain import create_x402_agent
from langchain.chat_models import ChatOpenAI

agent = create_x402_agent(
    wallet_keypair=keypair,
    llm=ChatOpenAI(),
    max_payment="5.0"
)

response = agent.run("Get premium market data from the API")

How It Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AI Agent   β”‚  ─1─→   β”‚  API Server  β”‚         β”‚ Blockchain β”‚
β”‚   (Client)  β”‚         β”‚   (Server)   β”‚         β”‚  (Solana)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                        β”‚                        β”‚
       β”‚  GET /data             β”‚                        β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β†’β”‚                        β”‚
       β”‚                        β”‚                        β”‚
       β”‚  402 Payment Required  β”‚                        β”‚
       β”‚  + Payment Details     β”‚                        β”‚
       │←────────────────────────                        β”‚
       β”‚                        β”‚                        β”‚
       β”‚  Create & Broadcast    β”‚                        β”‚
       β”‚  Payment Transaction   β”‚                        β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β†’β”‚
       β”‚                        β”‚                        β”‚
       β”‚                        β”‚   Verify Transaction   β”‚
       β”‚                        │←────────────────────────
       β”‚                        β”‚                        β”‚
       β”‚  GET /data             β”‚                        β”‚
       β”‚  + Payment Auth Header β”‚                        β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β†’β”‚                        β”‚
       β”‚                        β”‚                        β”‚
       β”‚  200 OK + Data         β”‚                        β”‚
       │←────────────────────────                        β”‚

Documentation

πŸ“š Technical Specification - Complete architecture and design
πŸ“– Quick Reference - Common patterns and examples
πŸ“ Project Summary - Overview and next steps

Packages

Core

  • openlibx402-core - Core protocol implementation
  • @openlibx402/core - TypeScript equivalent

Server Frameworks

  • openlibx402-fastapi - FastAPI middleware (Python)
  • @openlibx402/express - Express.js middleware (TypeScript)
  • @openlibx402/nextjs - Next.js API routes (TypeScript)
  • @openlibx402/hono - Hono middleware (TypeScript)

Client Libraries

  • openlibx402-client - HTTP client with payment support (Python)
  • @openlibx402/client - TypeScript client

AI Agent Integrations

  • openlibx402-langchain - LangChain Tool & Middleware (Python)
  • openlibx402-langgraph - LangGraph nodes (Python)
  • @openlibx402/langchain - TypeScript LangChain integration
  • @openlibx402/langgraph - TypeScript LangGraph integration

Use Cases

For API Providers

  • πŸ’΅ Monetize APIs with pay-per-use pricing
  • 🚫 Eliminate API key management
  • ⚑ Instant payment settlement
  • πŸ›‘οΈ No chargebacks or fraud risk

For AI Agents

  • πŸ”“ Access premium data without human intervention
  • πŸ’° Pay exactly for what you use
  • 🌍 No geographic restrictions
  • πŸ€– Fully autonomous operation

Real-World Examples

  • πŸ“Š Research agent paying per financial data point
  • 🎯 Trading bot accessing real-time market data
  • πŸ“° Content aggregator paying per article
  • πŸ–ΌοΈ Image generation API charging per image
  • ☁️ GPU compute charged per minute

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Application Layer                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
β”‚  β”‚ FastAPI  β”‚  β”‚LangChain β”‚  β”‚LangGraph β”‚             β”‚
β”‚  β”‚ Middlewareβ”‚  β”‚   Tool   β”‚  β”‚  Nodes   β”‚             β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚             β”‚             β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              openlibx402-core / @openlibx402/core              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   Payment    β”‚  β”‚    Solana    β”‚  β”‚     Error     β”‚  β”‚
β”‚  β”‚    Models    β”‚  β”‚  Processor   β”‚  β”‚   Handling    β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Blockchain Layer                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚  β”‚  Solana  β”‚  β”‚ Ethereum β”‚  β”‚   Base   β”‚  (Future)    β”‚
β”‚  β”‚  Devnet  β”‚  β”‚ Mainnet  β”‚  β”‚    L2    β”‚              β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Development Status

βœ… Phase 1: Python

  • βœ… Technical specification complete
  • βœ… Core package (Python)
  • βœ… FastAPI integration
  • βœ… Client library
  • βœ… LangChain integration
  • βœ… LangGraph integration
  • βœ… Example implementations

πŸ—οΈ Phase 2: TypeScript

  • βœ… Core package (TypeScript)
  • βœ… Express.js middleware
  • βœ… Client library (TS)
  • βœ… LangChain.js integration
  • βœ… LangGraph.js integration
  • πŸ—οΈ Next.js integration

πŸ”² Phase 3: Ecosystem

  • [ ] Flask middleware
  • [ ] Django middleware
  • [ ] Hono middleware
  • [ ] Additional agent frameworks
  • [ ] CLI tools

πŸ”² Phase 4: Advanced

  • [ ] Multi-chain support (Ethereum, Base)
  • [ ] Payment batching
  • [ ] Admin dashboard
  • [ ] Analytics & monitoring
  • [ ] Browser extension

Installation (When Available)

Python

pip install openlibx402-core openlibx402-fastapi openlibx402-client
pip install openlibx402-langchain openlibx402-langgraph

TypeScript

npm install @openlibx402/core @openlibx402/express @openlibx402/client
npm install @openlibx402/langchain @openlibx402/langgraph

Examples

FastAPI Server

from fastapi import FastAPI
from openlibx402_fastapi import X402Config, init_x402, payment_required

# Initialize X402
config = X402Config(
    payment_address="YOUR_WALLET",
    token_mint="USDC_MINT",
    network="solana-devnet"
)
init_x402(config)

app = FastAPI()

@app.get("/basic-data")
async def get_basic_data():
    return {"data": "Free content"}

@app.get("/premium-data")
@payment_required(amount="0.10", description="Premium market data")
async def get_premium_data():
    return {"data": "Premium content", "price": 100.50}

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

Configuration

Environment Variables

1
2
3
4
X402_PAYMENT_ADDRESS=YourSolanaWalletAddress
X402_TOKEN_MINT=USDC_MINT_ADDRESS
X402_NETWORK=solana-devnet
X402_RPC_URL=https://api.devnet.solana.com

Code Configuration

from openlibx402_fastapi import X402Config

config = X402Config(
    payment_address="YOUR_WALLET",
    token_mint="USDC_MINT",
    network="solana-devnet",
    rpc_url="https://api.devnet.solana.com",
    payment_timeout=300,  # 5 minutes
    auto_verify=True
)

Security

πŸ” Key Security Features: - Private keys never leave client - On-chain transaction verification - Nonce-based replay protection - Payment expiration timestamps - Maximum payment limits - HTTPS required for production

⚠️ Security Best Practices: - Never log private keys - Use environment variables for secrets - Validate all payment fields - Set reasonable payment timeouts - Implement rate limiting - Use hardware wallets in production

Testing

Mock Payment Processor

1
2
3
4
5
6
7
8
from openlibx402_core.testing import MockSolanaPaymentProcessor

processor = MockSolanaPaymentProcessor()
processor.balance = 100.0

# Use in tests without real blockchain
client = X402AutoClient(wallet_keypair=test_keypair)
client.client.processor = processor

Test Server

from openlibx402_core.testing import TestServer

server = TestServer(
    payment_address="test_address",
    token_mint="test_usdc"
)
server.start(port=8402)

# Test against mock server
# ...

Contributing

We welcome contributions! Here's how you can help:

  1. πŸ› Report bugs via GitHub Issues
  2. πŸ’‘ Suggest features or improvements
  3. πŸ“ Improve documentation
  4. πŸ”§ Submit pull requests
  5. ⭐ Star the repository

Development Setup

# Clone repository
git clone https://github.com/openlibx402/openlibx402.git
cd openlibx402

# Install development dependencies
pip install -e "packages/python/openlibx402-core[dev]"

# Run tests
pytest

# Format code
black packages/python/

Roadmap

Q4 2025

  • βœ… Technical specification
  • βœ… Core libraries (Python & TypeScript)
  • βœ… FastAPI integration
  • βœ… LangChain/LangGraph integrations
  • βœ… Express.js, Next.js integrations
  • βœ… Documentation site
  • πŸ”² Additional framework support
  • πŸ”² CLI tools

2026

  • πŸ”² Multi-chain support (Ethereum, Base)
  • πŸ”² Admin dashboard
  • πŸ”² Analytics & monitoring
  • πŸ”² Production deployments
  • πŸ”² Browser extension
  • πŸ”² Wallet UI components
  • πŸ”² Zapier/Make.com integrations
  • πŸ”² Enterprise features

FAQ

Q: Why Solana first?
A: Solana offers ~200ms transaction finality and <$0.0001 fees, making it ideal for micropayments.

Q: Will this support other blockchains?
A: Yes! The architecture is designed to be chain-agnostic. Ethereum and Base L2 support is planned.

Q: Do I need crypto knowledge to use this?
A: Minimal. The libraries handle blockchain complexity. You just need a wallet and some tokens.

Q: Is this production-ready?
A: Not yet. We're currently in development. Follow progress on GitHub.

Q: How much do transactions cost?
A: On Solana devnet/mainnet, transaction fees are <$0.0001. Payment amounts are configurable.

Q: Can agents really operate autonomously?
A: Yes! Once configured with a wallet, agents can discover, pay for, and use APIs without human intervention.

Resources

Community

License

OpenLibx402 is released under the MIT License.

Acknowledgments


Built with ❀️ for the autonomous AI economy

Documentation | Examples | Contribute