Skip to main content

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

  1. Your agent calls X402GetTool to fetch a URL.
  2. If the server responds with HTTP 402 Payment Required, the tool automatically:
    • Parses the payment terms from the Payment-Required header
    • 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-SIGNATURE header
  3. 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:

  1. Create a wallet: solana-keygen new -o agent-keypair.json
  2. Fund it: Send USDC and a small amount of SOL for transaction fees
  3. 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 serialisedkeypair_bytes is excluded from model_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_pr402-0.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_pr402-0.1.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file langchain_pr402-0.1.1.tar.gz.

File metadata

  • Download URL: langchain_pr402-0.1.1.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

Hashes for langchain_pr402-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8eff6a8a7affa0270ae719b8194176cac3875af30e1cc4c63d9c9607fbd0fc57
MD5 e552b37a5d3337049abfc66c9a559a71
BLAKE2b-256 61b2d7ac1bef6e6aad0e44ab9eaef464c59ce3c2597cce5f3adcb945779181c1

See more details on using hashes here.

File details

Details for the file langchain_pr402-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_pr402-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 928223f665ecc745af8cb8a53207db8556bd240eff4556b6475a8e256be66af2
MD5 5627fd8fb875bff4efdcce540f180057
BLAKE2b-256 29d8c9a818acde60bcecbcc144791c9ead17cbe54c3abc88a388a0d17d6faf6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page