Skip to main content
This example demonstrates how to create a Swarms agent that can search and query services from the X402 bazaar using the Coinbase CDP API. The agent can discover available services, filter them by price, and provide summaries of the results.

Overview

The X402 Discovery Query Agent enables you to:

Prerequisites

Before you begin, ensure you have:
  • Python 3.10 or higher
  • API keys for your AI model provider (e.g., Anthropic Claude)
  • httpx library for async HTTP requests

Installation

Install the required dependencies:

Code Example

Here’s the complete implementation of the X402 Discovery Query Agent:

Usage

Basic Query

Query all available services:

Filtered Query

Get services within a specific price range:

Using the Agent

Run the agent to get AI-powered summaries:

Understanding Price Units

X402 services use atomic units for pricing. For example:
  • USDC typically uses 6 decimals
  • 100,000 atomic units = $0.10 USDC
  • 1,000,000 atomic units = $1.00 USDC
Always check the accepts array in each service to understand the payment options and their price requirements.

API Response Structure

Each service in the response contains:
  • resource: The service endpoint or resource identifier
  • accepts: Array of payment options with maxAmountRequired values
  • Additional metadata about the service

Error Handling

The functions handle various error cases:
  • Network errors are raised as httpx.RequestError
  • HTTP errors are raised as httpx.HTTPError
  • Invalid price values are silently skipped during filtering

Next Steps

  1. Customize the agent’s system prompt for specific use cases
  2. Add additional filtering criteria (e.g., by service type)
  3. Implement caching for frequently accessed services
  4. Create a web interface for browsing services
  5. Integrate with payment processing to actually use discovered services