LangChain tools for x402 Engine — pay-per-call AI, crypto, and storage APIs via HTTP 402 micropayments.
Give your LangChain agents the ability to generate images, execute code, transcribe audio, look up crypto prices, analyze wallets, and pin data to IPFS — all paid with stablecoins (USDC on Base/Solana, USDm on MegaETH).
npm install langchain-x402 @langchain/coreimport { createX402Tools } from "langchain-x402";
// Create all tools with shared config
const tools = createX402Tools({
paymentHeader: process.env.X402_PAYMENT_HEADER,
});
// Or import individual tools
import { GenerateImageTool, CryptoPriceTool } from "langchain-x402";
const imageTool = new GenerateImageTool();
const priceTool = new CryptoPriceTool();import { ChatOpenAI } from "@langchain/openai";
import { createToolCallingAgent, AgentExecutor } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { createX402Tools } from "langchain-x402";
const llm = new ChatOpenAI({ model: "gpt-4o" });
const tools = createX402Tools();
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant with access to x402 Engine APIs."],
["human", "{input}"],
["placeholder", "{agent_scratchpad}"],
]);
const agent = createToolCallingAgent({ llm, tools, prompt });
const executor = new AgentExecutor({ agent, tools });
const result = await executor.invoke({
input: "What's the current price of Bitcoin and Ethereum?",
});Tools accept an optional config object and/or read from environment variables:
| Config Key | Env Variable | Description |
|---|---|---|
baseUrl |
X402_BASE_URL |
Gateway URL (default: https://x402engine.app) |
paymentHeader |
X402_PAYMENT_HEADER |
Pre-signed X-PAYMENT header for x402 |
devBypass |
X402_DEV_BYPASS |
Dev bypass token to skip payment |
| Tool | Description | Price |
|---|---|---|
GenerateImageTool |
AI image generation (fast/quality/text tiers) | $0.015-$0.12 |
ExecuteCodeTool |
Sandboxed code execution (Python, JS, Bash, R) | $0.005 |
TranscribeAudioTool |
Audio-to-text transcription (Deepgram Nova-3) | $0.10 |
| Tool | Description | Price |
|---|---|---|
CryptoPriceTool |
Real-time cryptocurrency prices | $0.001 |
CryptoMarketsTool |
Market rankings with detailed data | $0.002 |
CryptoHistoryTool |
Historical price/volume/mcap data | $0.003 |
TrendingCryptoTool |
Currently trending coins | $0.001 |
CryptoSearchTool |
Search coins by name or symbol | $0.001 |
| Tool | Description | Price |
|---|---|---|
WalletBalancesTool |
Token balances across 20+ chains | $0.005 |
WalletTransactionsTool |
Transaction history with labels | $0.005 |
WalletPnlTool |
Portfolio profit/loss analysis | $0.01 |
TokenPricesTool |
DEX-derived token prices (up to 200/req) | $0.005 |
TokenMetadataTool |
Token metadata (name, symbol, decimals) | $0.002 |
| Tool | Description | Price |
|---|---|---|
PinJsonToIpfsTool |
Pin JSON to IPFS, returns CID | $0.01 |
GetFromIpfsTool |
Retrieve content from IPFS by CID | $0.001 |
x402 is an open protocol for HTTP-native micropayments. When a tool makes a request:
- If no payment header is configured, the API returns HTTP 402 with payment requirements
- The tool returns the payment details so your agent (or a higher-level system) can handle payment
- With a valid
X-PAYMENTheader, the request succeeds and the API responds normally
For automatic payment handling, use @x402/client to generate payment headers from a wallet.
| Network | Stablecoin | Confirmation |
|---|---|---|
| Base | USDC | ~2s |
| Solana | USDC | ~400ms |
| MegaETH | USDm | ~10ms |
All tools extend X402BaseTool, which itself extends LangChain's StructuredTool. You can create custom tools by extending the base class:
import { z } from "zod";
import { X402BaseTool } from "langchain-x402";
class MyCustomTool extends X402BaseTool {
name = "my_custom_tool";
description = "My custom x402 tool";
schema = z.object({ input: z.string() });
async _call(input: { input: string }) {
const res = await this.callApi("POST", "/api/my-endpoint", undefined, input);
return this.formatResult(res);
}
}- x402 Engine — API gateway
- x402 Protocol — Payment protocol spec
- MCP Server — Model Context Protocol server
- GitHub
MIT