Skip to main content
X402 is a protocol that enables seamless cryptocurrency payments for API endpoints. This guide demonstrates how to monetize your Swarms agents by integrating X402 payment requirements into your FastAPI applications. With X402, you can:

Prerequisites

Before you begin, ensure you have:
  • Python 3.10 or higher
  • A cryptocurrency wallet address (for receiving payments)
  • API keys for your AI model provider (e.g., OpenAI)
  • An Exa API key (if using web search functionality)

Installation

Install the required dependencies:

Environment Setup

Create a .env file in your project root:

Basic X402 Integration Example

Here’s a complete example of a research agent with X402 payment integration:

Running Your Service

Start the server:
Or with uvicorn directly:
Your API will be available at:
  • Main endpoint: http://localhost:8000/
  • Research endpoint: http://localhost:8000/research
  • API docs: http://localhost:8000/docs

Next Steps

  1. Experiment with different pricing models
  2. Add multiple agents with specialized capabilities
  3. Implement analytics to track usage and revenue
  4. Deploy to production (see Deployment Solutions)
  5. Integrate with your existing payment processing