| Name | fly402langchain JSON |
| Version |
0.1.1
JSON |
| download |
| home_page | None |
| Summary | LangChain integration for the 402fly payment protocol |
| upload_time | 2025-11-01 07:17:13 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.8 |
| license | MIT License
Copyright (c) 2025 402fly Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. |
| keywords |
402
402fly
agents
langchain
payments
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# 402fly-langchain
LangChain integration for the X402 payment protocol - enables AI agents to autonomously pay for API access.
## Overview
The `402fly-langchain` package provides LangChain tools and utilities that enable AI agents to autonomously make payments to access paid APIs using the X402 protocol. Agents can detect payment requirements, process payments, and retry requests automatically.
## Features
- Autonomous payment capability for LangChain agents
- X402 payment tool for LangChain agent workflows
- Helper functions for quick agent creation
- Configurable payment limits for safety
- Seamless integration with existing LangChain applications
## Installation
```bash
pip install 402fly-langchain
```
## Quick Start
### Simple Agent with Autonomous Payment
The easiest way to create an X402-enabled agent:
```python
from 402fly_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
import json
# Load wallet
with open("wallet.json") as f:
wallet_data = json.load(f)
keypair = Keypair.from_bytes(bytes(wallet_data))
# Create agent with X402 support in one function call
agent = create_x402_agent(
wallet_keypair=keypair,
llm=ChatOpenAI(temperature=0),
max_payment="5.0", # Safety limit
debug=True,
)
# Agent can now autonomously pay for API access
inputs = {
"messages": [
{
"role": "user",
"content": "Get the premium data from http://localhost:8000/premium-data"
}
]
}
for chunk in agent.stream(inputs, stream_mode="updates"):
if "agent" in chunk:
result = chunk["agent"]
if result and "messages" in result:
final_message = result["messages"][-1]
print(f"Agent response: {final_message.content}")
```
## Usage Patterns
### Pattern 1: Quick Agent Creation (Recommended)
Best for most use cases - creates a fully configured agent:
```python
from 402fly_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
keypair = Keypair() # Your wallet
agent = create_x402_agent(
wallet_keypair=keypair,
llm=ChatOpenAI(temperature=0),
max_payment="10.0",
debug=True,
)
# Use the agent
result = agent.invoke({
"messages": [
{"role": "user", "content": "Access paid APIs"}
]
})
```
### Pattern 2: Custom Agent with Payment Tool
For custom agent configurations:
```python
from 402fly_langchain import X402PaymentTool
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
keypair = Keypair()
# Create X402 payment tool
payment_tool = X402PaymentTool(
wallet_keypair=keypair,
max_payment="5.0",
name="pay_for_api",
description="Make payment to access premium API data"
)
# Create custom agent with your tools
agent = create_agent(
model=ChatOpenAI(temperature=0),
tools=[payment_tool, *other_tools],
system_prompt="Your custom system prompt",
)
```
### Pattern 3: Multi-API Access
Agent accessing multiple paid APIs:
```python
from 402fly_langchain import create_x402_agent
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
keypair = Keypair()
agent = create_x402_agent(
wallet_keypair=keypair,
llm=ChatOpenAI(temperature=0),
max_payment="10.0", # Higher limit for multiple payments
debug=True,
)
inputs = {
"messages": [
{
"role": "user",
"content": "Get data from both /premium-data and /expensive-data APIs, then compare them"
}
]
}
result = agent.invoke(inputs)
```
## Complete Example
```python
from 402fly_langchain import (
create_x402_agent,
X402PaymentTool,
)
from langchain_openai import ChatOpenAI
from solders.keypair import Keypair
import json
import os
# Load wallet keypair
with open("wallet.json") as f:
wallet_data = json.load(f)
keypair = Keypair.from_bytes(bytes(wallet_data))
# Create X402-enabled agent
agent = create_x402_agent(
wallet_keypair=keypair,
llm=ChatOpenAI(
temperature=0,
api_key=os.getenv("OPENAI_API_KEY")
),
max_payment="5.0",
debug=True,
)
# Run agent
inputs = {
"messages": [
{
"role": "user",
"content": "Get premium market data from http://localhost:8000/premium-data and summarize it"
}
]
}
for chunk in agent.stream(inputs, stream_mode="updates"):
if "agent" in chunk:
result = chunk["agent"]
if "messages" in result:
print(result["messages"][-1].content)
```
## Configuration
### create_x402_agent Parameters
- `wallet_keypair`: Your Solana wallet keypair for payments (required)
- `llm`: LangChain LLM instance (required)
- `max_payment`: Maximum payment amount - safety limit (required)
- `debug`: Enable debug logging (optional)
### X402PaymentTool Parameters
- `wallet_keypair`: Your Solana wallet keypair
- `max_payment`: Maximum payment limit
- `name`: Tool name for agent (default: "x402_payment")
- `description`: Tool description for agent
## Wallet Setup
```python
import json
from solders.keypair import Keypair
# Create new wallet
keypair = Keypair()
wallet_data = list(bytes(keypair))
with open("wallet.json", "w") as f:
json.dump(wallet_data, f)
print(f"Wallet address: {keypair.pubkey()}")
print("Fund this wallet with SOL and USDC on devnet!")
```
## Documentation
For complete API reference and guides, see:
- [Documentation](https://402fly.github.io/docs)
- [GitHub Repository](https://github.com/SerPepe/402fly)
- [Full Example](https://github.com/SerPepe/402fly/tree/main/examples/python/langchain-agent)
## Testing
```bash
pytest tests/
```
## License
MIT License - See [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "fly402langchain",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "402, 402fly, agents, langchain, payments",
"author": null,
"author_email": "402fly Contributors <hello@402fly.dev>",
"download_url": "https://files.pythonhosted.org/packages/80/6f/6874d82ee7808e28547f77dd604e9378b1403f18fd848de7892e6a705587/fly402langchain-0.1.1.tar.gz",
"platform": null,
"description": "# 402fly-langchain\n\nLangChain integration for the X402 payment protocol - enables AI agents to autonomously pay for API access.\n\n## Overview\n\nThe `402fly-langchain` package provides LangChain tools and utilities that enable AI agents to autonomously make payments to access paid APIs using the X402 protocol. Agents can detect payment requirements, process payments, and retry requests automatically.\n\n## Features\n\n- Autonomous payment capability for LangChain agents\n- X402 payment tool for LangChain agent workflows\n- Helper functions for quick agent creation\n- Configurable payment limits for safety\n- Seamless integration with existing LangChain applications\n\n## Installation\n\n```bash\npip install 402fly-langchain\n```\n\n## Quick Start\n\n### Simple Agent with Autonomous Payment\n\nThe easiest way to create an X402-enabled agent:\n\n```python\nfrom 402fly_langchain import create_x402_agent\nfrom langchain_openai import ChatOpenAI\nfrom solders.keypair import Keypair\nimport json\n\n# Load wallet\nwith open(\"wallet.json\") as f:\n wallet_data = json.load(f)\n keypair = Keypair.from_bytes(bytes(wallet_data))\n\n# Create agent with X402 support in one function call\nagent = create_x402_agent(\n wallet_keypair=keypair,\n llm=ChatOpenAI(temperature=0),\n max_payment=\"5.0\", # Safety limit\n debug=True,\n)\n\n# Agent can now autonomously pay for API access\ninputs = {\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Get the premium data from http://localhost:8000/premium-data\"\n }\n ]\n}\n\nfor chunk in agent.stream(inputs, stream_mode=\"updates\"):\n if \"agent\" in chunk:\n result = chunk[\"agent\"]\n\nif result and \"messages\" in result:\n final_message = result[\"messages\"][-1]\n print(f\"Agent response: {final_message.content}\")\n```\n\n## Usage Patterns\n\n### Pattern 1: Quick Agent Creation (Recommended)\n\nBest for most use cases - creates a fully configured agent:\n\n```python\nfrom 402fly_langchain import create_x402_agent\nfrom langchain_openai import ChatOpenAI\nfrom solders.keypair import Keypair\n\nkeypair = Keypair() # Your wallet\n\nagent = create_x402_agent(\n wallet_keypair=keypair,\n llm=ChatOpenAI(temperature=0),\n max_payment=\"10.0\",\n debug=True,\n)\n\n# Use the agent\nresult = agent.invoke({\n \"messages\": [\n {\"role\": \"user\", \"content\": \"Access paid APIs\"}\n ]\n})\n```\n\n### Pattern 2: Custom Agent with Payment Tool\n\nFor custom agent configurations:\n\n```python\nfrom 402fly_langchain import X402PaymentTool\nfrom langchain.agents import create_agent\nfrom langchain_openai import ChatOpenAI\nfrom solders.keypair import Keypair\n\nkeypair = Keypair()\n\n# Create X402 payment tool\npayment_tool = X402PaymentTool(\n wallet_keypair=keypair,\n max_payment=\"5.0\",\n name=\"pay_for_api\",\n description=\"Make payment to access premium API data\"\n)\n\n# Create custom agent with your tools\nagent = create_agent(\n model=ChatOpenAI(temperature=0),\n tools=[payment_tool, *other_tools],\n system_prompt=\"Your custom system prompt\",\n)\n```\n\n### Pattern 3: Multi-API Access\n\nAgent accessing multiple paid APIs:\n\n```python\nfrom 402fly_langchain import create_x402_agent\nfrom langchain_openai import ChatOpenAI\nfrom solders.keypair import Keypair\n\nkeypair = Keypair()\n\nagent = create_x402_agent(\n wallet_keypair=keypair,\n llm=ChatOpenAI(temperature=0),\n max_payment=\"10.0\", # Higher limit for multiple payments\n debug=True,\n)\n\ninputs = {\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Get data from both /premium-data and /expensive-data APIs, then compare them\"\n }\n ]\n}\n\nresult = agent.invoke(inputs)\n```\n\n## Complete Example\n\n```python\nfrom 402fly_langchain import (\n create_x402_agent,\n X402PaymentTool,\n)\nfrom langchain_openai import ChatOpenAI\nfrom solders.keypair import Keypair\nimport json\nimport os\n\n# Load wallet keypair\nwith open(\"wallet.json\") as f:\n wallet_data = json.load(f)\n keypair = Keypair.from_bytes(bytes(wallet_data))\n\n# Create X402-enabled agent\nagent = create_x402_agent(\n wallet_keypair=keypair,\n llm=ChatOpenAI(\n temperature=0,\n api_key=os.getenv(\"OPENAI_API_KEY\")\n ),\n max_payment=\"5.0\",\n debug=True,\n)\n\n# Run agent\ninputs = {\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"Get premium market data from http://localhost:8000/premium-data and summarize it\"\n }\n ]\n}\n\nfor chunk in agent.stream(inputs, stream_mode=\"updates\"):\n if \"agent\" in chunk:\n result = chunk[\"agent\"]\n if \"messages\" in result:\n print(result[\"messages\"][-1].content)\n```\n\n## Configuration\n\n### create_x402_agent Parameters\n\n- `wallet_keypair`: Your Solana wallet keypair for payments (required)\n- `llm`: LangChain LLM instance (required)\n- `max_payment`: Maximum payment amount - safety limit (required)\n- `debug`: Enable debug logging (optional)\n\n### X402PaymentTool Parameters\n\n- `wallet_keypair`: Your Solana wallet keypair\n- `max_payment`: Maximum payment limit\n- `name`: Tool name for agent (default: \"x402_payment\")\n- `description`: Tool description for agent\n\n## Wallet Setup\n\n```python\nimport json\nfrom solders.keypair import Keypair\n\n# Create new wallet\nkeypair = Keypair()\nwallet_data = list(bytes(keypair))\nwith open(\"wallet.json\", \"w\") as f:\n json.dump(wallet_data, f)\n\nprint(f\"Wallet address: {keypair.pubkey()}\")\nprint(\"Fund this wallet with SOL and USDC on devnet!\")\n```\n\n## Documentation\n\nFor complete API reference and guides, see:\n- [Documentation](https://402fly.github.io/docs)\n- [GitHub Repository](https://github.com/SerPepe/402fly)\n- [Full Example](https://github.com/SerPepe/402fly/tree/main/examples/python/langchain-agent)\n\n## Testing\n\n```bash\npytest tests/\n```\n\n## License\n\nMIT License - See [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2025 402fly Contributors\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.",
"summary": "LangChain integration for the 402fly payment protocol",
"version": "0.1.1",
"project_urls": {
"Documentation": "https://docs.402fly.dev",
"Homepage": "https://github.com/SerPepe/402fly",
"Repository": "https://github.com/SerPepe/402fly"
},
"split_keywords": [
"402",
" 402fly",
" agents",
" langchain",
" payments"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6ffad4a4b7de82a62e87c8d4c2a22d2e0018aa64bd5f6aa75489ec35e184f7a7",
"md5": "40f08ce6569259a75e421be0b7424fdf",
"sha256": "2ffb84f3084d744eb3e58f6fd15fda05f8e007964c7b2565d1617cbf18865e34"
},
"downloads": -1,
"filename": "fly402langchain-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "40f08ce6569259a75e421be0b7424fdf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8371,
"upload_time": "2025-11-01T07:17:12",
"upload_time_iso_8601": "2025-11-01T07:17:12.316028Z",
"url": "https://files.pythonhosted.org/packages/6f/fa/d4a4b7de82a62e87c8d4c2a22d2e0018aa64bd5f6aa75489ec35e184f7a7/fly402langchain-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "806f6874d82ee7808e28547f77dd604e9378b1403f18fd848de7892e6a705587",
"md5": "2e36a23cb04847c28eb9dd47ad72b2dd",
"sha256": "7f61408226f913e22c4fa8c2aa931e176cb884b478a6e38f2074c08f057b3622"
},
"downloads": -1,
"filename": "fly402langchain-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "2e36a23cb04847c28eb9dd47ad72b2dd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 6258,
"upload_time": "2025-11-01T07:17:13",
"upload_time_iso_8601": "2025-11-01T07:17:13.461135Z",
"url": "https://files.pythonhosted.org/packages/80/6f/6874d82ee7808e28547f77dd604e9378b1403f18fd848de7892e6a705587/fly402langchain-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-01 07:17:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "SerPepe",
"github_project": "402fly",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fly402langchain"
}