Back to Academy
Level 315 min

LangChain Integration

Use 402bazaar.com services as LangChain tools. Agents discover, evaluate, and pay for the best service automatically.

1

Create a 402bazaar.com tool

Wrap the x402 payment flow in a LangChain-compatible tool.

TypeScript
import { Tool } from "langchain/tools";

class BazaarTool extends Tool {
  name = "bazaar_call";
  description = "Call a paid API via x402. Input: slug and optional body.";

  async _call(input: string) {
    const { slug, body } = JSON.parse(input);
    const res = await x402Fetch(`/api/v1/proxy/${slug}`, {
      method: "POST",
      body: JSON.stringify(body)
    });
    return JSON.stringify(await res.json());
  }
}
2

Add discovery tool

Let your agent search the marketplace to find the right service for any task.

TypeScript
class BazaarDiscoverTool extends Tool {
  name = "bazaar_discover";
  description = "Search 402bazaar.com marketplace. Input: search query.";

  async _call(query: string) {
    const res = await fetch(
      `/api/v1/discover?q=${encodeURIComponent(query)}`
    );
    const { services } = await res.json();
    return JSON.stringify(services.slice(0, 5));
  }
}
3

Use in an agent chain

Combine discovery + execution in an agent that autonomously finds and pays for services.

TypeScript
import { initializeAgentExecutorWithOptions } from "langchain/agents";

const tools = [new BazaarDiscoverTool(), new BazaarTool()];
const agent = await initializeAgentExecutorWithOptions(tools, llm, {
  agentType: "openai-functions",
});

const result = await agent.invoke({
  input: "Find and call a weather service for NYC"
});