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

x402-langchain

v0.1.2

Published

LangChain StructuredTool adapter for x402 priced endpoints

Readme

x402-langchain

LangChain StructuredTool adapter for x402 priced endpoints. Wrap any x402 API in a LangChain-compatible tool with automatic payment handling.

Install

npm install x402-langchain @langchain/core zod
# or
pnpm add x402-langchain @langchain/core zod

Quick start

Standalone tool invoke (no LLM required)

import { X402Tool } from 'x402-langchain';
import { MockPayer } from 'x402-adapters';
import { z } from 'zod';

const weatherTool = new X402Tool({
  name: 'get_weather',
  description: 'Get current weather for a city',
  schema: z.object({
    city: z.string().describe('City name'),
  }),
  endpoint: 'http://localhost:3000/weather',
  method: 'GET',
  fetchOptions: { payer: new MockPayer({ secret: 'your-secret' }) },
});

const result = await weatherTool.invoke({ city: 'Tokyo' });
console.log(result);
// '{"city":"Tokyo","temp":22,"condition":"Sunny"}'

With a LangChain agent

import { X402Tool } from 'x402-langchain';
import { ChatOpenAI } from '@langchain/openai';
import { createReactAgent } from '@langchain/langgraph/prebuilt';
import { MockPayer } from 'x402-adapters';
import { z } from 'zod';

const payer = new MockPayer({ secret: 'your-secret' });

const weatherTool = new X402Tool({
  name: 'get_weather',
  description: 'Get current weather for a city. Costs 0.001 USDC per call.',
  schema: z.object({
    city: z.string().describe('City name'),
  }),
  endpoint: 'http://localhost:3000/weather',
  method: 'GET',
  fetchOptions: { payer },
});

const llm = new ChatOpenAI({ model: 'gpt-4o-mini' });
const agent = createReactAgent({ llm, tools: [weatherTool] });

const result = await agent.invoke({
  messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
});

console.log(result.messages.at(-1)?.content);

Multiple tools via factory

import { createX402Tools } from 'x402-langchain';
import { z } from 'zod';

const tools = createX402Tools([
  {
    name: 'get_weather',
    description: 'Get weather',
    schema: z.object({ city: z.string() }),
    endpoint: 'http://localhost:3000/weather',
    method: 'GET',
    fetchOptions: { payer },
  },
  {
    name: 'do_action',
    description: 'Perform an action',
    schema: z.object({ action: z.string() }),
    endpoint: 'http://localhost:3000/action',
    method: 'POST',
    fetchOptions: { payer },
  },
]);

With Solana USDC payer (production)

import { SolanaUSDCPayer } from 'x402-adapters/solana';

const payer = new SolanaUSDCPayer({
  privateKey: process.env.SOLANA_PRIVATE_KEY!,
});

const tool = new X402Tool({
  name: 'premium_api',
  description: 'Call a premium API',
  schema: z.object({ query: z.string() }),
  endpoint: 'https://api.example.com/search',
  method: 'POST',
  fetchOptions: { payer, maxRetries: 2 },
});

API Reference

| Export | Type | Description | |--------|------|-------------| | X402Tool | class | LangChain StructuredTool backed by an x402 endpoint | | createX402Tools | function | Factory to create multiple X402Tool instances | | X402ToolConfig | interface | Configuration type for X402Tool | | ToolException | class | Re-exported from @langchain/core/tools for error handling |

X402ToolConfig<T>

| Field | Type | Required | Description | |-------|------|----------|-------------| | name | string | ✅ | Tool name used by the LLM for function calling | | description | string | ✅ | Human-readable description of what the tool does | | schema | ZodObject<T> | ✅ | Zod schema defining the tool's input | | endpoint | string | ✅ | Full URL of the priced x402 endpoint | | method | string | | HTTP method (default: GET) | | fetchOptions | X402FetchOptions | ✅ | x402 fetch options including payer | | returnDirect | boolean | | Return tool result directly without agent reasoning (default: false) |