Integration Guide 👥 CrewAI

CrewAI Integration

Let your CrewAI agents discover and use x402 services dynamically. Build crews that can find their own tools.

What you'll build: A CrewAI crew where agents can search for and use any x402 API they need — from crypto prices to weather data to AI inference.

Prerequisites

Installation

pip install agentindex crewai crewai-tools

Quick Start

Create a CrewAI tool that wraps Agent Index:

from crewai import Agent, Task, Crew
from crewai_tools import tool
from agentindex import AgentIndex

# Create the search tool
@tool("Search x402 Agents")
def search_agents(query: str) -> str:
    """
    Search for x402 AI agents that can perform specific tasks.
    Use this to find APIs for crypto data, weather, news, AI models, etc.
    
    Args:
        query: What capability you're looking for (e.g., "crypto prices", "weather forecast")
    
    Returns:
        List of matching agents with URLs and prices
    """
    client = AgentIndex()
    results = client.search(query, limit=5)
    
    if not results.results:
        return "No agents found matching your query."
    
    output = []
    for agent in results.results:
        output.append(
            f"• {agent.description}\n"
            f"  URL: {agent.url}\n"
            f"  Price: ${agent.price_usd}/call | Health: {agent.health}"
        )
    
    return "\n\n".join(output)

# Create an agent that uses this tool
researcher = Agent(
    role="API Researcher",
    goal="Find the best APIs for any data need",
    backstory="You are an expert at finding and evaluating x402 APIs. "
              "You know how to search for specific capabilities and "
              "evaluate them based on price, reliability, and features.",
    tools=[search_agents],
    verbose=True
)

# Create a task
find_crypto_api = Task(
    description="Find the best API for getting real-time Bitcoin prices. "
                "Consider price, reliability (health status), and features. "
                "Recommend the top 2-3 options with reasoning.",
    expected_output="A recommendation of the best crypto price APIs with URLs and justification",
    agent=researcher
)

# Run the crew
crew = Crew(
    agents=[researcher],
    tasks=[find_crypto_api],
    verbose=True
)

result = crew.kickoff()
print(result)

Advanced: Multi-Agent Research Crew

Build a crew with specialized agents for different API categories:

from crewai import Agent, Task, Crew, Process
from crewai_tools import tool
from agentindex import AgentIndex
import httpx

client = AgentIndex()

@tool("Search Crypto APIs")
def search_crypto_apis(query: str) -> str:
    """Search for cryptocurrency-related APIs."""
    results = client.search(query, category="crypto", limit=5)
    return format_results(results)

@tool("Search Weather APIs")
def search_weather_apis(query: str) -> str:
    """Search for weather and climate APIs."""
    results = client.search(query, category="weather", limit=5)
    return format_results(results)

@tool("Search AI APIs")
def search_ai_apis(query: str) -> str:
    """Search for AI and ML inference APIs."""
    results = client.search(query, category="ai/ml", limit=5)
    return format_results(results)

@tool("Call API Endpoint")
def call_api(url: str) -> str:
    """Call an x402 API endpoint and return the response."""
    try:
        response = httpx.get(url, timeout=10)
        return response.text[:2000]
    except Exception as e:
        return f"Error calling API: {e}"

def format_results(results):
    if not results.results:
        return "No APIs found."
    return "\n".join([
        f"• {a.description} - {a.url} (${a.price_usd}/call, {a.health})"
        for a in results.results
    ])

# Specialized agents
crypto_specialist = Agent(
    role="Crypto Data Specialist",
    goal="Find the best cryptocurrency data APIs",
    backstory="Expert in DeFi and crypto data sources",
    tools=[search_crypto_apis, call_api],
    verbose=True
)

data_analyst = Agent(
    role="Data Analyst",
    goal="Analyze and compare API options",
    backstory="Expert at evaluating APIs for reliability and value",
    tools=[],
    verbose=True
)

# Tasks
find_apis = Task(
    description="Find APIs that can provide real-time ETH and BTC prices",
    expected_output="List of suitable APIs with URLs",
    agent=crypto_specialist
)

analyze_apis = Task(
    description="Analyze the found APIs and recommend the best one based on "
                "price, reliability, and data quality",
    expected_output="Final recommendation with reasoning",
    agent=data_analyst,
    context=[find_apis]  # Uses output from previous task
)

# Run the crew
crew = Crew(
    agents=[crypto_specialist, data_analyst],
    tasks=[find_apis, analyze_apis],
    process=Process.sequential,
    verbose=True
)

result = crew.kickoff()
print(result)

Category-Specific Tools

Pre-built tool functions for common categories:

from agentindex import AgentIndex

client = AgentIndex()

def make_category_tool(category: str, name: str, description: str):
    """Factory for category-specific search tools."""
    
    @tool(name)
    def category_search(query: str) -> str:
        __doc__ = description
        results = client.search(query, category=category, limit=5)
        if not results.results:
            return f"No {category} APIs found for: {query}"
        return "\n".join([
            f"• {a.description} ({a.url}) - ${a.price_usd}/call"
            for a in results.results
        ])
    
    return category_search

# Create tools for each category
crypto_tool = make_category_tool("crypto", "Search Crypto APIs", "Find cryptocurrency APIs")
defi_tool = make_category_tool("defi", "Search DeFi APIs", "Find DeFi analytics APIs")
ai_tool = make_category_tool("ai/ml", "Search AI APIs", "Find AI inference APIs")
weather_tool = make_category_tool("weather", "Search Weather APIs", "Find weather APIs")
news_tool = make_category_tool("news", "Search News APIs", "Find news and media APIs")

Best Practices

Recommended Tools

Build production-ready CrewAI applications with these tools:

🤖 LLM Providers

💾 Data & State

🚀 Deployment

View our full recommended stack →

Next Steps

Install from PyPI →