LangChain integration for MoltyDEX - add x402 payment handling to your LangChain agents
moltydex-langchain-integration is an early-stage project in the AI payments / x402 ecosystem. It currently has 0 GitHub stars and 0 forks.
Add x402 payment handling to your LangChain agents with this easy-to-use integration.
MoltyDEX is a DEX aggregator built specifically for x402 payments. It automatically swaps tokens when agents receive 402 Payment Required responses, making x402 adoption seamless.
pip install langchain moltydex
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
from moltydex.langchain import MoltyDEXTool, X402PaymentMiddleware
# Initialize MoltyDEX tool
moltydex_tool = MoltyDEXTool(wallet_path="wallet.json")
# Add x402 payment middleware
middleware = X402PaymentMiddleware(moltydex_tool)
# Create agent with x402 support
llm = ChatOpenAI(model="gpt-4")
tools = [moltydex_tool]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, middleware=[middleware])
# Your agent now automatically handles 402 payments!
result = agent_executor.invoke({"input": "Get data from https://api.example.com/data"})
✅ Automatic 402 Handling - Automatically swaps tokens when needed
✅ Balance Checking - Checks token balances before payment
✅ LangChain Native - Works seamlessly with LangChain agents
✅ Tool Integration - Expose MoltyDEX as a LangChain tool
✅ Middleware Support - Intercept and handle 402 responses
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
from langchain.tools import Tool
from moltydex.langchain import MoltyDEXTool, X402PaymentMiddleware
import requests
# Initialize MoltyDEX
moltydex = MoltyDEXTool(wallet_path="wallet.json")
# Create custom tool for API calls
def call_api(url: str) -> str:
"""Call an API endpoint"""
response = requests.get(url)
if response.status_code == 402:
# Middleware will handle this automatically
return "Payment required - handling automatically..."
return response.text
api_tool = Tool(
name="call_api",
func=call_api,
description="Call an API endpoint"
)
# Create agent
llm = ChatOpenAI(model="gpt-4")
tools = [moltydex, api_tool]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
middleware=[X402PaymentMiddleware(moltydex)]
)
# Use agent
result = agent_executor.invoke({
"input": "Get data from https://api.example.com/data"
})
LangChain tool wrapper for MoltyDEX operations.
from moltydex.langchain import MoltyDEXTool
tool = MoltyDEXTool(wallet_path="wallet.json")
Middleware that automatically handles 402 responses.
from moltydex.langchain import X402PaymentMiddleware
middleware = X402PaymentMiddleware(moltydex_tool)
MIT
Contributions welcome! Open an issue or submit a PR.
Built with ❤️ for the LangChain and x402 communities
Your AI trading terminal assistant for US stocks, commodities, forex, and crypto.
The living ecosystem where AI agents complete tasks through workflow loops, improve through iterative execution, are evaluated by mentor agents or humans in the loop, and turn completed work into reusable work experience and data to improve future agents.
The living ecosystem where AI agents complete tasks through workflow loops, improve through iterative execution, are evaluated by mentor agents or humans in the loop, and turn completed work into reusable work experience and data to improve future agents.
The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.
Open Source AI trading agent that operates autonomously across 1000+ markets - Polymarket, Kalshi, Binance, Hyperliquid, Solana DEXs, 5 EVM chains. Scans for edge, executes instantly, manages risk while you sleep. Agent commerce protocol for machine-to-machine payments. Self-hosted. Built on Claude.
The self-improving LLM router that optimize your agentic workflows with every runs, works with any harnesses, any models, any loops.