Skip to main content

LangChain tools for x402 payments - Give AI agents the ability to pay

Project description

Nory LangChain Tools

Give your LangChain agents the ability to pay.

Nory LangChain Tools provides a set of LangChain-compatible tools that enable AI agents to make instant payments using the x402 protocol. Payments settle on Solana in under 1 second.

Installation

pip install nory-langchain

Quick Start

from langchain.agents import initialize_agent, AgentType
from langchain_openai import ChatOpenAI
from nory_langchain import get_nory_tools

# Initialize LLM
llm = ChatOpenAI(model="gpt-4")

# Get all Nory payment tools
tools = get_nory_tools()

# Create agent with payment capabilities
agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Your agent can now make payments!
agent.run("Check my Nory wallet balance")
agent.run("Pay 5 USDC to GkXn...abc for the API access")
agent.run("Use x402 to fetch data from https://api.example.com/premium-data")

Configuration

Set your environment variables:

export NORY_WALLET_KEY="your-solana-wallet-private-key"
export NORY_API_URL="https://api.noryx402.com"  # Optional, this is the default

Available Tools

nory_check_balance

Check your wallet balance before making payments.

from nory_langchain import NoryCheckBalanceTool

tool = NoryCheckBalanceTool()
result = tool.run({"currency": "USDC"})

nory_pay

Make direct payments to any Solana wallet address.

from nory_langchain import NoryPayTool

tool = NoryPayTool()
result = tool.run({
    "recipient": "GkXn9FqJ4pT1qVz8hQJ9X5...",
    "amount": 5.0,
    "memo": "Payment for API access"
})

nory_x402_request

Access x402-enabled APIs with automatic payment handling. When the server returns HTTP 402 (Payment Required), Nory automatically pays and retries.

from nory_langchain import NoryX402RequestTool

tool = NoryX402RequestTool()
result = tool.run({
    "url": "https://api.noryx402.com/data/crypto",
    "method": "GET"
})

nory_discover_services

Find available x402-enabled services and APIs.

from nory_langchain import NoryDiscoverServicesTool

tool = NoryDiscoverServicesTool()
result = tool.run({
    "category": "ai",
    "max_price": 0.01
})

nory_payment_history

View your recent payment transactions.

from nory_langchain import NoryPaymentHistoryTool

tool = NoryPaymentHistoryTool()
result = tool.run({"limit": 10})

Using with Different Agent Types

ReAct Agent

from langchain.agents import create_react_agent
from langchain import hub
from nory_langchain import get_nory_tools

prompt = hub.pull("hwchase17/react")
tools = get_nory_tools()

agent = create_react_agent(llm, tools, prompt)

OpenAI Functions Agent

from langchain.agents import create_openai_functions_agent
from langchain import hub
from nory_langchain import get_nory_tools

prompt = hub.pull("hwchase17/openai-functions-agent")
tools = get_nory_tools()

agent = create_openai_functions_agent(llm, tools, prompt)

Tool Calling Agent (Recommended)

from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from nory_langchain import get_nory_tools

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant with payment capabilities."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

tools = get_nory_tools()
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

executor.invoke({"input": "Check my balance and pay 1 USDC to Alice"})

How x402 Works

  1. Request - Agent makes HTTP request to x402-enabled endpoint
  2. 402 Response - Server returns HTTP 402 with payment requirements
  3. Payment - Nory creates and signs a Solana transaction
  4. Settlement - Transaction confirms in ~400ms
  5. Access - Agent retries with payment proof, receives the resource

All of this happens automatically when using nory_x402_request.

Use Cases

  • API Access: Pay-per-call access to premium APIs
  • AI Services: Pay for model inference, image generation
  • Data Feeds: Access premium market data, research reports
  • Compute: Purchase serverless compute, GPU time
  • Agent-to-Agent: Agents paying other agents for services

Security

  • Non-custodial: Your private key stays on your machine
  • Per-transaction limits: Configure maximum payment amounts
  • Audit logging: All transactions are logged

Links

License

MIT

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

nory_langchain-0.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

nory_langchain-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file nory_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: nory_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for nory_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ae17395eeef1e7055f22ab37e0246c5e659e12bea2b4a8f00c7e5d66a78248e
MD5 6841c9fe6e72887b28aa487f2c94fbde
BLAKE2b-256 41764443ef98042906aedd55d9796f3ee8bd127f5c5753a38af126e98535e287

See more details on using hashes here.

File details

Details for the file nory_langchain-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nory_langchain-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for nory_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bdbcfea4ae6136e98cf6419d963afa9a5c55f986cc6f38cf81bdb72e2ca867f
MD5 d36b93c1a9f4969e66ff6a823e12c2a0
BLAKE2b-256 9149df1bccadccd7a95b308d86ee1f1dd0705042ccec8928fd6664abc41362b0

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