|
| 1 | +# X402 Payment Integration with Swarms Agents |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +With X402, you can: |
| 6 | + |
| 7 | +| Feature | Description | |
| 8 | +|-----------------------------------------------------|----------------------------------------------| |
| 9 | +| Charge per API request | Monetize your agents on a per-call basis | |
| 10 | +| Accept cryptocurrency payments | e.g., Base, Base Sepolia, and more | |
| 11 | +| Payment gate protection for agent endpoints | Secure endpoints with pay-to-access gates | |
| 12 | +| Create pay-per-use AI services | Offer AI agents as on-demand paid services | |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Before you begin, ensure you have: |
| 17 | + |
| 18 | +- Python 3.10 or higher |
| 19 | +- A cryptocurrency wallet address (for receiving payments) |
| 20 | +- API keys for your AI model provider (e.g., OpenAI) |
| 21 | +- An Exa API key (if using web search functionality) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +Install the required dependencies: |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install swarms x402 fastapi uvicorn python-dotenv swarms-tools |
| 29 | +``` |
| 30 | + |
| 31 | +## Environment Setup |
| 32 | + |
| 33 | +Create a `.env` file in your project root: |
| 34 | + |
| 35 | +```bash |
| 36 | +# OpenAI API Key |
| 37 | +OPENAI_API_KEY=your_openai_api_key_here |
| 38 | + |
| 39 | +# Exa API Key (for web search) |
| 40 | +EXA_API_KEY=your_exa_api_key_here |
| 41 | + |
| 42 | +# Your wallet address (where you'll receive payments) |
| 43 | +WALLET_ADDRESS=0xYourWalletAddressHere |
| 44 | +``` |
| 45 | + |
| 46 | +## Basic X402 Integration Example |
| 47 | + |
| 48 | +Here's a complete example of a research agent with X402 payment integration: |
| 49 | + |
| 50 | +```python |
| 51 | +from dotenv import load_dotenv |
| 52 | +from fastapi import FastAPI |
| 53 | +from swarms_tools import exa_search |
| 54 | + |
| 55 | +from swarms import Agent |
| 56 | +from x402.fastapi.middleware import require_payment |
| 57 | + |
| 58 | +# Load environment variables |
| 59 | +load_dotenv() |
| 60 | + |
| 61 | +app = FastAPI(title="Research Agent API") |
| 62 | + |
| 63 | +# Initialize the research agent |
| 64 | +research_agent = Agent( |
| 65 | + agent_name="Research-Agent", |
| 66 | + system_prompt="You are an expert research analyst. Conduct thorough research on the given topic and provide comprehensive, well-structured insights with citations.", |
| 67 | + model_name="gpt-4o-mini", |
| 68 | + max_loops=1, |
| 69 | + tools=[exa_search], |
| 70 | +) |
| 71 | + |
| 72 | + |
| 73 | +# Apply x402 payment middleware to the research endpoint |
| 74 | +app.middleware("http")( |
| 75 | + require_payment( |
| 76 | + path="/research", |
| 77 | + price="$0.01", |
| 78 | + pay_to_address="0xYourWalletAddressHere", |
| 79 | + network_id="base-sepolia", |
| 80 | + description="AI-powered research agent that conducts comprehensive research on any topic", |
| 81 | + input_schema={ |
| 82 | + "type": "object", |
| 83 | + "properties": { |
| 84 | + "query": { |
| 85 | + "type": "string", |
| 86 | + "description": "Research topic or question", |
| 87 | + } |
| 88 | + }, |
| 89 | + "required": ["query"], |
| 90 | + }, |
| 91 | + output_schema={ |
| 92 | + "type": "object", |
| 93 | + "properties": { |
| 94 | + "research": { |
| 95 | + "type": "string", |
| 96 | + "description": "Comprehensive research results", |
| 97 | + } |
| 98 | + }, |
| 99 | + }, |
| 100 | + ) |
| 101 | +) |
| 102 | + |
| 103 | + |
| 104 | +@app.get("/research") |
| 105 | +async def conduct_research(query: str): |
| 106 | + """ |
| 107 | + Conduct research on a given topic using the research agent. |
| 108 | +
|
| 109 | + Args: |
| 110 | + query: The research topic or question |
| 111 | +
|
| 112 | + Returns: |
| 113 | + Research results from the agent |
| 114 | + """ |
| 115 | + result = research_agent.run(query) |
| 116 | + return {"research": result} |
| 117 | + |
| 118 | + |
| 119 | +@app.get("/") |
| 120 | +async def root(): |
| 121 | + """Health check endpoint (free, no payment required)""" |
| 122 | + return { |
| 123 | + "message": "Research Agent API with x402 payments", |
| 124 | + "endpoints": { |
| 125 | + "/research": "Paid endpoint - $0.01 per request", |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + import uvicorn |
| 132 | + |
| 133 | + uvicorn.run(app, host="0.0.0.0", port=8000) |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +## Running Your Service |
| 138 | + |
| 139 | +Start the server: |
| 140 | + |
| 141 | +```bash |
| 142 | +python research_agent_x402_example.py |
| 143 | +``` |
| 144 | + |
| 145 | +Or with uvicorn directly: |
| 146 | + |
| 147 | +```bash |
| 148 | +uvicorn research_agent_x402_example:app --host 0.0.0.0 --port 8000 --reload |
| 149 | +``` |
| 150 | + |
| 151 | +Your API will be available at: |
| 152 | + |
| 153 | +- Main endpoint: `http://localhost:8000/` |
| 154 | + |
| 155 | +- Research endpoint: `http://localhost:8000/research` |
| 156 | + |
| 157 | +- API docs: `http://localhost:8000/docs` |
| 158 | + |
| 159 | + |
| 160 | +## Next Steps |
| 161 | + |
| 162 | +1. Experiment with different pricing models |
| 163 | +2. Add multiple agents with specialized capabilities |
| 164 | +3. Implement analytics to track usage and revenue |
| 165 | +4. Deploy to production (see [Deployment Solutions](../deployment_solutions/overview.md)) |
| 166 | +5. Integrate with your existing payment processing |
0 commit comments