Guides

Use Unslept with LangChain

Give a LangChain agent a tool that pays x402 requests within its policy.

Install

terminal
pip install "unslept[web3,langchain]"

Create the tool

agent.py
import os
from eth_account import Account
from unslept import Unslept
from unslept.web3_chain import EthAccountSigner
from unslept.langchain_tool import unslept_tool

signer = EthAccountSigner(Account.from_key(os.environ["UNSLEPT_SIGNER_KEY"]))
unslept = Unslept(os.environ["UNSLEPT_AGENT_ACCOUNT"], signer=signer, network="robinhood-chain")

tools = [unslept_tool(unslept)]  # pass to your agent, e.g. create_react_agent(llm, tools)

The tool is called pay_x402_request. It takes a URL, an optional method and body, and an optional max_amount_usdg per call, written as a decimal string such as "0.25".

Environment

The Python SDK reads the same variables as the TypeScript SDK: UNSLEPT_CHAIN_ID, UNSLEPT_RPC_URL, UNSLEPT_USDG, UNSLEPT_ROUTER and UNSLEPT_BUFFERS.