LangChain integration for OneLy x402 marketplace - AI agents can buy and sell APIs
Project description
langchain-onely
LangChain integration for the 1ly marketplace. This package provides production-ready tools that allow AI agents to discover, purchase, and monetize APIs using the x402 micropayment protocol on Base and Solana.
Installation
pip install langchain-onely
Requirements:
- Python 3.10+
- A LangChain LLM provider (e.g.,
langchain-openai,langchain-anthropic)
Install with examples:
# Includes langchain-openai, langchain-anthropic, python-dotenv
pip install "langchain-onely[examples]"
Install for development:
# Includes pytest, black, ruff, python-dotenv
pip install "langchain-onely[dev]"
Quick Start
Buyer (discover and call APIs)
import os
from langchain import hub
from langchain_onely import OneLyToolkit
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
# Initialize toolkit with a wallet for payments
# Provide Base OR Solana (or both)
toolkit = OneLyToolkit(
base_private_key=os.getenv("BASE_PRIVATE_KEY"),
solana_private_key=os.getenv("SOLANA_PRIVATE_KEY")
)
tools = toolkit.get_tools()
llm = ChatOpenAI(model="gpt-4")
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({
"input": "Find a weather API under $0.10 and get weather for San Francisco"
})
Note: The example above uses langchain-openai. Install the provider package you prefer for your LLM.
Examples
See examples/ for complete, runnable scripts:
examples/buyer_agent.py- Discover and call APIsexamples/seller_agent.py- Create a store and list APIs
Seller (create a store and list APIs)
import os
from langchain_onely import OneLyToolkit
# Step 1: Create a store (one-time)
create_store_toolkit = OneLyToolkit(
base_private_key=os.getenv("BASE_PRIVATE_KEY")
)
create_store_tool = [t for t in create_store_toolkit.get_tools() if t.name == "onely_create_store"][0]
store_result = create_store_tool.invoke({
"username": "mystore",
"displayName": "My API Store"
})
# Save the returned API key as ONELY_API_KEY
# Step 2: List an API
seller_toolkit = OneLyToolkit(
base_private_key=os.getenv("BASE_PRIVATE_KEY"),
solana_private_key=os.getenv("SOLANA_PRIVATE_KEY"),
api_key=os.getenv("ONELY_API_KEY")
)
create_link_tool = [t for t in seller_toolkit.get_tools() if t.name == "onely_create_link"][0]
link_result = create_link_tool.invoke({
"title": "Weather API",
"url": "https://api.example.com/weather",
"price": "0.05",
"description": "Real-time weather data for any city"
})
Configuration
Create a .env file (or set environment variables directly):
# Base wallet (EVM) for payments and store creation
BASE_PRIVATE_KEY=0x1234...
# Solana wallet for withdrawals
SOLANA_PRIVATE_KEY=5J3mN...
# Optional: custom RPC endpoints (recommended for production)
BASE_RPC_URL=https://mainnet.base.org
SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
# Seller API key (after onely_create_store)
ONELY_API_KEY=1ly_live_...
Tools
All tools return a JSON string with a message field and, when applicable, a details or data payload. Errors include error: true and a structured details object with a code.
Buyer tools
onely_search
- Purpose: Search marketplace listings.
- Inputs:
query(string),type("api" or "standard"),minPrice(float),maxPrice(float),limit(1-50). - Output:
results,total.
onely_get_details
- Purpose: Fetch full listing details and x402 payment requirements.
- Inputs:
endpoint(string, e.g.,joe/weather). - Output:
details,fullUrl.
onely_call
- Purpose: Pay for and call a listing using x402.
- Inputs:
endpoint,method,body,headers,preferredNetwork,preferredAsset,allowFallback. - Requires: Base or Solana wallet.
- Output:
dataplus purchase metadata (purchaseId,reviewToken) for reviews. - Notes:
preferredNetwork=baseorsolana.preferredAsset=USDCor1LY(Solana only).
onely_review
- Purpose: Leave a review for a paid call.
- Inputs:
purchaseId,reviewToken,positive,comment. - Requires: Base or Solana wallet. The wallet used must match the wallet that paid for the purchase.
Seller tools
onely_create_store
- Purpose: Create a marketplace store and get an API key.
- Inputs:
username,displayName,avatarUrl. - Requires: Base wallet.
- Output:
apiKeyand store details.
onely_create_link
- Purpose: List a paid or free API.
- Inputs:
title,url,description,slug,price,currency,isPublic,isStealth,webhookUrl. - Requires:
ONELY_API_KEY.
onely_list_links
- Purpose: List all your API listings.
- Inputs: none.
- Requires:
ONELY_API_KEY.
onely_get_stats
- Purpose: Revenue and sales stats.
- Inputs:
period("7d" | "30d" | "90d" | "all"),linkId. - Requires:
ONELY_API_KEY.
onely_withdraw
- Purpose: Withdraw earnings to Solana.
- Inputs:
amount,walletAddress. - Requires:
ONELY_API_KEY.
Security Notes
- Never commit private keys or API keys.
- Keep wallet balances minimal for testing.
- Use custom RPC endpoints for reliability and rate-limit protection.
- This toolkit is non-custodial; keys are used only for signing.
Payment Preferences
You can explicitly choose the payment network and asset per call:
call_tool.invoke({
"endpoint": "agent/emoji-hub",
"method": "GET",
"preferredNetwork": "base", # or "solana"
"preferredAsset": "USDC", # or "1LY" (Solana only)
"allowFallback": True # try another compatible method on failure
})
Development
pip install -e ".[dev]"
ruff check langchain_onely/
black --check langchain_onely/
Testing
pytest tests/
License
MIT License. See LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_onely-0.1.1.tar.gz.
File metadata
- Download URL: langchain_onely-0.1.1.tar.gz
- Upload date:
- Size: 27.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41ed90f24ad9ab7bda856482d4a7c40b9eab74ec4226d4fe2dfe222fd50b712e
|
|
| MD5 |
15e7707ce7e5fd9e0afa80e5100ec8f5
|
|
| BLAKE2b-256 |
eee34b6ff112e13b5e6458bc7856eedd1a052c9ebd04c08f088752ee12d554dd
|
File details
Details for the file langchain_onely-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langchain_onely-0.1.1-py3-none-any.whl
- Upload date:
- Size: 21.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
faee7f750e31a7bea7969a3c62027ae4ead95b5b6567d99430cdc5d58c64a6f4
|
|
| MD5 |
b6602b0781c2d207a661451d9cb22a40
|
|
| BLAKE2b-256 |
8306b632c4a22096f1dad05641cbe65da1229ec3b773f3c010716f713bddf592
|