AI Agent Integration
Shade402 provides integrations for popular AI agent frameworks, enabling autonomous agents to make payments for API access automatically.
Supported Frameworks
LangChain.js: Payment tool for LangChain agents
LangGraph.js: Payment nodes for LangGraph workflows
LangChain Integration
The @shade402/langchain package provides a payment tool that agents can use to access paid APIs.
Installation
pnpm add @shade402/langchain @shade402/client @shade402/coreBasic Usage
import { createX402PaymentTool } from '@shade402/langchain';
import { Keypair } from '@solana/web3.js';
import { ChatOpenAI } from '@langchain/openai';
import { AgentExecutor, createOpenAIFunctionsAgent } from 'langchain/agents';
// Create wallet
const wallet = Keypair.generate();
// Create payment tool
const paymentTool = createX402PaymentTool({
walletKeypair: wallet,
rpcUrl: process.env.SOLANA_RPC_URL,
maxPayment: '1.0', // Maximum payment per request
name: 'x402_payment',
description: 'Make an X402 payment to access a paid API endpoint',
});
// Create agent with payment tool
const llm = new ChatOpenAI({ temperature: 0 });
const tools = [paymentTool];
const agent = await createOpenAIFunctionsAgent({
llm,
tools,
prompt: myPrompt,
});
const executor = new AgentExecutor({
agent,
tools,
});
// Agent can now use the payment tool
const result = await executor.invoke({
input: 'Fetch data from https://api.example.com/premium-data',
});Tool Configuration
Tool Usage
The agent will use the tool when it needs to access a paid API:
LangGraph Integration
The @shade402/langgraph package provides payment nodes for LangGraph workflows.
Installation
Basic Usage
Payment Nodes
fetchWithPaymentNode
fetchWithPaymentNodeCombined node that fetches API and handles payment automatically:
paymentNode
paymentNodeSeparate payment node for more control:
Conditional Edges
Use conditional functions to route based on payment status:
State Interface
The PaymentState interface includes:
Complete Examples
LangChain Agent with Payment Tool
LangGraph Workflow with Payment
Best Practices
Set maximum payment amounts to prevent overpayment
Monitor payment usage and costs
Handle payment errors gracefully
Use appropriate wallet management for production
Test with devnet before production
Log payment transactions for audit
Consider rate limiting for payment tools
Use separate wallets for different agents if needed
Next Steps
Learn about LangChain Integration in detail
Explore LangGraph Integration features
Check out Security best practices
Last updated
