npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@openlibx402/langchain

v0.1.2

Published

LangChain.js integration for X402 payment protocol

Readme

@openlibx402/langchain

LangChain.js integration for the X402 payment protocol, enabling AI agents to autonomously access paid resources.

Overview

This package provides LangChain tools that allow AI agents to make HTTP requests to X402-protected resources with automatic payment handling. Built on top of @openlibx402/client, it seamlessly integrates blockchain payments into LangChain workflows.

Features

  • X402PaymentTool: LangChain tool for fetching paid resources
  • Automatic Payment: AI agents handle payments autonomously
  • Schema Validation: Built-in Zod schema validation for inputs
  • Solana Integration: Native support for Solana wallet payments
  • Agent-Ready: Designed specifically for autonomous AI agent workflows

Installation

npm install @openlibx402/langchain
# or
pnpm add @openlibx402/langchain
# or
yarn add @openlibx402/langchain

Usage

Basic Tool Setup

import { createX402PaymentTool } from '@openlibx402/langchain';
import { Keypair } from '@solana/web3.js';

// Load your AI agent's wallet
const walletKeypair = Keypair.fromSecretKey(secretKeyBytes);

// Create the X402 payment tool
const paymentTool = createX402PaymentTool({
  walletKeypair,
  rpcUrl: 'https://api.devnet.solana.com',
  maxPaymentAmount: '10000000' // Optional safety limit
});

// Use with LangChain agent
const tools = [paymentTool, /* other tools */];

Using with LangChain Agent

import { ChatOpenAI } from '@langchain/openai';
import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
import { pull } from 'langchain/hub';
import { createX402PaymentTool } from '@openlibx402/langchain';
import { Keypair } from '@solana/web3.js';

// Setup
const walletKeypair = Keypair.fromSecretKey(secretKeyBytes);
const model = new ChatOpenAI({ temperature: 0 });
const prompt = await pull('hwchase17/openai-functions-agent');

// Create tool
const x402Tool = createX402PaymentTool({
  walletKeypair,
  rpcUrl: 'https://api.devnet.solana.com',
  maxPaymentAmount: '10000000'
});

// Create agent
const agent = await createOpenAIFunctionsAgent({
  llm: model,
  tools: [x402Tool],
  prompt
});

const agentExecutor = new AgentExecutor({
  agent,
  tools: [x402Tool]
});

// Agent can now autonomously fetch paid resources
const result = await agentExecutor.invoke({
  input: 'Fetch the premium data from https://api.example.com/premium-data'
});

console.log(result.output);

Custom Tool Configuration

import { X402PaymentTool } from '@openlibx402/langchain';
import { Keypair } from '@solana/web3.js';

const walletKeypair = Keypair.fromSecretKey(secretKeyBytes);

const tool = new X402PaymentTool({
  walletKeypair,
  rpcUrl: 'https://api.mainnet-beta.solana.com',
  maxRetries: 2,
  autoRetry: true,
  maxPaymentAmount: '5000000'
});

// Use directly
const result = await tool._call(JSON.stringify({
  url: 'https://api.example.com/data',
  method: 'GET'
}));

console.log(result);

POST Requests with Data

// Agent can make POST requests with payment
const result = await agentExecutor.invoke({
  input: `
    Make a POST request to https://api.example.com/process with the following data:
    {
      "input": "some data to process"
    }
  `
});

Tool Input Schema

The X402PaymentTool accepts inputs with the following schema:

{
  url: string;          // Required: URL to fetch
  method?: string;      // Optional: HTTP method (default: 'GET')
  body?: object;        // Optional: Request body for POST/PUT
  headers?: object;     // Optional: Additional headers
}

Safety Features

import { createX402PaymentTool } from '@openlibx402/langchain';

const tool = createX402PaymentTool({
  walletKeypair,
  rpcUrl: 'https://api.devnet.solana.com',
  maxPaymentAmount: '10000000', // Maximum amount agent can spend per request
  maxRetries: 1,                 // Limit retry attempts
  autoRetry: true               // Enable automatic payment retry
});

API Reference

Functions

createX402PaymentTool(options: X402PaymentToolOptions)

Factory function to create an X402PaymentTool instance.

Options:

{
  walletKeypair: Keypair;       // Solana wallet keypair
  rpcUrl?: string;               // Solana RPC URL
  maxRetries?: number;           // Max retry attempts (default: 1)
  autoRetry?: boolean;           // Enable auto-retry (default: true)
  maxPaymentAmount?: string;     // Maximum payment amount per request
}

Classes

X402PaymentTool

LangChain tool class for making X402 payments.

Constructor:

constructor(options: X402PaymentToolOptions)

Methods:

  • _call(input: string) - Execute tool with JSON input string
  • Tool is automatically cleaned up when agent completes

Re-exports

  • X402AutoClient - Re-exported from @openlibx402/client for convenience

Example: Multi-Tool Agent

import { ChatOpenAI } from '@langchain/openai';
import { Calculator } from 'langchain/tools/calculator';
import { createX402PaymentTool } from '@openlibx402/langchain';
import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';

const walletKeypair = Keypair.fromSecretKey(secretKeyBytes);

// Create multiple tools including X402
const tools = [
  createX402PaymentTool({
    walletKeypair,
    rpcUrl: 'https://api.devnet.solana.com'
  }),
  new Calculator()
];

const agent = await createOpenAIFunctionsAgent({
  llm: new ChatOpenAI({ temperature: 0 }),
  tools,
  prompt: await pull('hwchase17/openai-functions-agent')
});

const executor = new AgentExecutor({ agent, tools });

// Agent can now use both calculation and paid API access
const result = await executor.invoke({
  input: 'Fetch premium data and calculate the average of the values'
});

Documentation

For complete API documentation and guides, visit openlibx402.github.io

Testing

pnpm test

Contributing

See CONTRIBUTING.md

License

MIT - See LICENSE