LangChain tools for pr402/x402 protocol — autonomous AI agent payments on Solana
Project description
langchain-pr402
LangChain tools for autonomous AI agent payments on Solana via the pr402/x402 protocol.
Give any LangChain agent a Solana wallet so it can autonomously pay for HTTP services that return 402 Payment Required.
Installation
pip install langchain-pr402
Quick Start
from langchain_pr402 import X402RequestsWrapper, X402GetTool, X402BalanceTool
# 1. Create a wrapper with your agent's Solana keypair (defaults to Devnet)
wrapper = X402RequestsWrapper(
keypair_bytes=[1, 2, 3, ...], # 64-byte Solana keypair as list of ints
# rpc_url defaults to "https://api.devnet.solana.com"
# default_facilitator_url defaults to "https://preview.ipay.sh" (Devnet)
)
# 2. Create tools and give them to your agent
tools = [
X402GetTool(requests_wrapper=wrapper),
X402BalanceTool(requests_wrapper=wrapper),
]
# 3. Use with any LangChain agent (GPT-4, Claude, Llama, etc.)
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
llm = ChatOpenAI(model="gpt-4o-mini")
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
executor.invoke({"input": "Check my wallet balance"})
How It Works
- Your agent calls
X402GetToolto fetch a URL. - If the server responds with
HTTP 402 Payment Required, the tool automatically:- Parses the payment terms from the
Payment-Requiredheader - Builds a Solana transaction via the pr402 facilitator (Devnet by default)
- Signs the transaction locally with the agent's keypair
- Retries the request with a
PAYMENT-SIGNATUREheader
- Parses the payment terms from the
- The agent receives the paid content — the LLM never sees the payment complexity.
Tools
| Tool | Description |
|---|---|
X402GetTool |
GET requests with automatic 402 payment handling |
X402PostTool |
POST requests with automatic 402 payment handling |
X402BalanceTool |
Check the agent's wallet address and SOL/USDC balance |
Setting Up the Agent Wallet
AI agents don't create their own wallets. The developer provisions one:
- Create a wallet:
solana-keygen new -o agent-keypair.json - Fund it: Send USDC and a small amount of SOL for transaction fees
- Configure: Pass the keypair bytes to
X402RequestsWrapper
# Via environment variable (recommended for production)
export SOLANA_PRIVATE_KEY='[1,2,3,...,64]'
import os, json
wrapper = X402RequestsWrapper(
keypair_bytes=json.loads(os.environ["SOLANA_PRIVATE_KEY"])
)
Networks
By default, the wrapper is configured for Solana Devnet (great for testing). To switch to Mainnet, override both URLs:
wrapper = X402RequestsWrapper(
keypair_bytes=json.loads(os.environ["SOLANA_PRIVATE_KEY"]),
rpc_url="https://api.mainnet-beta.solana.com",
default_facilitator_url="https://ipay.sh", # Mainnet facilitator
)
| Network | RPC URL | Facilitator URL |
|---|---|---|
| Devnet (default) | https://api.devnet.solana.com |
https://preview.ipay.sh |
| Mainnet | https://api.mainnet-beta.solana.com |
https://ipay.sh |
Security
- Private key material is never serialised —
keypair_bytesis excluded frommodel_dump(),repr(), and logging. - The wrapper signs transactions locally. Private keys never leave the machine.
- The pr402 facilitator only receives the public key and returns an unsigned transaction.
Links
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_pr402-0.1.0.tar.gz.
File metadata
- Download URL: langchain_pr402-0.1.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aefd58cc1008ac6502e74bd57c293f159f2d45191512ffbf80dc77a83c863793
|
|
| MD5 |
3cf5a0f7060fb9fdb881d9f8b0bc3cfe
|
|
| BLAKE2b-256 |
2405adcec82f0b5428451cd940f01095690e4780d822506d7351388855fbdf24
|
File details
Details for the file langchain_pr402-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_pr402-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31e30db653766f34dd7db82c30d7ded4b6eb3ee0e244d0e97fc209a1bd9c5cef
|
|
| MD5 |
5f74bb9b64f64d34d3c335ab3031b4b1
|
|
| BLAKE2b-256 |
61c6cd541edbb3c6eebb057ff87463dd1b106f67bee42236e341e82f46a1f764
|