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.
β¨ 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
fromopenlibx402_langchainimportcreate_x402_agentfromlangchain.chat_modelsimportChatOpenAIagent=create_x402_agent(wallet_keypair=keypair,llm=ChatOpenAI(),max_payment="5.0")response=agent.run("Get premium market data from the API")
fromlangchain.chat_modelsimportChatOpenAIfromopenlibx402_langchainimportcreate_x402_agentfromsolders.keypairimportKeypair# Load walletkeypair=Keypair()# Create agent with X402 supportagent=create_x402_agent(wallet_keypair=keypair,llm=ChatOpenAI(),max_payment="5.0")# Agent can now autonomously pay for API accessresponse=agent.run("Get the latest market data from https://api.example.com/premium-data ""and summarize the key trends")
fromtypingimportTypedDictfromlanggraph.graphimportStateGraph,ENDfromopenlibx402_langgraphimportpayment_node,check_payment_requiredfromsolders.keypairimportKeypairclassAgentState(TypedDict):api_url:strapi_response:strpayment_required:boolpayment_completed:boolwallet_keypair:Keypairworkflow=StateGraph(AgentState)workflow.add_node("fetch",fetch_api_node)workflow.add_node("pay",payment_node)# From openlibx402-langgraphworkflow.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()
π 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
fromopenlibx402_core.testingimportMockSolanaPaymentProcessorprocessor=MockSolanaPaymentProcessor()processor.balance=100.0# Use in tests without real blockchainclient=X402AutoClient(wallet_keypair=test_keypair)client.client.processor=processor
fromopenlibx402_core.testingimportTestServerserver=TestServer(payment_address="test_address",token_mint="test_usdc")server.start(port=8402)# Test against mock server# ...
# Clone repositorygitclonehttps://github.com/openlibx402/openlibx402.git
cdopenlibx402
# Install development dependenciespipinstall-e"packages/python/openlibx402-core[dev]"# Run testspytest
# Format codeblackpackages/python/
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.