Skip to main content

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 serialised — keypair_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

Metadata

Release files for langchain-pr402 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-pr402 0.1.1
File Size Uploaded Provenance Actions
langchain_pr402-0.1.1.tar.gz 10.2 kB Without provenance Details for langchain_pr402-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-pr402 0.1.1
File Interpreter ABI Platform Actions
langchain_pr402-0.1.1-py3-none-any.whl Python 3 none any Details for langchain_pr402-0.1.1-py3-none-any.whl

Total release size: 19.0 kB

Release files / langchain_pr402-0.1.1.tar.gz

Download URL langchain_pr402-0.1.1.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8eff6a8a7affa0270ae719b8194176cac3875af30e1cc4c63d9c9607fbd0fc57
BLAKE2b-256 checksum
How to use checksums
61b2d7ac1bef6e6aad0e44ab9eaef464c59ce3c2597cce5f3adcb945779181c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / langchain_pr402-0.1.1-py3-none-any.whl

Download URL langchain_pr402-0.1.1-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
928223f665ecc745af8cb8a53207db8556bd240eff4556b6475a8e256be66af2
BLAKE2b-256 checksum
How to use checksums
29d8c9a818acde60bcecbcc144791c9ead17cbe54c3abc88a388a0d17d6faf6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Security for langchain-pr402 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page