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/core

Basic 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

Combined node that fetches API and handles payment automatically:

paymentNode

Separate 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

  1. Set maximum payment amounts to prevent overpayment

  2. Monitor payment usage and costs

  3. Handle payment errors gracefully

  4. Use appropriate wallet management for production

  5. Test with devnet before production

  6. Log payment transactions for audit

  7. Consider rate limiting for payment tools

  8. Use separate wallets for different agents if needed

Next Steps

Last updated